Big Data/Analytics Zone is brought to you in partnership with:

Damaris has posted 19 posts at DZone. View Full User Profile

How to Build Your First .NET Graph Database

03.13.2013
| 5954 views |
  • submit to reddit

There has been a lot of talking about NoSQL lately, and how this "new" databases can solve problems of Big Data. NoSQL is a movement that promotes alternatives to the relational model in order to provide higher scalability and availability using simple and lightweight systems of storage and retrieval.

As part of the NoSQL movement, Graph Databases in particular are especially suited for applications that need to analyze relationships in a network of data assuring high scalability. They are also suited to deal with ad-hoc data or including different type of data as they are less rigid with the schema. 

Among the most well-known databases there are solutions specially for .Net developers, for instance DEX graph database offers full support for their native C# API with direct and fast access to the graph core, popular Neo4j offers third-party .NET client via REST API, Microsoft research Trinity also supports C# language (although it is more a key-value solution) and CloudGraph seems to focus also on .Net with a beta-release.

So, for all .Net developers that are interested into using graphs for their applications, this article will give them a first hint of how to construct a first graph in C# using Visual Studio. For the example we are going to use DEX's native .Net API for a simple first approach.

  1. Download your API (in this case DEX API can be downloaded from here )
  2. Create the C# console application
  3. Replace the Program.cs with the sample below.  This is a typical HelloWorld example where the developer will create a graph database with two nodes (of the same type) and an edge between them. Nodes and edges are the main units of a graph (graph theory ), nodes can be seen as a row of a relational table whereas edges could be seen as the foreign key relating two rows from different tables. The example also includes a neighbors operation, in order to see how information is retrieved from a graph database. Following the relational simile, this retrieval from the graph would be like a select operation from the rows of the two tables joined by the foreign key.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using com.sparsity.dex.gdb;
    
    namespace HelloWorld
    {
        class Program
        {
            static void Main(string[] args)
            {
                //
                // Create a sample database
                //
                DexConfig cfg = new DexConfig();
                Dex dex = new Dex(cfg);
                Database db = dex.Create("HelloWorld.dex", "HelloWorld");
                Session sess = db.NewSession();
                Graph g = sess.GetGraph();
    
                // Add a node type with two attributes
                int nodeType = g.NewNodeType("TheNodeType");
                int idAttribute = g.NewAttribute(nodeType, "id", DataType.Long, AttributeKind.Unique);
                int nameAttribute = g.NewAttribute(nodeType, "name", DataType.String, AttributeKind.Indexed);
    
                // Add a directed edge type with an attribute
                int edgeType = g.NewEdgeType("TheEdgeType", true, false);
    
                // Add a node
                long hellow = g.NewNode(nodeType);
                Value value = new Value();
                g.SetAttribute(hellow, idAttribute, value.SetLong(1));
                g.SetAttribute(hellow, nameAttribute, value.SetString("Hellow"));
    
                // Add another node
                long world = g.NewNode(nodeType);
                g.SetAttribute(world, idAttribute, value.SetLong(2));
                g.SetAttribute(world, nameAttribute, value.SetString("World"));
    
                // Add an edge
                long theEdge = g.NewEdge(edgeType, hellow, world);
    
                // Get the neighbors of the first node using the edges of "TheEdgeType" type
                Objects neighbors = g.Neighbors(hellow, edgeType, EdgesDirection.Outgoing);
    
                // Say hello to the neighbors
                ObjectsIterator it = neighbors.Iterator();
                while (it.HasNext())
                {
                    long neighborOid = it.Next();
                    g.GetAttribute(neighborOid, edgeType, value);
                    System.Console.WriteLine("Hello " + value.GetString());
                }
                // The ObjectsIterator must be closed
                it.Close();
                // The Objects must be closed
                neighbors.Close();
    
                // Close the database
                sess.Close();
                db.Close();
                dex.Close();
            }
        }
    }
  4. Add a reference in the project (Menu Project -> AddReference) to the library "dexnet.dll".
  5. Put the other dlls in a directory where the application can found it. The easier way is to put them where the application exe is created. Or set the working directory to the directory where you have the dlls. But you could also put them with the windows dlls.
  6. Here it is your first .Net graph!
  7. What next? populate your graph database with more nodes and edges (play with different types of nodes and edges!) and also do not forget to use popular graph algorithms (take a look at this dzone article here or this more advanced here )


Published at DZone with permission of its author, Damaris Coll.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)