Getting started

The Dse.Graph package leverages the features of Gremlin.Net language variant and the high-level client driver features of the DataStax Enterprise C# Driver.

using Dse;
using Dse.Graph;
using Gremlin.Net;

To start building traversals, you will need a IDseSession instance that represents a pool of connections to your DSE cluster.

IDseCluster cluster = DseCluster.Builder()
IDseSession session = cluster.Connect();

IDseSession instances of the DSE driver are designed to be long-lived and you should normally reuse it during your application lifetime.

You can use your IDseSession instances to obtain GraphTraversalSource instances.

GraphTraversalSource g = DseGraph.Traversal(session);

A GraphTraversalSource from Gremlin.Net can be used (and reused) to get traversals.

var traversal = g.V().HasLabel("person");

Traversal Execution

Explicit execution

Traversals can be executed like regular GraphStatement instaces using IDseSession.ExecuteGraph() and IDseSession.ExecuteGraphAsync() methods.

The returned types from this execution would be the ones from the DataStax Enterprise C# Driver, in the Dse.Graph namespace.

var statement = DseGraph.StatementFromTraversal(g.V().HasLabel("person"));
GraphResultSet result = session.ExecuteGraph(statement);

You can benefit from the extension method on the Dse.Graph namespace to call ExecuteGraph() using the traversal, without the need to manually convert it:

GraphResultSet result = session.ExecuteGraph(g.V().HasLabel("person"));

GraphResultSet is an IEnumerable<IGraphNode> implementation. IGraphNode represents a response item returned by the server. Each item can be converted to the expected type, for example: node.To<IVertex>(). You can also apply a conversion to the expected type to all the sequence by using GraphResultSet.To<T>() method:

foreach (IVertex vertex in result.To<IVertex>())

Implicit execution

Traversals can be executed on the server using the methods that represents Gremlin terminal steps. In the case of Gremlin.Net variant, those are ToList(), ToSet(), Next(), NextTraverser() and Iterate(), along with Promise() for async traversal execution.

The types returned from this type of execution will be Gremlin.Net types.

// An IList<Gremlin.Net.Structure.Vertex> instance
IList<Vertex> people = g.V().HasLabel("person").ToList();

Enums, Static Methods and the Anonymous Traversal

Gremlin has various tokens (ie: T, P, Order, …) that are represented in Gremlin.Net as classes and enums.

g.V().HasLabel("person").Has("age", P.Gt(36))

The statements can be further simplified with the using static directive in C# 6.

using static Gremlin.Net.Process.Traversal.P;

Then it is possible to represent the above traversal as below.

// Gt is declared in the P class
g.V().HasLabel("person").Has("age", Gt(36))

Finally, the anonymous traversal is exposed in the class __ that can be statically imported, allowing to be expressed as below:

using static Gremlin.Net.Process.Traversal.__;
// Out is declared in the __ class

Execution options

As explained in the C# DSE driver docs, the graph options can be defined when initializing the cluster, making them the defaults for all graph executions.

var dseCluster = DseCluster.Builder()
    .WithGraphOptions(new GraphOptions().SetName("demo"))

In the previous example, the graph with the name “demo” will be used for all executions.

Additionally, you can define the graph options when obtaining the GraphTraversalSource.

var g = DseGraph.Traversal(session, new GraphOptions().SetName("demo"));

That way all traversals created from the GraphTraversalSource instance will be using those options.