Sony Arouje Blog

a programmer's log

Neo4jD–.NET client for Neo4j Graph DB

with 18 comments

Last couple of days I was working on a small light weight .NET client for Neo4j. The client framework is still in progress. This post gives some existing Api’s in Neo4jD to perform basic graph operations. In Neo4j two main entities are Nodes and Relationships. So my initial focus for the client library is to deal with Node and Relationship. The communication between client and Neo4j server is in REST Api’s and the response from the server is in json format.

Let’s go through some of the Neo4j REST Api’s and the equivalent api’s in Neo4jD, you can see more details of Neo4j RestAPi’s here.

The below table will show how to call Neo4j REST Api directly from an application and the right hand will show how to do the same operation using Neo4jD client.

Neo4j Api Equivalent Neo4jD Api
Create Node

POST http://localhost:7474/db/data/node
Accept: application/json
Content-Type: application/json
{"FirstName" : "Sony"}

Response (click here to see full response)

{
  "outgoing_relationships" : "
http://localhost:7474/db/data/node/……..,
  "data" : {
    "foo" : "bar"
  },…..

}

Node sony=new Node();
sony.AddProperty(“FirstName”,”Sony”);
sony.Create();

Get Node by ID
GET http://localhost:7474/db/data/node/3

Response (click here to see full response)

{
  "outgoing_relationships" :
http://localhost:7474/db/……….,
  "data" : {
    "FirstName" : "Sony"
  },
  "traverse" :
http://localhost:7474/db/data. . .{returnType},
..........
}

