从Titan Graph Node获取<key,value>对</key,value>

时间:2014-07-16 06:55:10

标签: cassandra titan bulbs

我正在使用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。我正在使用灯泡包。

我使用灯泡从python创建3种类型的顶点,如下所示。

有3种类型的顶点
- switch
- port
- device

 from bulbs.titan import Graph
 vswitch = self.g.vertices.get_or_create('dpid',dpid_str,{'state':'active','dpid':dpid_str,'type':'switch'})
 vport   = self.g.vertices.get_or_create('port_id',port_id,{'desc':desc,'port_id':port_id,'state':state,'port_state':port_state,'number':number,'type':'port'})

如果我尝试打印变量vswitch,vport和vdevice,我会得到以下结果。

vswitch     <Vertex: http://localhost:8182/graphs/graph/vertices/4>
vport       <Vertex: http://localhost:8182/graphs/graph/vertices/28>

但是,如果我尝试使用键检索上述顶点,如下所示。

vswitch = self.g.vertices.index.lookup(dpid=dpid_str)
vport   = self.g.vertices.index.lookup(port_id=port_id_str)

for s in vswitch
     print s

Here 's' prints the the node `<Vertex: http://localhost:8182/graphs/graph/vertices/4>`
which is expected.How do I extract the key-value pairs from this node?

'dpid'  : dpid_str,
'state' :'active',
'type'  :'switch'

1 个答案:

答案 0 :(得分:1)

>>> switches = self.g.vertices.index.lookup(dpid=dpid_str)
>>> ports    = self.g.vertices.index.lookup(port_id=port_id_str)

for s in switches
     print s.dpid, s.state, s.type
     print s.data()

...或者如果顶点是唯一的(仅返回一个值),则使用index.get_unique()而不是index.lookup()...

 >>> switch = self.g.vertices.index.get_unique(dpid=dpid_str)
 >>> print switch.dpid, switch.state, switch.type
 >>> print switch.data()

有关详情,请参阅灯泡文档和快速入门:

相关问题