cassandradatastax-enterprisedatastax-java-driverdatastax-enterprise-graph

Combine Traversal API and Gremlin


This might be a silly question.

I like traversal API because of the type safety I get from within my Java program, However am exploring the possibility of using it in conjunction with Gremlin API.

Something like roughly below. Am guessing this is not possible, But would like to know.

GraphTraversalSource g; // Get reference
g.V().has('author', 'name', 'Duke').injectGrooovy('SomeExternalGroovy').toList()

Solution

  • I'll start with a side-note....I wouldn't make a distinction between "Traversal API" and "Gremlin API" - that's not a comparison we typically make. There is a distinction between the Traversal API and the Graph API. The Traversal API is for users (like you) and the Graph API is for graph providers (like DSE Graph) who want to become TinkerPop enabled. The Traversal API is initialized through GraphTraversalSource, typically named g whereas the Graph API is initialized through the Graph, typically named graph. You can see the javadoc for related classes in the Traversal API here and the Graph API here. The Traversal API forms the steps that we think of in the Gremlin language.

    The equivalent to injectGroovy("SomeExternalGroovy") is likely something akin to:

    g.V().has('author','name','Duke').map(Lambda.function("it.get().value('name')"))
    

    It basically passes a Groovy closure as a string into the map() step and the server will evaluate the string in the context of the server when it executes the traversal. I am assuming here of course that you are creating g through the DseGraph class like this:

    GraphTraversalSource g = DseGraph.traversal();
    

    If you were just submitting a string then you could just use Groovy directly do:

    dseSession.executeGraph("g.V().has('author','name','Duke').map{it.get().value('name')"};
    

    Note that you will need to enable lambdas in DSE Graph for any of this to work, by issuing a command like:

    graph.schema().config().option("graph_name.traversal_sources.g.restrict_lambda").set(false)
    

    Before you do any of that though you should ask yourself why you need to use a lambda. TinkerPop generally advises that you avoid lambdas in your Gremlin and only use them when there are no other options available to you. Gremlin is quite expressive and in most cases you can usually find appropriate Gremlin steps to replicate what you are doing in a lambda.