Tuesday 22 March 2016

Tutorial 2: AddV() for adding a vertex to graph

Titan Graph DB Tutorial: Getting Started

Gremlin is awesome

Now that you installed Titan and tried few things like V() and E(), let’s learn how to do more interesting things with Gremlin.

But before that, let’s ask ourselves what is Gremlin?

Gremlin is a query language that we use to query and mutate a graph that supports Tinkerpop3. So, you can use Gremlin to add vertex or edge, update one edge or vertex or update many of them. You could also query the graph for data or for relationships. Let me show you how.

The Gremlin console is very friendly, try this:

1+1

See? It’s cool. It can do a lot. So now let’s

Open empty graph

You could always start an empty in-memory graph using Gremlin console.

In-memory graph means when you close the program, everything will be gone so remember that very well.

graph = TinkerGraph.open()
g=graph.traversal()

I want to add a vertex

g.addV('person')

You have just created a vertex and gave it a label person.

The label of the vertex helps distinguish between different types of vertexes in your graph DB. So if you have persons, places and pages. You could assign the appropriate label for each one to distinguish between them later with your database is big.

OR

v=g.addV('person')

You assigned the new created vertex into an object called v. Now you could do whatever you want with it.

OR

g.addV('person').property("name", "Mo")

You created a vertex and added a property to it. The property has two main elements, key and value. So this person is called Mo. Great!

Each vertex can have an unlimited number of key/value pairs. You could add as many as you want.

Interested more in properties? Go to next class here.

Please don’t forget to subscribe to my blog to get all updated straight to your inbox for FREE.

Wednesday 27 January 2016

# Titan Graph DB Tutorial 1: Getting started

Untitled Document.md

Titan DB Tutorial (1): Getting started

Alt text

Download and Run Titan 1.0.0

So you must porbably have been fascinated about graph databases and would like to give Titan DB a try.
In this tutorial, I will show you how to download and run Titan 1.0.0 version on Ubuntu and Windows. Then we will learn how to use it in next lessons.

Let’s get started

Download it:
Click to download Titan 1.0.0 with Hadoop 1 – recommended. Alternatively, you can go to download page here and pick the Titan 1.0.0 with Hadoop 1.
P.S: If you wonder why we choose Hadoop 1 over Hadoop 2, it seems like Titan 1.0.0 that uses Tinkerpop has some issues with it at the current version. So once it solves the problem, Titan 1.+ will use Hadoop 2. In short, use Hadoop 1 because it’s more stable.
Great, now you have Titan DB downloaded. So, let’s unzip this ZIP file. You can use WinRAR on Windows or unzip command on Linux.
Run it on Linux/Mac:
In Linux or Mac, it will be straight forward, just cd to the root directory of Titan which is titan-1.0.0-hadoop1. Then run this:
bin/gremlin.sh 
You may skip to Hello World: Example section :).
Run it on Windows:
Well, there is a bug that needs to be fixed on bin/gremlin.bat file. There is a a solution in this google group for this issue but I summarized it for you here. Open bin/gremlin.bat using a text editor of your choise then:
Change:
set LIBDIR=..\lib
To:
set LIBDIR=lib
Change:
if "%CP%" == "" (
set CP=%LIBDIR%\%1
)else (
set CP=%CP%;%LIBDIR%\%1
)
To:
if "%CP%" == "" (
set CP=%1
)else (
set CP=%CP%;%1
)
To add command history abilities to the gremlin command line in the gremlin.bat file add to the set JAVA_OPTIONS line (solution from same source):
set JAVA_OPTIONS=-Xms32m -Xmx512m -javaagent:%LIBDIR%\jamm-0.3.0.jar
add:
set JAVA_OPTIONS=-Xms32m -Xmx512m -javaagent:%LIBDIR%\jamm-0.3.0.jar -Djline.terminal=none
Once you are done, you can run it using:
bin/gremlin.bat

Hello World: Example

So now you have the gremlin console running giving you a message like this
moha@me:~/dev/titan-1.0.0-hadoop1$ bin/gremlin.sh

         \,,,/
         (o o)
-----oOOo-(3)-oOOo-----
plugin activated: aurelius.titan
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/moha/dev/titan-1.0.0-hadoop1/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/moha/dev/titan-1.0.0-hadoop1/lib/logback-classic-1.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
17:39:17 INFO  org.apache.tinkerpop.gremlin.hadoop.structure.HadoopGraph  - HADOOP_GREMLIN_LIBS is set to: /home/moha/dev/titan-1.0.0-hadoop1/lib
plugin activated: tinkerpop.hadoop
plugin activated: tinkerpop.tinkergraph
gremlin> 

Gremlin console is very cool, it will allow you to conenct to a Titan graph and play with it. But what is Gremlin console anyway? Well, it’s this tool that will give you all the powers to manipulate a Titan graph, load a local graph or even connect to a remote graph. It’s amazing. I will teach you more things about it as you go deeper in next tutorials.
So, let’s try to play a bit with gremlin console
gremlin> 1+1
==>2
gremlin> 1+9
==>10
gremlin> 
Yaaaay, it’s a calculator too? Hahaha, no don’t worry. We will do more sophisticated things with it.
Now, let’s load a graph. YES SIR !
gremlin> graph=TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> graph
==>tinkergraph[vertices:6 edges:6]
gremlin>
So what did we do? We loaded a graph called Tinkerpop Graph and assigned it to a variable called graph that we could retreive later too. You see? Gremlin console is cool.
So what is Tinkerpop Graph? It’s a pre-built simple graph to test the capabilities of this console and do some exercises on several Gremlin queries.
Here is a visual view of it: Alt text
God, what is Gremlin? Gremlin is the name of the query language (or API) that you will use to manipulate the graph. i.e., open and load a graph, create, update and delete edges and vertexes. And also traverse the graph. The langauge is very easy so don’t be scared. It will be straight forward.
Alt text
Gremlin is soo coool
Let’s see all vertices in the graph, run this
gremlin> g=graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> 
This creates a new variable called g which is the traversal of the graph. If we want to play with the graph, we should use g instead of calling graph.traversal() every time.
Now let’s see all vertices in the graph

V() for vertices

gremlin> g.V()
==>v[1]
==>v[2]
==>v[3]
==>v[4]
==>v[5]
==>v[6]
so v[1] means there is a vertex with ID 1. If you want to load a vertex or vertices by ID, all you need to run is this:
g.V(1)
g.V(1,3)
g.V(1,3,6)
Get it? Great.
How about edges?

E() for edges

gremlin> g.E()
==>e[7][1-knows->2]
==>e[8][1-knows->4]
==>e[9][1-created->3]
==>e[10][4-created->5]
==>e[11][4-created->3]
==>e[12][6-created->3]
gremlin> 
so e[12] means there is an edge with ID 12 connecting vertex 6 with vertex 3. And created is the label of this edge. Makes sense?
As an exercise for today, I want you to load edge by ID, could you do it? Multiple edges by ID?
Alright, that was enough for today. I will continue with more tutorials if I see some engagements here. Would you please help me fill this survey? Please leave any comments or questions you have below.