Search code examples
cassandratitanbulbs

Bulbs : Creating "in" and "out" edges between two vertices in TitanDB


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.

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

From the examples I understand how to create edges between the vertices as below

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

But assume that I don't have the reference to the vertices in my program.

I want to RETRIEVE vertex1 using its key "dpid" AND I want to RETRIEVE vertex2 using its key "desc"

AND then using the retrieved values I want to create the edge.How do I do this?


Solution

  • To retrieve a vertex by an indexed property (as opposed to its database ID), you can use one the Bulbs built-in index methods:

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

    To create an edge, simply do...

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

    NOTE: You don't need to label the edge "out" ("out" is implied by the direction of the edge going out from vertex1 and in to vertex2). You should consider using a more descriptive label.

    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