cassandratitanbulbs

Bulbs: g.vertices.index.lookup(dpi=dpid_str) not working as expected


I am using TitanGraphDB + Cassandra.I am starting Titan as follows

cd titan-cassandra-0.3.1
bin/titan.sh config/titan-server-rexster.xml config/titan-server-cassandra.properties

I have a Rexster shell that I can use to communicate to Titan+Cassandra above.

cd rexster-console-2.3.0
bin/rexster-console.sh

I want to program the Titan Graph DB from my python program.I am using bulbs package for that.

I create 3 types of vertices from python using bulbs as given below. The 3 types of vertices are

- 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'})

If I try to print out the variables vswitch,vport and vdevice I get the following results.

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

However If I try to retrieve the above vertices using a key as follows.

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

And try to print-out vswitch and vport variables I get the following values

<generator object <genexpr> at 0x26d6370>)
<generator object <genexpr> at 0x26d63c0>

Am I doing something WRONG in trying to retrieve vertices as above using g.vertices.index.lookup(dpid=dpid_str)


Solution

  • The Bulbs g.vertices.index.lookup() method returns a Python generator (which is a type of iterator).

    Use next() to get the next value in the generator:

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

    Or you can use list() to turn the generator into a 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)
    

    However, if the indexed item is unique, you can use the get_unique() method to return one value or None:

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

    See...

    Rexter index documentation:

    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

    NOTE: Iterators and generators are Python programming basics -- they're used everywhere and are not specific to Bulbs -- if you're new to Python programming, see my answer to How Can I Learn to Program in Python? for a list of good online resources for learning to program in Python.