Neo4j is the leading open source graph database project. Graph databases are a type of NoSQL database, and they excel in storing information about relationships between entities. The graph databases will store nodes (entities), which can also have properties. Edges (relationships) connect nodes, and can also have properties to describe the type of relationship.
Installation
Neo4j can run on Windows, the free Community edition is available at Download Neo4j (You need the Java JRE installed as well before running).
Extract the downloaded ZIP, and run the bin/Neo4j.bat file to start the database service.
The service runs on port 7474 by default. Navigating to http:\\localhost:7474/webadmin/ will open the Web Admin tool, which includes a data browser and a query console. The console can query the database using the Cypher query language.
For my experimentation, I downloaded a C# client, Neo4jClient using NuGet.
Documentation:
An online Neo4j Manual is available, it can also be downloaded as a PDF, which requires filling out a short form with contact information the site.
There is also a Free Graph Databases e-book from O’Reilly available to download. It also requires some contact information as well.
Code:
Here is a short program to create nodes and edges, before dropping them.
using System; using Neo4jClient; // https://bitbucket.org/Readify/neo4jclient/wiki/Home for code samples and more info on Neo4jClient namespace Neo4jDemo { public class Program { // Define our entity public class FootballEntity { public string FirstName { get; set; } public string LastName { get; set; } public string Position { get; set; } } // Define relationships public class Coaches : Relationship, IRelationshipAllowingSourceNode, IRelationshipAllowingTargetNode { public Coaches(NodeReference targetNode) : base(targetNode){} public const string TypeKey = "Coaches"; public override string RelationshipTypeKey { get { return TypeKey; } } } public class Teammates : Relationship, IRelationshipAllowingSourceNode, IRelationshipAllowingTargetNode { public Teammates(NodeReference targetNode) : base(targetNode){} public const string TypeKey = "Plays with"; public override string RelationshipTypeKey { get { return TypeKey; } } } static void Main(string[] args) { // Connect var client = new GraphClient(new Uri("http://localhost:7474/db/data")); client.Connect(); // Add nodes var coachReference = client.Create(new FootballEntity { FirstName = "Mike", LastName = "Smith", Position = "Head Coach" }); var coachId = coachReference.Id; var qbReference = client.Create(new FootballEntity { FirstName = "Matt", LastName = "Ryan", Position = "QB" }); var qbId = qbReference.Id; var wrReference = client.Create(new FootballEntity { FirstName = "Roddy", LastName = "White", Position = "WR" }); var wrId = wrReference.Id; var rbReference = client.Create(new FootballEntity { FirstName = "Steven", LastName = "Jackson", Position = "RB" }); var rbId = rbReference.Id; // Display Node IDs Console.WriteLine("Coach ID = " + coachId.ToString()); Console.WriteLine("QB ID = " + qbId.ToString()); Console.WriteLine("WR ID = " + wrId.ToString()); Console.WriteLine("RB ID = " + rbId.ToString()); // Retrieve a node var retrievedNode = client.Get(coachReference); // Create relationships var relationship = client.CreateRelationship(coachReference, new Coaches(qbReference)); relationship = client.CreateRelationship(coachReference, new Coaches(wrReference)); relationship = client.CreateRelationship(coachReference, new Coaches(rbReference)); relationship = client.CreateRelationship(qbReference, new Teammates(wrReference)); relationship = client.CreateRelationship(qbReference, new Teammates(rbReference)); relationship = client.CreateRelationship(rbReference, new Teammates(wrReference)); // Pause to examine values Console.WriteLine("Pause"); Console.Read(); // Cleanup client.Delete(coachReference, DeleteMode.NodeAndRelationships); client.Delete(qbReference, DeleteMode.NodeAndRelationships); client.Delete(wrReference, DeleteMode.NodeAndRelationships); client.Delete(rbReference, DeleteMode.NodeAndRelationships); // End Console.WriteLine("Completed"); Console.Read(); } } }
Thanks Rob for your article.
So what is your opinion of Neo4j and what do you think would you use it for?
It would be great if you could highlight your code, makes it much easier to read. Perhaps embed a github gist for that (or something similar).
Thanks for the reply.
I think very highly of Neo4j, this is a very easy product to use, I’m certainly going to dig deeper into it. The Graph Databases fill what I think is a big gap in relational databases (SQL Server in particular) with storing complex relationships and hierarchies. In particular, I’ve struggled in the past with storing family relationships between people, Neo4j would be a great way to accomplish that.
Thanks for the code suggestion as well.