What is Neo4jConnect?

Neo4jConnect is a .NET Client for Neo4j through the REST service.

Neo4j is an open-source graph database implemented in Java. Developers describe Neo4j as
"embedded, disk-based, fully transactional Java persistence engine that stores data structured in graphs rather than in tables"

More information about Neo4j can be found on the homepage

Running Unit Tests

In order to run the unit tests succesfully you need to copy the file neo4j-server-examples-1.4 that reside in the folder examples\java\server\lib of your Neo4j installation folder to the plugins folder.

Note that the unit tests will delete all nodes and edges that are currently in the database!!!

Quickstart

Analog to the Matrix example that is shown here, we will guide you through the example using Neo4jConnect.


Download

Download the latest version of Neo4j from here.
This library has been tested with version 1.4 (2011-07-11) Community Edition.

Install

Extract the zip file and launch the Neo4j.bat file that reside in the bin folder.
When the console has been launched you should be able to surf to the following url:

http://localhost:7474/webadmin

Neo4j.jpg

The Matrix

First we will need to define the type of relationships we have inside our social network defined by an enum.

public enum MyRelationshipTypes
{
   Root,
   Knows,
   CodedBy
}

Next we denote where our Neo4j server reside

Uri neo4jUri = new Uri("http://localhost:7474/db/data/");

Now we create our network by creating nodes and edges according to the dependencies and properties in the Matrix example (see picture above).

var graphDb = new GraphDatabaseService(neo4jUri);

Node thomas = graphDb.CreateNode();
thomas.SetProperty("name", "Thomas Anderson");
thomas.SetProperty("age", "29");
thomas.Save();

Node trinity = graphDb.CreateNode();
trinity.SetProperty("name", "Trinity");
trinity.Save();

Node morpheus = graphDb.CreateNode();
morpheus.SetProperty("name", "Morpheus");
morpheus.SetProperty("rank", "Captain");
morpheus.SetProperty("occupation", "Total badass");
morpheus.Save();

Node cypher = graphDb.CreateNode();
cypher.SetProperty("name", "Cypher");
cypher.SetProperty("last name", "Reagan");
cypher.Save();

Node smith = graphDb.CreateNode();
smith.SetProperty("name", "Agent Smith");
smith.SetProperty("version", "1.0b");
smith.SetProperty("language", "C++");
smith.Save();

Node architect = graphDb.CreateNode();
architect.SetProperty("name", "The Architect");
architect.Save();

graphDb.GetRootNode().CreateRelationshipTo(thomas, MyRelationshipTypes.Root);

Relationship rel = null;

rel = thomas.CreateRelationshipTo(trinity, MyRelationshipTypes.Knows);
rel.SetProperty("age", "3 days");

rel = thomas.CreateRelationshipTo(morpheus, MyRelationshipTypes.Knows);

rel = morpheus.CreateRelationshipTo(trinity, MyRelationshipTypes.Knows);
rel.SetProperty("age", "12 years");

rel = morpheus.CreateRelationshipTo(cypher, MyRelationshipTypes.Knows);
rel.SetProperty("disclosure", "public");

rel = cypher.CreateRelationshipTo(smith, MyRelationshipTypes.Knows);
rel.SetProperty("disclosure", "secret");
rel.SetProperty("age", "6 months");

rel = smith.CreateRelationshipTo(architect, MyRelationshipTypes.CodedBy);

Now we want to know the friends of one person. And the friends of the friends too.
All the friends of thomas can de traversed by the following code


   var builder = new TraversalBuilder()
    .WithMaxDepth(1000)
    .WithOrder(TraversalOrder.BreadthFirst)
    .WithReturn(BuiltInReturnableEvaluatorType.AllButStartNode)
    .WithRelationship(MyRelationshipTypes.Knows, RelationshipDirection.Out);

var nodes = thomas.Traverse(builder);
var friendNames = nodes.Select(x => x.GetProperty<string>("name")).ToList();

Last edited Aug 8, 2011 at 9:57 PM by delarou, version 9