December 21, 2012

Getting Started with Titan Graph Database

I have used Neo4J as my first graph database. And it had web console which makes more easier to handle. But this time I'm not going to tell you how to getting started with Neo4J. This time we are going to work with Titan which is an open source project which is held by Aurelius.

This database is more scalable graph database. Lets get to know how to get started with Titan Graph Database.

First download Titan from Titan Site
Click Here to download Titan Graph Database

Then extract titan to a disk. In this example I take D drive. If you are Linux user extract this Titan file to somewhere you can execute.

Then take a shell in Linux or command prompt in Windows. Then move to titan folder. Then move to bin.

Before take Gremlin Console you need to install Java.

After that if you are Linux guy run this command.
 bash gremlin.sh
Or if you are windows guy you command is,
 gremlin.bat

Now you are having gremlin. Now we are going to connect to graph database. Basically Titan we can use with three main storage backends. They are
  • Cassandra
  • HBase
  • Oracle Berkley 
But I'm not going to use any of them now. Now I'm just going to use Titans Native Storage.
 g = TitanFactory.open('local/tmp');
 Below command will initiate a graph and assign it to g variable. Now in you bin you can see local and temp folders are created.

If you hope to search through your graph in Titan you have to index it first. Otherwise you can't search it. Actually it's a bug like thing in titan, and it's a know issue. Let say we are going to search with "name" property. Then we have to add "name" index first.

g.createKeyIndex('name', Vertex.class);

Now you are ready to add nodes and edges. And you have a index on "name" property.

v = g.addVertex(null);
This will create a new node on graph. we are going to set properties to this node.
v.setProperty('name','dedunu');
v.setProperty('type','person');
v.setProperty('age',20);
 v1 = g.addVertex(null);
v1.setProperty('name','malinda');

v1.setProperty('type','person');

v1.setProperty('age',22);
 v2 = g.addVertex(null);

v2.setProperty('name','UCSC');
v2.setProperty('type','institute');

Now we have three nodes. Two of them are Persons and the other one is a institute.  Now we are going to create relationships between those three nodes.

e1 = g.addEdge(null, v, v2, 'study in');
e2 = g.addEdge(null, v1, v2, 'study in');
e3 = g.addEdge(null, v, v1, 'knows');
e4 = g.addEdge(null, v1, v, 'knows');

Now you have a graph like this.


 Now we are going to have a journey around our beautiful data!!! And now I want to know where malinda "study in". For that I have to load Malinda to a vertex variable. I'll use existing one.

v1.out('study in').map();

Now I want who study in UCSC.  For that I have to load UCSC to a variable first. I'll use existing one.

v2.in('study in').map();

If I only need names of them then below one is the command.

v2.in('study in').name;
Finally to commit all those thing to disk you have to shutdown the graph for that run below command.

g.shutdown();

Be careful those commands are case sensitive!!! Enjoy Graphs!!!

19 comments:

  1. This is great! thanks! Greetings from Denver

    ReplyDelete
  2. Nice, thanks. I need a quick start.

    ReplyDelete
  3. Can you tell us how to install "Titan Server" on windows 7/8/8.1

    ReplyDelete
    Replies
    1. Actually its rexter service. I'll post a blog on that as well

      Delete
  4. Please implement a graph database (a kind of NoSQL). This graph database
    should consist of nodes (with have properties) for entities and edges (which
    have single or multiple properties and can be directional or bidirectional) for
    relationships, and support node indexing and query. The query language has
    following keywords: START, MATCH, WHERE, RETURN, ORDER BY,
    AGGREGATE, SKIP, and LIMIT.

    Example Input:
    Node martin = graphDB.createNode();
    martin.setProperty(“name”, “Martin”);
    Node pramod = graphDB.createNode();
    pramod.setProperty(“name”, “Pramod”);
    Node barbara = graphDB.createNode();
    pramod.setProperty(“name”, “Barbara”);
    martin.createRelationshipTo(pramod, FRIEND, since = 1998);
    pramod.createRelationshipTo(martin, FRIEND, since = 1998);
    martin.createRelationshipTo(barbara, FRIEND);
    barbara.createRelationshipTo(martin, FRIEND);
    START node = Node:nodeIndex(name = “Barbara”)
    MATCH path = node-[relation*1..3]->related_node
    WHERE type(relation) = ‘FRIEND’
    RETURN related_node.name, length(path), relation.since;

    ReplyDelete
  5. Hi,

    Am new to titan-cassandra, this blog has given me a nice information about how to use titan.But where and how can i see the all vertices and edges created.Does titan will have the GUI for it?.If it has can you please provide the info about it or related docs of it.


    Thanks,
    Narsing

    ReplyDelete
    Replies
    1. As I know there is no GUI component for that. You can query and get results as I have mentioned above.

      Delete
  6. Hi... This was a great tutorial and really helped me get started... this maybe a silly question but after I have closed it down and then want to open it again to do further editing the commands above are not loading, could you give some advice on this, much appreciated

    ReplyDelete
  7. HI Dedunu,

    Thanks for your quick & cream document. I would appreciate if you can give info on how to visualize the above created graph??? any possible way??

    Regards,
    Deval

    T

    ReplyDelete
    Replies
    1. You can try d3js. It is a good way to visualise graph data. But you may need to write the code to import nodes and traverse. D3js would take care of visualisations. I worked on a big data project there we used d3js for graph and various other kinds of visualisations.

      Delete
    2. Thanks Dedunu. I have worked little on D3js. however as it requires json fomrat then it renders the nodes and links. So, here the graph needs to be saved in json format to process in d3js ? or any other way ?

      Regards,
      Deval

      Delete
  8. Hi,

    Could you please provide us the comparison between normal Sql DB and Titan DB? e.g : db/table name, attributes, records compared with vertex, edges, property key.,


    Regards,
    Heman

    ReplyDelete
    Replies
    1. I would try to elaborate if further. But I think we can compare graph databases with relational databases. like MongoDB vs SQL server. But I would try to do that. Thank you for the idea.

      Delete
  9. Hi DD, Excellent blog! however, I am Naive in Graph Db and experiencing problem in getting started. I have few queries:

    1. Which Titan version did you use ( from Download's Section) for your demonstrated example?
    2. I need to Use Titan with Hbase back end. Which version shall I be using Titan+Hbase one or any normal Titan version or Titan with Hadoop 2 would be enough?
    3. I installed Titan-Hbase version and upon opening gremlin.bat from console, I got this error. Please help.

    java.utli.prefs,WindowsPreferences
    WARNING Could nor open/create prefs root node Software/JavaSoft/Prefs at root 0x80000002. Windows RegCreateKeyEx<...> returned error code 5.

    p.S.: I am not form programming background.

    I am sure, these questions would help beginners like me. Thank you very much in advance.

    ReplyDelete
    Replies
    1. I used very old version of titan for this example. I'll write a new blog post about latest version and HBase.

      Delete
    2. Thank you so much. Awaiting for it at earliest!!

      Delete