Thursday, August 27, 2015

Simple gremlin queries in Titan...

Titan is an open source graph database and, even though it isn't as easy to setup for use as Neo4J, it is easy enough to start using in just a few minutes. Here is an example of using Titan with HBase as the backend storage.


I'm using HBase 0.98.13. I downloaded hbase, untarred the files, changed to the hbase directory and ran "bin/".

I cloned Titan from the github repository and built it using the maven package command.

git clone
cd titan
mvn clean package

I started gremlin using "bin/".

I followed the Titan Hbase instructions for initializing Titan for use with HBase within the gremlin console. You can also create a properties file that contains the same settings and load the settings from the gremlin console.


conf = new BaseConfiguration();
conf.setProperty("storage.hbase.table", "test") 

g =;

Now let's add some vertices.

alice = g.addVertexWithLabel('human')
alice.setProperty('name', 'alice')
bob = g.addVertexWithLabel('human')
bob.setProperty('name', 'bob')
clark = g.addVertexWithLabel('human')
clark.setProperty('name', 'clark')
darwin = g.addVertexWithLabel('human')
darwin.setProperty('name', 'darwin')
ernie = g.addVertexWithLabel('android')
ernie.setProperty('name', 'ernie')

Let's list the vertices and their properties.

==>{name=alice, age=25}
==>{name=darwin, age=206}
==>{name=clark, age=93}
==>{name=bob, age=21}

And now let's make these humans be friends with each other.

alice.addEdge('friend', bob)
alice.addEdge('friend', darwin)
bob.addEdge('friend', alice)
bob.addEdge('friend', darwin)
clark.addEdge('friend', darwin)
darwin.addEdge('friend', bob)
darwin.addEdge('friend', clark)

Now let's remove ernie from the graph.

g.V.has('name', 'ernie').remove()

Now we can see that ernie is gone

g.V.has('name', 'ernie').map()

(no results displayed, just the gremlin prompt)

Let's add ernie back, but this time he's a human.

ernie = g.addVertexWithLabel('human')
ernie.setProperty('name', 'ernie')

Let's try finding out who has friends


Wait - what happened? We see an entry for every friend edge, which is exactly what our gremlin query was asking for, but that doesn't look very nice.

Let's try the dedup method.


Ahh! That's more like it! But how else can we get that list?


Nice! We have two ways to get a distinct list.

No comments:

Post a Comment