CosmosDB with Gremlin.NET - part 2

On top of being faster than the original client, the Gremlin.NET native driver comes with a couple more features that make it really appealing to use: Fluent API and Query Bindings

Fluent API

First and most interesting is the fluent API. Gone are the days when you need to build complicated queries as a string and worry about syntax and various issues when typing that long Gremlin string. Now you can use a more intuitive, and intellisense powered query.

String query:

1
2
3
4
5
6
7
8
9
10
11
var server = new GremlinServer(
connectionString,
443,
enableSsl: true,
username: $"/dbs/{dbName}/colls/{collectionName}",
password: key);

using (var client = new GremlinClient(_server))
{
var result = await client.SubmitAsync<T>("g.V().hasLabel('person').has('age', gt(40))")
);

Fluent API:

1
2
3
4
5
6
7
8
9
10
var server = new GremlinServer(
connectionString,
443,
enableSsl: true,
username: $"/dbs/{dbName}/colls/{collectionName}",
password: key);
var graph = new Graph();
var g = graph.Traversal().WithRemote(new DriverRemoteConnection(new GremlinClient(_server)));

return g.V().hasLabel("person").has("age", gt(40)).ToList()

Query Bindings

When a query is sent over to the gremlin server, it needs to be translated, compiled and executed. But if you send the same query twice, the server can skip the translation and compilation steps and execute directly, giving you a performance boost in the process.
However, it’s not likely that you need to run the exact query over and over, it’s more probable that you can extract some parameters that change in the query - i.e. when you apply some filters in your UI, the values are parameters in the same base query. To achieve this in Gremlin, we use the concept of bindings:

###String Query:

1
2
3
4
5
6
7
var arguments = new Dictionary<string, object>
{
{"maxAge," 40}
};
var queryString = "g.V().hasLabel('person').has('age', gt(maxAge))"

await client.SubmitAsync<T>(queryString, arguments);

Fluent API

1
2
3
var b = new Bindings();
g.V().hasLabel('person').has('age', gt(b.Of("age",40)).ToList();
g.V().hasLabel('person').has('age', gt(b.Of("age",30)).ToList();
Share Comments