pythoncassandratitanbulbs

Bulbs: replacing create() with get_or_create()


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

I want to replace my create() call with get_or_create()

I saw the following example on the web.

 james = g.vertices.create(name="James")

written as shown below.

 james = g.vertices.get_or_create('name',"James",{'name':'james')

Now my vertex create function is as follows.

self.g.vertices.create({ 'desc':desc,
                         'port_id':port_id,
                         'state':state,
                         'port_state':port_state,
                         'number':number,
                         'type':'port'} )

If I want to rewrite the above function call (create()) which takes multiple key-value pairs using get_or_create()

I first need to create a key.Or does it check all the attributes by default.

I am a beginner in python and I don't realy get the significance of
get_or_create('name',"James",{'name':'james')

why are function attributes specified like this.?

The function definition for get_or_create() is here

Any help will be appreciated.


Solution

  • The Bulbs 'get_or_create()' method looks up a vertex in the index and creates it if it doesn't exist. You can supply get_or_create() a Python dict of database properties the same way you can with create().

    See...

    Here are a few examples...

    >>> # a vertex where name is "James" doesn't exist so lookup() returns None
    >>> g.vertices.index.lookup("name", "James")  
    None
    
    >>> # a vertex where name is "James" doesn't exist so a vertex is created
    >>> data = dict(name="James", city="Dallas")
    >>> james = g.vertices.get_or_create("name", "James", data)
    >>> james.data()
    {'city': 'Dallas', 'name': 'James'}
    
    >>> james.eid   # returns the element ID for the james vertex    
    >>> 1       
    
    >>> # a vertex where name is "James" DOES exist so vertex is returned unmodified 
    >>> data = dict(name="James", city="Dallas", age=35)
    >>> james = g.vertices.get_or_create("name", "James", data)
    >>> james.data()         # note age=35 was not added to the vertex properties
    {'city': 'Dallas', 'name': 'James'}     
    
    >>> # one way to update the vertex properities
    >>> james.age = 35   
    >>> james.save()
    
    >>> james.data()
    >>> {'city': 'Dallas', 'age': 35, 'name': 'James'}
    
    >>> # a way to update the vertex properties if you only have the vertex ID
    >>> # the vertex ID for the james vertex is 1
    >>> data = dict(name="James", city="Dallas", age=35)
    >>> g.vertices.update(1, data)           
    
    >>> james = g.vertices.get(1)
    >>> james.data()
    >>> {'city': 'Dallas', 'age': 35, 'name': 'James'}