我使用的是TitanGraphDB + Cassandra。我以如下方式启动Titan
cd titan-cassandra-0.3.1
bin/titan.sh config/titan-server-rexster.xml config/titan-server-cassandra.properties
我有一个Rexster外壳,我可以用它来与上面的泰坦+卡桑德拉通信。
cd rexster-console-2.3.0
bin/rexster-console.sh
我想从我的python程序编程的泰坦图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)
但假设我没有对程序中顶点的引用。
我想检索vertex1使用它的键"dpid"和我想使用键"desc"检索vertex2
然后使用检索值我想创建的边。我该怎么做呢?
要通过索引属性(而不是数据库ID)检索顶点,您可以使用一个bulb内置索引方法:
>>> # 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索引文档:
- http://bulbflow.com/docs/api/bulbs/rexster/indices/
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