Node sony=Node.Get(1);
Assert.AreEqual(“Sony”, sony.GetProperty(“FirstName”);

The Neo4jD will create a Rest request to fetch the Node with Id 1 and parse the json response and set the properties and required field.

Create Relationship between two Nodes

POST http://localhost:7474/db/data/node/1/relationships
Accept: application/json
Content-Type: application/json
{"to" :
http://localhost:7474/db/data/node/2, "type" : "wife"}

Response

(click here to see full response)

Node sony=Node.Get(1); //Get an existing node
//Create a new node
Node viji=new Node();
viji.AddProperty(“FirstName”,”Viji”);
viji.Create();

RelationShip relation=sony.CreateRelationshipTo(viji, “wife”);

 

Client lib is still missing some basic features and I am working on it.

The next important part of the library is Graph traversing. Neo4j supports REST api, Cypher and Germlin for Graph traversing. I am currently writing a query creator for Germlin. So the initial version of Neo4jD will only support Germlin.

If any one wants to join the project feels free to send me a personal mail or add a comment. You all can have a look at the code in Git.

Neo4jD Part 2  Neo4jD Part 3  How to Persist Static typed Entities

 

Neo4jD Git Repository

About these ads

Written by Sony Arouje

February 3, 2012 at 2:28 pm

Posted in .NET

Tagged with , , ,

18 Responses

Subscribe to comments with RSS.

  1. Very cool Sony! Do you think it would be beneficial to contribute your thoughts to one of the existing .NET REST client efforts, http://docs.neo4j.org/chunked/snapshot/tutorials-rest.html#id389218 ?

  2. Hi Sony,

    Great to see more .NET adoption of neo4j.

    Have you seen the project we are already running at http://hg.readify.net/neo4jclient ? We’re on NuGet as “Neo4jClient”.

    Tatham Oddie

    February 4, 2012 at 5:28 pm

    • Yes Tatham I saw that project. It’s a well known .net client for ne04j. Its a great one.

      Sony Arouje

      February 4, 2012 at 7:51 pm

  3. […] Neo4jD–.NET client for Neo4j Graph DB […]

  4. Hi Sony!

    I’m a community curator at DZone.com and I really was impressed by your blog and the interesting .NET topics you write about. Over at our site, we have a .NET Zone, http://dzone.com/mz/dotnet

    I was going to ask you if you would be interested in giving me permission to republish one of your recent posts to benefit our audience and give some exposure to your content.

    Thanks!

    Mitch

    February 9, 2012 at 2:27 am

    • Hi Mitch,
      I am a regular reader of Dzone and its my pleasure to see my post in dzone. You can publish my post in dzone.

      Regards,
      Sony Arouje

      Sony Arouje

      February 9, 2012 at 9:10 am

  5. […] the previous post I explained how to create Node and Relationship using Neo4jD. As you might noticed that […]

  6. […] to persist data to Neo4j graph database. If you want to see how the core is working then visit here. You might notice that, the Node and Relationship is not static typed objects. Most of us work with […]

  7. hi. sorry my engllish is not very good. this kind of db was always in my dream. and i want to thank u for this very nice api(neo4jd).

    i have one question. here is my model :

    public abstract class Person

    {

    [EntityId]

    public int Id{get;set;}

    }

    public class Employee : Person

    {

    public string Name {get;set;}

    }

    public class Manager : Person

    {

    public string OfficeName{get;set;}

    }

    public class MyClass

    {

    [EntityId]

    public int Id{get;set;}

    public Person SelectedPerson{get;set;}

    }

    i’ve created map class for all.

    here is the map class of MyClass:

    public class MyClassMap : EntityConfiguration

    {

    public MyClassMap()

    {

    RelatedTo(p => p.SelectedPerson);

    }

    }

    my question is when i create instance from MyClassMap and persist in the neo4j every thing is ok. but when i want to get this object from neo4j i get this error

    Retrieved object with ID ‘1’ is an instance of ‘Domain.Entity.Manager’ and unable to cast it to ‘Domain.Entity.Person’

    what should i do?

    thank you.

    Hooman

    Hooman

    May 24, 2012 at 10:43 am

    • Hi Hooman,
      Thanks for reporting the issue. I will have a look at and let you know once I fixed it.

      Regards,
      Sony

      Sony Arouje

      May 24, 2012 at 5:54 pm

  8. Hi Hooman,
    I fixed the issue, you can take the latest version from GitHub. You can find the issue details here https://github.com/sonyarouje/Neo4jD/issues/2

    The test case can find here https://github.com/sonyarouje/Neo4jD/blob/master/Test.Neo4JD/EntityMappingTest/InheritedEntityTest.cs

    Added a new function in ModelBuilder called AddAssembly(), you have to use this function if your entity is inherited.

    Regards,
    Sony Arouje

    Sony Arouje

    May 24, 2012 at 6:25 pm

    • hi sony.
      thank you very much for reply and fixing the issue.
      neo4jd is very nice .net api for neo4j db.

      Hooman

      May 25, 2012 at 5:04 pm

      • You welcome Hooman. Thanks for using Neo4jD. If you come across any issues you can raise it in GitHub itself. Have a nice day.

        Sony Arouje

        May 25, 2012 at 5:07 pm

      • hi sony.
        please lets look at previouse example:

        public abstract class Person

        {

        [EntityId]

        public int Id{get;set;}

        }

        public class Manager : Person

        {

        public string OfficeName{get;set;}

        }

        public class MyClass

        {

        [EntityId]

        public int Id{get;set;}

        public Person SelectedPerson{get;set;}

        }
        _______________________________________________________________________

        public void main()
        {
        var mapper = new NodeMapper();
        var myClass = new MyClass{SelectedPerson = new Manager{OfficeName = “Neo4jd”}};
        mapper.Save(myClass);
        var retriveMyClass = mapper.Get(myClass.Id);
        =========Here is the problem======
        var selectedPersonOfficeName = ((Manager)retriveMyClass.SelectedPerson).OfficeName;
        //retriveMyClass.SelectedPerson is a type of PersonProxy not ManagerProxy and loose information.
        ==============================
        }
        __________________________________________________

        i think may be you should do somthing like this but not exactly:
        ive just wrote changes to NodeMapper class

        public class NodeMapper
        {
        //becaus of static method i pas these from my domain logic class:
        public static Assembly CurrentAssembly { get; set; }
        public static NodeMapper CurrentMapper { get; set; }
        ………

        public static IList LoadRelatedEntitiesWithId(int id, string memberName) where TRelated : class
        {
        ………………

        foreach (Node nodeToConvert in relatedNodes)
        {
        var nodeToConvertType = CurrentAssembly.GetTypes().Single(p => p.FullName == nodeToConvert.GetProperty(“clazz”));

        relatedEntities.Add((TRelated)ReflectionHelper.InvokePrivateStaticGenericMethod(CurrentMapper, “Get”, nodeToConvertType, new object[] { nodeToConvert }));
        }
        return relatedEntities;
        }
        }

        and in ReflectionHelper class ive created this method:

        public static object InvokePrivateStaticGenericMethod(object obj,string methodName,Type genericType,object[] args)
        {
        return obj.GetType().GetMethod(methodName,BindingFlags.Static | BindingFlags.NonPublic).MakeGenericMethod(genericType).Invoke(null, args);
        }

        now the type of SelectedPerson is a type of ManagerProxy not PersonProxy and you have all of information of manager.

        thank you
        Hooman

        Hooman

        May 25, 2012 at 10:07 pm

  9. I found neo4jd and it was far easier to get working than other dotnet solutions for neoj4, but I have some issues to get everything to work, specially I don’t know how the app.config should look like, would it possible to see a simple example how the app.config should look like if the above examples wouldn’t be a test example.

    Trizt

    November 20, 2012 at 2:09 am

    • Hi Trizt,
      I am so glad to hear that you liked Neo4jD. You don’t need much in config file, only thing you need is an app setting to store the BaseURI and that needs to passed to GraphEnvironment.SetBaseUri(). That’s all you need to start working with Neo4jD. Let me if you have any questions.

      Sony Arouje

      November 20, 2012 at 1:53 pm

      • Ah, never thought about that, just too justed to how you can do with entityframework. Then I’ll make some changes to my test project and get it a bit more dynamic. I should have mentioned that everything seems to work as intended in a mono environment running on Linux.

        Trizt

        November 20, 2012 at 6:41 pm


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.

Join 175 other followers

%d bloggers like this: