Fork me on GitHub

Get a Taste of Graph Databases: InfoGrid, Neo4j and sones GraphDB

Alex Popescu, author of the well-known MyNoSQL blog recently wrote about a very (very) basic tag­ging app and its imple­men­ta­tion within the Info­Grid and Neo4j graph data­base systems.

Today we want to pick up the given exam­ple and show you an imple­men­ta­tion using the sones GraphDB and the GraphDB­Sharp API:

 using System; using System.Linq; using System.Collections.Generic; using sones.GraphDB; using sones.GraphDB.Structures; using sones.GraphDB.API.CSharp; using sones.GraphDB.API.CSharp.Reflection; using sones.GraphDB.Connectors.GraphDBREST; using sones.GraphFS.Connectors.GraphFSCLI; using sones.GraphDB.Connectors.GraphDBCLI; namespace TagExample { public class Tag : DBObject { // Will inherit an UUID and RevisionID property from DBObject [CreateIndex(DBIndexTypes.HashTable)] public String Name { get; set; } // Backwardedges to the attribute Tags of type Websites [BackwardEdge("Tags")] public List TaggedWebsites { get; set; } public Tag() { } } public class Website : DBObject { // Will inherit an UUID and RevisionID property from DBObject [CreateIndex(DBIndexTypes.HashTable)] public String Name { get; set; } public String URL { get; set; } // Edges to the tags public List Tags { get; set; } public Website() { } } public class TagExample { private void CheckResult(QueryResult myQueryResult) { Console.WriteLine("{0} => {1}", myQueryResult.Query, myQueryResult.ResultType); } private void Run() { // Create a new in-memory database var GDB = new GraphDBSharp() { DatabaseName = "TagExampleDB", Username = "Dr.Falken", Password = "Joshua" // For persistence use: //StorageLocation = "file://TagExampleDB.fs", //StorageLocation = "net.tcp://127.0.0.1:8000", }; GDB.CreateDatabase(true); // Create types tag and website using reflection GDB.CreateTypes(CheckResult, new Tag(), new Website()); // Insert tags var _good = new Tag() { Name = "good" }; var _funny = new Tag() { Name = "funny" }; GDB.Insert(CheckResult, _good, _funny); // Insert websites and link them to their tags var _cnn = new Website() { Name = "CNN", URL = "http://cnn.com/", Tags = new List() { _good } }; var _xkcd = new Website() { Name = "xkcd", URL = "http://xkcd.com/", Tags = new List() { _good, _funny } }; var _onion = new Website() { Name = "onion", URL = "http://theonion.com/", Tags = new List() { _funny } }; GDB.Insert(CheckResult, _cnn, _xkcd, _onion); // Find out which tags xkcd is tagged with var _xkcdtags = GDB.Query("FROM Website w SELECT w.Tags " + "WHERE w.Name = 'xkcd' DEPTH 1"); foreach (var _tag in (List) _xkcdtags["Tags"]) Console.WriteLine(_tag["Name"]); // List tagged sites var _taggedsites = GDB.Query("FROM Website w SELECT w.Name, " + "Count(w.Tags) AS Counter " + "WHERE Count(w.Tags)>0"); foreach (var _sites in (List) _taggedsites[0]) Console.WriteLine("{0} => {1}", _sites["Name"], _sites["Counter"]); // Start a REST service on localhost port 9975 GDB.StartREST(new Uri("http://localhost:9975")); // Start the GraphDB command line interface GDB.OpenCLI(typeof(ABasicFSCLICommands), typeof(AAdvancedFSCLICommands), typeof(ABasicDBCLICommands), typeof(AAdvancedDBCLICommands)); GDB.Shutdown(); } public static void Main(string[] myArgs) { var w = new TagExample(); w.Run(); } } } 

 

The cur­rent C# API is already very expres­sive, but other pro­gram­ming lan­guages might be much more ver­bose. To avoid writ­ing a lot of code, you can always use our Graph Query Lan­u­age (GraphQL) which is an opti­mized Domain-specific lan­guage (DSL) for cre­at­ing and manip­u­lat­ing a graph within our database.

 CREATE TYPES Tag EXTENDS DBObject ATTRIBUTES (String Name) BACKWARDEDGES (Website.Tags TaggedWebsites) INDICES (Name), Website EXTENDS DBObject ATTRIBUTES (String Name, String URL, LIST Tags) INDICES (Name) INSERT INTO Tag VALUES (Name = 'good') INSERT INTO Tag VALUES (Name = 'funny') INSERT INTO Website VALUES (Name = 'CNN', URL = 'http://cnn.com/', Tags = SETOF (Name = 'good')) INSERT INTO Website VALUES (Name = 'xkcd', URL = 'http://xkcd.com/', Tags = SETOF (Name = 'good', Name = 'funny')) INSERT INTO Website VALUES (Name = 'onion', URL = 'http://theonion.com/', Tags = SETOF (Name = 'funny')) // Find out which tags xkcd is tagged with... FROM Website w SELECT w.Tags WHERE w.Name = 'xkcd' DEPTH 1 // Alternative query... FROM Tag t SELECT t.Name WHERE t.TaggedWebsites.Name = 'xkcd' // List tagged sites... FROM Website w SELECT w.Name, Count(w.Tags) AS Counter WHERE Count(w.Tags)>0 

 

13 comments zu “Get a Taste of Graph Databases: InfoGrid, Neo4j and sones GraphDB”

  • Alex Popescu

    Fan­tas­tic! I’d really love to fea­ture this code along with the oth­ers. Would you be OK with that? (just drop me an email if you agree).

    And thanks a lot for pick­ing up my challenge!

    :- alex

    MyNoSQL: All Things NoSQL
    http://nosql.mypopescu.com

  • Get a Taste of Graph Databases: a Filament Example « SourceForge.net: Project filament

    […] exam­ple illus­trates the sim­i­lar­i­ties and dif­fer­ences between the two graph imple­men­ta­tions.  A follow-up arti­cle from the sones devel­op­ers shows an exam­ple of the same appli­ca­tion in the .NET sones GraphDB […]

  • Neo4j News: Update on Neo4j Ruby bindings | Ruby WebDev Insider

    […] developers.sones.de » Get a Taste of Graph Data­bases: InfoGrid … […]

  • Stefan Wenig

    Wouldn’t this look much pret­tier if you’d pro­vide native LINQ support?

    var tagged­sites = from w in GDB.Query()
    where w.Tags.Count() > 0
    select new { w.Name, Counter = w.Tags.Count() };
    fore­ach (var site in tagged­Sites)
    Console.WriteLine (“{0} => {1}”, site.Name, site.Counter);

    It would make a nice bul­let point on your fea­ture list too ;-)
    If you’re inter­ested, take a look at relinq.codeplex.com. You’d be in the same place as NHiber­nate, they use re-linq to go from LINQ to HQL, only that GQL should make an eas­ier tar­get. (Or con­tact me directly if you want to dis­cuss sup­port options.)

  • Stefan Wenig

    your com­ments eat angle brackets.

    from w in GDB.Query<Website>()

  • Yahya

    Guys, can you please post more elab­o­rated sam­ples around the .net API.

    The sam­ple given above is even incom­plete. When you go to GraphD­Sharp on github, you do not find the C# API there.

    Seems like, you guys have done good job in writ­ing the library, please spend some time on tech docs too.

    Loved it. But frustrated.

  • bietiekay

    it’s mainly a prob­lem of keep­ing the doc­u­men­ta­tion in sync with the progress we are mak­ing with the tech­nol­ogy. If you are work­ing on the cur­rent mas­ter branch on github you’ll find a num­ber of exam­ples in the wiki we added in the last 3 months — doc­u­men­ta­tion is a main focus and we’re going to expand the effort put into doc­u­men­ta­tion in the future. If you got any ques­tion or sug­ges­tion please use the (also new) forum to write about it.

  • Yahya

    The prob­lem is I can not fig­ure out how to imple­ment (as part of POC) some­thing using the C# library. The sam­ples do not work, no proper doc­u­men­ta­tion avail­able, tech­ni­cal doc­u­men­ta­tion is not comprehensive.

    Sones surely could become indus­try leader if it becomes easy-to-use and easy-to-figure-out.

    Can you please pro­vide more doc­u­men­ta­tion around API?

  • Yahya

    If you can get an answer for a spe­cific question.

    I am try­ing to imple­ment the Uni­ver­sity sam­ple in C#. I have added City and Uni­ver­sity, now I want to add Edge between these two Ver­tices. Can you tell me how do I do it?

    Much Appre­ci­ated.

  • bietiekay

    I think we can help you and get your code run­ning — if you would please take the time to post your code includ­ing a short descrip­tion which ver­sion of GraphDB you are cur­rently using into our forum on http://forum.sones.de

  • bietiekay

    maybe you find some answers by tak­ing a look at the most cur­rent API exam­ple: https://github.com/sones/sones/blob/master/Applications/TagExample/Example.cs

    If you check­out out the cur­rent ver­sion of GraphDB (2.1-pre-release) you’ll see the tremen­dous amount of progress we are mak­ing — it’s always hard to keep the doc­u­men­ta­tion cur­rent but we’re doing our best.

    Some more infor­ma­tion on the cur­rent API can be found here: http://developers.sones.de/wiki/doku.php?id=tutorials:tagexample

  • Yahya

    I am using the same 2.1 ver­sion. I will try post­ing my exact prob­lems on the forum.

    Thanks for your help.

  • bietiekay

    Great! We appre­ci­ate it

write a new comment

*