Chapter 20. Connecting from .NET

JanusGraph can be queried from a .NET application with Apache TinkerPop’s Gremlin.Net.

Gremlin traversals can be constructed with Gremlin.Net just like in Gremlin-Java or Gremiln-Groovy. Refer to Chapter 6, 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.

20.1. 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.4.1
  3. Create a GraphTraversalSource which is the basis for all Gremlin traversals:

    var graph = new Graph();
    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 = graph.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.");

    Next() is a terminal step that submits the traversal to the Gremlin Server and returns a single result. Other terminal steps can be found in TinkerPop’s reference documentation.

    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());

20.2. 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.