我正在使用TitanGraphDB + Cassandra.I,我将按照以下方式启动土卫六
cd titan-cassandra-0.3.1
bin/titan.sh config/titan-server-rexster.xml config/titan-server-cassandra.properties我有一个Rexster外壳,我可以使用它与上面的Titan+Cassandra通信。
cd rexster-console-2.3.0
bin/rexster-console.sh我想从我的python程序中编写土卫六图形数据库,我正在使用灯泡包。
我从python中创建了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)并尝试打印出vswitch和vport变量,我得到以下值
<generator object <genexpr> at 0x26d6370>)
<generator object <genexpr> at 0x26d63c0>我是否在尝试使用g.vertices.index.lookup(dpid=dpid_str)检索上面的顶点时做错了什么?
发布于 2014-06-17 19:27:54
g.vertices.index.lookup()方法返回一个Python生成器 (这是迭代器的一种类型)。
使用next()获取生成器中的下一个值:
>>> # lookup() returns an generator (can return more than 1 value)
>>> switches = self.g.vertices.index.lookup(dpid=dpid_str)
>>> switch = switches.next()
>>> ports = self.g.vertices.index.lookup(port_id=port_id_str)
>>> port = ports.next()也可以使用list()将generator转换为Python list
>>> switches = self.g.vertices.index.lookup(dpid=dpid_str)
>>> list(switches)
>>> ports = self.g.vertices.index.lookup(port_id=port_id_str)
>>> list(ports)但是,如果索引项是唯一的,则可以使用get_unique()方法返回一个值或None。
# returns 1 vertex or None (errors if more than 1)
>>> vertex = g.vertices.index.get_unique( "dpid", dpid_str) 你看..。
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
注意到:迭代器和生成器是programming的基础--它们在任何地方都使用,并不是专门针对灯泡的--如果您是Python编程新手,请参阅我对我如何学会用Python编程?的回答,以获得一个用于学习使用我如何学会用Python编程?编程的好的在线资源列表。
https://stackoverflow.com/questions/24239327
复制相似问题