The intention of this post is to drive you through my small experience with this graphs database called Neo4j and .Net.

What is a Graph database?

This is a Wikipedia definition:

In computing, a graph database is a database that uses graph structures with nodes, edges, and properties to represent and store data (...).

What is Neo4j?

logo

An open source graph database that supports ACID and comes with a web-based administration tool that includes full transaction support and visual node-link graph explorer. Neo4j is accessible from most programming languages using its built-in REST web API interface.

I would like to start telling you that one of the best ways to learn about this database is reading its extent documentation here: http://www.neo4j.org/learn

What is my goal?

I will talk to you about my very first glance at this graph database using .Net!

My goal is to create a graph database that represents each Roman Emperor and his or her successor, like this:

example

Installing the Neo4j server

First things first: we must install the Neo4j database server, we can download it from http://www.neo4j.org/download

Once you installed the server, open the Neo4j Community application. That will prompt you to start the server:

First window

I used the Database Location that comes by default with Neo4j. Even though it seems like this location "C:\Users\ricardo\Documents\Neo4j\default.graphdb" is a file, it is actually a folder that contains many file that represent the database. However, it will initially be completely empty until you start storing things.
Just to give you an example, I could have created another empty folder like "C:\Users\ricardo\Documents\Neo4j\new.graphdb" and that will work too.

Ok, to warm up the engines, let's click on "Start"

Wait window

and when the server finally started, you should see something like this:
Started window

Done! Our Neo4j database server is up and running!
You might have noticed the message "Browse to http://localhost:7474". This URL will take your to the Neo4j Web Administration Tool. You can just click on that hyper link and that will take you to a website hosted on a Jetty web server in your machine.

That website should look something like this:

Website 1

I am going to come back to the Neo4j Administration Tool when we explore the graphs.

Using .Net

In order to use Neo4j from my .Net application, I need to use a Neo4j client for .Net. I found this one (https://github.com/Readify/Neo4jClient) very useful for the purposes of my little project. This client was developed my an Australian consultancy company called Readify (http://readify.net/).
By the way, I found the information of this client in the website of Neo4j (http://www.neo4j.org/develop/dotnet)

Don't you like .Net? No worries, there are other available clients for languages such as PHP, Java, Javascript, etc.: http://www.neo4j.org/develop

Now, you can create a .Net application in your favourite language, I created a console application in C# and then included this .Net client for Neo4j by using the Visual Studio Nuget packages manager and searching for the "Neo4jclient":
NugetP1

That's it! Now we can start to put our hands in code :)

Remember the goal? Let's create a graph of precendence for the Roman Emperors. In order to achieve this, we need to create two things:

1. Emperor class (Node)

public class Emperor
{
    public string Name { get; set; }
}

2. Precedes class (Relationship)

public class Precedes : Relationship, IRelationshipAllowingSourceNode<Emperor>,
    IRelationshipAllowingTargetNode<Emperor>
{
    public static readonly string TypeKey = "PRECEDES";

    public Precedes(NodeReference targetNode)
        : base(targetNode)
    {}

    public override string RelationshipTypeKey
    {
        get { return TypeKey; }
    }
}

This class must:

  • inherit from Relationship.
  • implement IRelationshipAllowingSourceNode<Emperor> to tell Neo4j the source of the relationship is an emperor.
  • implement IRelationshipAllowingTargetNode<Emperor> to tell Neo4j the target of the relationship is an emperor.
  • RelationshipTypeKey represents the text of the relationship.

3. Connect to the database server

Remember the URL to connect to the server? This one: http://localhost:7474
The "connection string" for that server is the URL of the server plus "/db/data"

var serverUrl = "http://localhost:7474/db/data";
// Instantiates the client
var client = new GraphClient(new Uri(serverUrl));
// Connects to the database
client.Connect();

'client' represents the object that we will use to interact with Neo4j

4. Create graph

// Creates an emperor.
var julius = new Emperor() { Name = "Julius Caesar" };
// Creates another emperor.
var augustus = new Emperor() { Name = "Augustus" };
// Creates a node that contains the first emperor.
var node1 = client.Create(julius);
// Creates a node that contains the second emperor.
var node2 = client.Create(augustus);
// Creates a graph with the two nodes and the relationstip
client.CreateRelationship(node1, new Precedes(node2));

That's it, this little graph has been created!

5. Browse graph

Open your favourite browse and navigate to http://localhost:7474/browser/

browsing

First, click on the three green balls of the left.
Second, click on the star symbol
Third, see the generated graph!

Check out the right-most red square I marked in the screeshot. That is the CYPHER query generated by the tool to retrieve the first 25 nodes:
'MATCH n RETURN n LIMIT 25'
I guess the Cypher language requires an entire post for itselft.

About Author

Ricardo Sebastian Youssef

Ricardo Sebastian Youssef