Table of Contents
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.
To get started with Gremlin.Net:
Create a console application:
dotnet new console -o GremlinExample
Add Gremlin.Net:
dotnet add package Gremlin.Net -v 3.2.9
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
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());
JanusGraph contains some types and predicates that are not part of Apache TinkerPop and are therefore also not supported by Gremlin.Net.