Gremlin-groovy script queries

As the first integration language, Gremlin-groovy requests were initially used to query DSE Graph via query Strings. However, such an interface is very difficult to use and takes a lot of time and expertise in the Traversal API and the Gremlin language before being able to use it without surprising errors.

Hence we recommend using the Fluent API which provides a much easier-to-use interface, provides mostly the same capabilities as the Gremlin-groovy queries and has better performance. The only queries that right now can only be done via the Gremlin-groovy interface are the DSE Graph Schema queries.

To create a GraphStatement out of a Gremlin-groovy script use the following:

String groovyScript = "system.graph('demo').ifNotExists().create()";

GraphStatement graphStatement = new SimpleGraphStatement(groovyScript);

Gremlin-groovy scripts can also be used to execute graph traversals (although as explained, is a less convenient and unrecommended way to do queries with DSE Graph).

Gremlin-groovy script parameters

Gremlin-groovy scripts accept parameters, which are always named. Parameter bindings are passed as a Map<String, Object> alongside the query (Guava’s ImmutableMap provides a convenient way to build maps on the fly):


// One-liner:
dseSession.executeGraph("g.addV(label, vertexLabel)",
        ImmutableMap.<String, Object>of("vertexLabel", "test_vertex_2"));

// Alternative syntax:
dseSession.executeGraph("g.addV(label, vertexLabel)",
        ImmutableMap.<String, Object>builder()
                .put("vertexLabel", "test_vertex_2")

Another way to specify parameters is to use the set() method on a SimpleGraphStatement:

SimpleGraphStatement s = new SimpleGraphStatement("g.addV(label, vertexLabel)")
        .set("vertexLabel", "test_vertex_2");

Note that, unlike in CQL, placeholders in a Gremlin-groovy script are not prefixed with “:”.