Tuesday, October 1, 2013

Neo4j - first steps

Hello, guys!

Some days ago we posted a very basic introduction about Neo4j. Let's see something more practical today! Let's give our first steps with Neo4j.

First thing we have to do is installing our database. If you use Mac OS, you can easily install the newest Neo4j version with HomeBrew. You need to have JDK 7 in order to see things running fine. Open a terminal and type:

 brew update  
 brew install neo4j  
 neo4j start  

Done!! Wait some minutes for Neo4j installation and after typing "neo4j start" you will have it up and running.

You can do a similar process for Linux or Windows. Or you can download the tar.gz file and do a manually installation process. You can have more information about downloading installing Neo4j here.

After starting Neo4j, you will see a message like this:

Accessing the suggested URL (http://localhost:7474/webadmin/), you will notice a panel with a look and feel similar to this:

You can take a tour for the web interface or check some tutorials.

We will create a sample quickstart Neo4j project to be able to create a Hello World sample. The project is at Github and you can download it here. You will need Maven to run the project.

Neo4j has several ways to run. We have showed you the traditional way to run it. However, it can run into an embedded form into Java applications. We will use this form for our first example. It is pretty fast and simple to use, but be aware that it is not good to use Neo4j embedded in production environments!

Take a look at the project, into the package com.ourdailycodes.neo4j_quickstart.helloworld (if you did not cloned the project from Github, follow this link to observe the classes). You will notice two classes. Let's explain them quickly. The first one and more important is Example.java. It has a main method that starts Embedded Neo4j at the first method line:

 GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase("database");  

This will create an embedded database into a folder called "database".

On the sequence, we start a transaction and create the nodes and relationships. We create two nodes - Pokemon and Trainer, and create a relationship between them. the relationship is declared into the class RelationshipExample.java. There, we defined CAPTURES relationship. After creating the nodes, we set a relationship between them. Then, we "commit" the transaction and shutdown the database.

There are several tools where you can visualise your final graph. One of the is Neoclipse tool, a nice open source tool to have a graphic representation of your graphs. Other graphic tools for Neo4j listed on this Stackoverflow answer.

This is it! Today we have seen a short quickstart about Neo4j: how to install full version and how to start a simple Java project, start embedded version and create nodes and relationships. NOTE: the project on Github will have more codes on other packages; they will be used on further posts to show more Neo4j examples. The examples on other packages different from the one mentioned on the post may not be working perfectly yet. Please be patient and wait for the next posts =)

Thanks for reading!

No comments:

Post a Comment