2

我正在使用 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 进行编程。我为此使用了灯泡包。

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()

我首先需要创建一个密钥。或者它是否默认检查所有属性。

我是python的初学者,我并没有真正理解
get_or_create('name',"James",{'name':'james')

为什么要这样指定函数属性。?

get_or_create() 的函数定义在 这里

任何帮助将不胜感激。

4

1 回答 1

2

Bulbs 'get_or_create()' 方法在索引中查找一个顶点,如果它不存在则创建它。您可以get_or_create()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'}
于 2014-06-17T20:08:39.473 回答