gremlintinkerpoptinkergraph

How can I run queries in TinkerGraph using query strings?


I want to run some queries in TinkerGraph, but I need to read them from an external text file, instead of performing Java calls:

TinkerGraph graph = TinkerGraph.open();
GraphTraversalSource g = graph.traversal();


InputStream input = new FileInputStream("graph.json");
GraphSONReader reader = GraphSONReader.build().create();
reader.readGraph(input, graph);

// Something like
run("g.V().hasLabel("foo").."

I've checked this question but the answers show some solutions using ConcurrentBindings that are not very clear to me.


Solution

  • You can use the GremlinGroovyScriptEngine class to perform the evaluation of a text string query, while using embedded TinkerGraph. If working with a Gremlin Server you would not need to do this.

    The example below should more or less work unchanged. You may need to add a few include and type declaration statements from Java code but the basic building blocks essentially are these:

    ScriptEngine engine = new GremlinGroovyScriptEngine(); 
    Bindings bindings = engine.createBindings(); 
    bindings.put("g", graph.traversal()); 
    engine.eval("g.V().limit(5)", bindings);