我正在使用 TitanGraphDB + Cassandra。我按如下方式启动 Titan
cd titan-cassandra-0.3.1
bin/titan.sh config/titan-server-rexster.xml config/titan-server-cassandra.properties
我有一个 Rexster shell,可以用来与上面的 Titan+Cassandra 通信。
cd rexster-console-2.3.0
bin/rexster-console.sh
我想通过我的 python 程序对 Titan Graph DB 进行编程。为此我使用了 bulbs 包。
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 。或者它是否默认检查所有属性。
我是 python 的初学者,我并不真正理解
get_or_create('name',"James",{'name':'james')
为什么要这样指定函数属性?
get_or_create() 的函数定义是 here
我们将不胜感激。
请您参考如下方法:
Bulbs 的“get_or_create()”方法在索引中查找一个顶点,如果不存在则创建它。您可以像使用 create() 一样提供数据库属性的 get_or_create() Python dict。
看...
- http://bulbflow.com/docs/api/bulbs/element/#vertex-proxy
- https://github.com/espeed/bulbs/blob/master/bulbs/element.py#L584
这里有几个例子......
>>> # 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'}
