Search code examples
graphgremlintinkerpop

Java GraphTraversal output Gremlin query


How to output Gremlin query from a Java GraphTraversal object? The default output (graphTraversal.toString()) looks like [HasStep([~label.eq(brand), name.eq(Nike), status.within([VALID])])] which is not easy to read.


Solution

  • Gremlin provides the GroovyTranslator class to help with that. Here is an example.

    // Simple traversal we can use for testing a few things
    Traversal t = 
      g.V().has("airport","region","US-TX").
            local(values("code","city").
            fold());
    
    
    // Generate the text form of the query from a Traversal
    String query;
    query = GroovyTranslator.of("g").
            translate(t.asAdmin().getBytecode());
    
    System.out.println("\nResults from GroovyTranslator on a traversal");
    System.out.println(query);
    

    This is taken from a set of examples located here: https://github.com/krlawrence/graph/blob/master/sample-code/RemoteWriteText.java