-
Notifications
You must be signed in to change notification settings - Fork 11
Quick start
Graph databases are a new and exciting NoSQL database technology. They simplify the modeling of many complex problems and enable easier and more intuitive querying of a vast number of different kinds of datasets. For those working on real-world networked applications Titan makes possible the creation of highly-scaleable and highly-available graph backed applications.
Thunderdome exists to provide a graph database interface in Python for a Titan/Rexster server setup. It aims to be both easy to use and highly customizable.
Thunderdome is available on PyPI and can be installed via pip with the following command:
$ pip install thunderdome
Let's create a simple social network example in the python console to showcase how thunderdome works. Firstly, let's create a new connection to our graph:
>>> import thunderdome
>>> from thunderdome.connection import setup
>>> setup(['localhost'], 'thunderdome')
Now, before we get started networking people together, let's create a very simple vertex type so we can see how it's done:
>>> class TestVertex(thunderdome.Vertex):
... pass
...
>>> t = TestVertex.create()
>>> t.vid
'339b5b54-3423-4a54-9464-2dc7fa8a3741'
>>> t.eid
4
The vid is a special UUID generated by thunderdome that is used to uniquely identify all vertices in the graph. The eid on the other hand is an identifier generated by Titan and not guaranteed to be unique across graph databases. Now that you've seen the basics let's create a more interesting vertex and some instances of it:
>>> class Person(thunderdome.Vertex):
... name = thunderdome.Text()
... age = thunderdome.Integer()
... def __repr__(self):
... return "<Person:name='{}'>".format(self.name)
...
>>> eric = Person.create(name='Eric', age=25)
>>> jon = Person.create(name='Jon', age=105)
>>> eric.name
u'Eric'
>>> jon.age
105L
Now let's create an edge between Jon and Eric to immortalize their friendship in the graph:
>>> class FriendOf(thunderdome.Edge):
... pass
...
>>> jon_friends_eric = FriendOf.create(jon, eric)
>>> jon_friends_eric.inV()
<Person:name='Eric'>
>>> jon_friends_eric.outV()
<Person:name='Jon'>
Here we've created an edge type called FriendOf and then created an instance of this edge coming out from vertex Jon and going into vertex Eric. The inV method when called on an edge gives us the vertex the edge goes into. Similarly, the outV method gives us the vertex it comes out of. With this we can now perform some simple graph traversals:
>>> jon.outE()
[<__main__.FriendOf object at 0x2754710>]
>>> jon_friends_eric == jon.outE()[0]
True
On the first line we get all the edges coming out of Jon's vertex. We see that there is a FriendOf edge and sure enough it is identical to the FriendOf edge we just created. Now let's ask some more interesting graph questions:
>>> jon.outV(FriendOf)
[<Person:name='Eric'>]
>>> eric.inV(FriendOf)
[<Person:name='Jon'>]
We can see that the outgoing edges (people Jon has friend) returns a list containing Eric. Traversing the other way we see that Jon has indeed connected with Eric via a FriendOf edge.