0

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.

Incognitex
  • 30
  • 5

1 Answers1

2

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);
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
  • Do I need to download any other JARs? I'm getting: error: package org.apache.tinkerpop.gremlin.groovy.jsr223 does not exist Although, gremlin-groovy package exists in the classpath. – Incognitex Mar 30 '23 at 18:16
  • On my system that has the Gremlin Console at the 3.6.2 level installed, the file is in the `gremlin-groovy-3.6.2.jar` file. – Kelvin Lawrence Mar 30 '23 at 18:51