Simple Graph Database using Neo4j

Lately, the NoSQL and Graph Database is so happening. Therefore I would also like to learn how it works, performs, and fits to our problem. I searched several graph database project (here) and finally I choose Neo4j. When I tried to search for the quick and nice example database using Neo4j, I couldn’t find it. So I decided to make it by my own and to publish it here.

Neo4jFirst, lets start by installing the Neo4j.

Download the latest release (community version for free)
For Linux User, you can do these steps:
1.Extract and Install
›sudo ./bin/neo4j install
2.Start the server
›sudo service neo4j-service start
3.Check the server status
›sudo service neo4j-service status
And, for Windows User you can follow my steps here:
1.Extract and Install
-Run Command Prompt as Administrator
-Navigate to Neo4j folder
-Execute bin\Neo4j.bat install
2.Start the server
bin\Neo4j.bat start
3.Check the server status
bin\Neo4j.bat status
Please note that we must have installed the Java Runtime Environtment on our Computer. Otherwise an error message will come up.

Next, lets try opening the Webadmin page (http://localhost:7474/webadmin/) and see the dashboard panel.

Neo4j Webadmin
Now, lets design our first graph database. I named my database as MovieDB as it contains list of movies, stars and director.
›Nodes will be representing
a.Movie: name (string), genre (string), year (integer).
b.Stars: name (string).
c.Director: name (string).
›Edge will show relationship between
a.Stars – Movie (stars_in)
b.Movie – Director (directed_by)
For faster data insertion, I chose to create a graphML using a simple text editor. If you are also new to graphML, refer to this site to get it clearly. Here are the example of node declaration in my graphML:
Movie Node:
<node id="0">
 <data key="genre">Thriller</data>
 <data key="year">2012</data>
 <data key="type">movie</data>

Stars Node:

<node id="5">
 <data key="name">Nolan Gould</data>
 <data key="type">stars</data>

Director Node:

<node id="21">
 <data key="name">Gregory Wilson</data>
 <data key="type">director</data>
And here are the edge declaration example:
Stars-Movie Relationship (stars_in):
<edge id="0" source="5" target="0" label="stars_in">
 <data key="weight">1</data>

Movie-Director Relationship (directed_by):

<edge id="16" source="0" target="21" label="directed_by">
 <data key="weight">1</data>

Just in case you need my full graphML file, you can download it here.

Then, I imported my graphML file using Gremlin (Webadmin->Console->Gremlin) using this function: g.loadGraphML(‘graphML_filepath’).
 Done, after that we can try to browse all of our nodes using Cypher Query Language in the Webadmin->Data browser.
START root=node(*)
Cypher is the most suitable choice for those who are looking for SQL-like query language, as it consist of several clauses, such as
a.START: The graph starting points.
b.MATCH: The graph matching pattern.
c.WHERE: Filtering criteria, same like SQL.
d.RETURN: Same like Select clause in SQL.
Here are the sample of my MovieDB.
Now lets try to do a simple Cypher Query:
1. Select Movie nodes:
START n=node(*) WHERE n.type=‘movie’ RETURN n;
2. Select Movie node which name is “Ghoul”:
START n=node(*) WHERE‘Ghoul’ RETURN n;
3. Select Movie nodes which genre are “Action”:
START n=node(*) WHERE n.type=‘movie’ AND n.genre=‘Action’ RETURN n;
Cypher can also do the counting aggregation, here is the example:
1. Count how many movies in the database:
START n=node(*) WHERE n.type=‘movie’ RETURN count(*);
2. Count how many movies, stars and director in the database:
START n=node(*) RETURN n.type, count(*);

Right now I’ve just started learning graph database, so this time i can only show you the RDBMS like query. Next, I will show you the power of Graph database compared to traditional RDBMS.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s