Skip to content

Connecting from .NET

Gremlin traversals can be constructed with Gremlin.Net just like in Gremlin-Java or Gremlin-Groovy. Refer to Gremlin Query Language for an introduction to Gremlin and pointers to further resources. The main syntactical difference for Gremlin.Net is that it follows .NET naming conventions, e.g., method names use PascalCase instead of camelCase.

Getting Started with JanusGraph and Gremlin.Net

To get started with Gremlin.Net:

  1. Create a console application:

    dotnet new console -o GremlinExample
    

  2. Add Gremlin.Net:

    dotnet add package Gremlin.Net -v 3.7.3
    

  3. Create a GraphTraversalSource which is the basis for all Gremlin traversals:

    using static Gremlin.Net.Process.Traversal.AnonymousTraversalSource;
    
    var client = new GremlinClient(new GremlinServer("localhost", 8182));
    // The client should be disposed on shut down to release resources
    // and to close open connections with client.Dispose()
    var g = Traversal().WithRemote(new DriverRemoteConnection(client));
            // Reuse 'g' across the application
    

  4. Execute a simple traversal:

    var herculesAge = g.V().Has("name", "hercules").Values<int>("age").Next();
    Console.WriteLine($"Hercules is {herculesAge} years old.");
    
    The traversal can also be executed asynchronously by using Promise() which is the recommended way as the underlying driver in Gremlin.Net also works asynchronously:
    var herculesAge = await g.V().Has("name", "hercules").Values<int>("age").Promise(t => t.Next());
    

JanusGraph.Net for JanusGraph Specific Types and Predicates

JanusGraph contains some types and predicates that are not part of Apache TinkerPop and are therefore also not supported by Gremlin.Net. JanusGraph.Net is a .NET library that adds support for these types and predicates to Gremlin.Net.

After installing the library, a message serializer needs to be configured for JanusGraph which can be done like this for GraphSON 3:

var client = new GremlinClient(new GremlinServer("localhost", 8182),
    new JanusGraphGraphSONMessageSerializer());

or like this for GraphBinary:

var client = new GremlinClient(new GremlinServer("localhost", 8182),
    new GraphBinaryMessageSerializer(JanusGraphTypeSerializerRegistry.Instance));

Refer to the documentation of JanusGraph.Net for more information about the library, including its compatibility with different JanusGraph versions and differences in support between the different serialization formats.