1

我正在使用 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
   g = Graph()


   vertex1  = g.vertices.get_or_create('dpid',dpid_str,{'state':'active','dpid':dpid_str,'type':'switch'}))
   vertex2  = g.vertices.get_or_create('desc',desc,{'desc':desc,'port_id':port_id,'state':state,'port_state':port_state,'number':number,'type':'port'}))

从示例中,我了解如何在顶点之间创建边缘,如下所示

 g.edges.create(vertex1,"out",vertex2)

但是假设我在我的程序中没有对顶点的引用。

我想使用它的键“dpid”检索顶点 1 并且我想使用它的键“desc”检索顶点 2

然后使用检索到的值创建边缘。我该怎么做?

4

1 回答 1

1

要通过索引属性(而不是其数据库 ID)检索顶点,可以使用 Bulbs 内置索引方法之一:

>>> # returns an iterator (can return more than 1)
>>> vertices = g.vertices.index.lookup("dpid", dpid_str)   
>>> vertex1 = vertices.next()

>>> # returns 1 vertex or None (errors if more than 1)
>>> vertex2 = g.vertices.index.get_unique( "dpid", dpid_str)  

要创建边缘,只需执行...

>>> g.edges.create(vertex1, "out", vertex2)

注意:您不需要将边缘标记为“out”(“out”是指边缘从顶点 1 出来并进入顶点 2 的方向)。您应该考虑使用更具描述性的标签。

看...

Rexter 索引文档:

index.lookup() https://github.com/espeed/bulbs/blob/afa28ccbacd2fb92e0039800090b8aa8bf2c6813/bulbs/titan/index.py#L251

index.get_unique() https://github.com/espeed/bulbs/blob/afa28ccbacd2fb92e0039800090b8aa8bf2c6813/bulbs/titan/index.py#L274

于 2014-06-17T19:36:40.807 回答