我正在使用TitanGraphDB + Cassandra.I,我将按照以下方式启动土卫六
cd titan-cassandra-0.3.1
bin/titan.sh config/titan-server-rexster.xml config/titan-server-cassandra.properties我有一个Rexster外壳,我可以使用它与上面的Titan+Cassandra通信。
cd rexster-console-2.3.0
bin/rexster-console.sh我想从我的python程序中编写土卫六图形数据库,我正在使用灯泡包。
from bulbs.titan import Graph我想用get_or_create()替换create()调用
我在网上看到了下面的例子。
james = g.vertices.create(name="James")如下图所示。
james = g.vertices.get_or_create('name',"James",{'name':'james')现在我的顶点创建函数如下所示。
self.g.vertices.create({ 'desc':desc,
'port_id':port_id,
'state':state,
'port_state':port_state,
'number':number,
'type':'port'} )如果我想重写上面的函数调用(create()),它使用get_or_create()来获取多个键值对。
我首先需要创建一个key.Or,默认情况下它会检查所有属性吗?
我是蟒蛇的初学者,我不知道
get_or_create('name',"James",{'name':'james')
为什么函数属性是这样指定的?
get_or_create()的函数定义是这里
任何帮助都将不胜感激。
发布于 2014-06-17 20:08:39
灯泡'get_or_create()‘方法在索引中查找顶点并在不存在的情况下创建它。您可以向get_or_create()提供数据库属性的Python dict,就像使用create()一样。
你看..。
这里有几个例子..。
>>> # a vertex where name is "James" doesn't exist so lookup() returns None
>>> g.vertices.index.lookup("name", "James")
None
>>> # a vertex where name is "James" doesn't exist so a vertex is created
>>> data = dict(name="James", city="Dallas")
>>> james = g.vertices.get_or_create("name", "James", data)
>>> james.data()
{'city': 'Dallas', 'name': 'James'}
>>> james.eid # returns the element ID for the james vertex
>>> 1
>>> # a vertex where name is "James" DOES exist so vertex is returned unmodified
>>> data = dict(name="James", city="Dallas", age=35)
>>> james = g.vertices.get_or_create("name", "James", data)
>>> james.data() # note age=35 was not added to the vertex properties
{'city': 'Dallas', 'name': 'James'}
>>> # one way to update the vertex properities
>>> james.age = 35
>>> james.save()
>>> james.data()
>>> {'city': 'Dallas', 'age': 35, 'name': 'James'}
>>> # a way to update the vertex properties if you only have the vertex ID
>>> # the vertex ID for the james vertex is 1
>>> data = dict(name="James", city="Dallas", age=35)
>>> g.vertices.update(1, data)
>>> james = g.vertices.get(1)
>>> james.data()
>>> {'city': 'Dallas', 'age': 35, 'name': 'James'}https://stackoverflow.com/questions/24158348
复制相似问题