Skip to content

Using Gremlin through Java

okram edited this page Jan 5, 2011 · 42 revisions

Compiling Gremlin from Java

Gremlin makes use of Groovy as its host language. As such, Gremlin.groovy compiles down to a Groovy.class. It is possible to use Gremlin in a Java application. The primary to interact with Gremlin.groovy is through the Gremlin.compile(String gremlin) method. This method creates a Pipe (see Pipes) that can be iterated. An example is provided below.

Graph graph = TinkerGraphFactory.createTinkerGraph();
Pipe pipe = Gremlin.compile("outE.inV.name");
pipe.setStarts(graph.getVertex(1));
for(String name : pipe) {
  System.out.println(name);
}

An Introduction to JSR 223

One of the exciting developments that occurred in Java 1.6+ was the creation of a collection of interfaces that allow developers to tie other languages to the Java virtual machine (JSR 223). In this way, these languages, through a standard set of methods, can be used within any Java 1.6+ application. Thus, it’s possible to capitalize on the features of another language when building a Java application. For Java applications that make use of graphs, Gremlin is a prime candidate for inclusion.

The reference implementation of JSR 223 deployed with Java 1.6+ is Mozilla’s JavaScript implementation known as Rhino. Other popular implementations include Jython, JRuby, and Groovy. For an excellent reference to other implementations of JSR 223, please see https://scripting.dev.java.net.

Finally, you can learn more about JSR 223 from the articles below.

Gremlin provides two classes that should be communicated with directly when using the Gremlin virtual machine from within a Java application.

  • GremlinScriptEngine implements ScriptEngine
  • GremlinScriptEngineFactory implements ScriptEngineFactory

The common way in which to use Gremlin through these interfaces is as follows. First add a text file named javax.script.ScriptEngineFactory to your META-INF/services directory (ScriptEngineFactory is a service provider). In that file, add the line com.tinkerpop.gremlin.GremlinScriptEngineFactory. Now, the GremlinScriptEngineFactory is available to the ScriptEngineManager.

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("gremlin");
// or, instead of above, simply do:
//       ScriptEngine engine = new GremlinScriptEngineFactory().getScriptEngine();
Graph graph = TinkerGraphFactory.createTinkerGraph();
engine.getBindings(ScriptContext.ENGINE_SCOPE).put("g", graph);
engine.getBindings(ScriptContext.ENGINE_SCOPE).put("v", graph.getVertex(1)); 
engine.getBindings(ScriptContext.ENGINE_SCOPE).put("name", "josh");
engine.getBindings(ScriptContext.ENGINE_SCOPE).get("name");
...
Iterable results = (Iterable) engine.eval("v.outE.inV[name:name]");
String script = "i := 0\n" +
                "for(x in 1..10)\n" +
                "  i++";
engine.eval(script);
engine.eval(new FileReader("script.grm"));
  • ENGINE_SCOPE: Engine scope variables/bindings are visible during the lifetime of a single ScriptEngine and a set of variables is maintained for each engine.
  • GLOBAL_SCOPE: Global scope variables/bindings are visible to all engines created by same ScriptEngineManager. This is a SimpleBindings that allow various ScriptEngine ’s created from the same manager to communicate with one another through a “blackboard”. For example, global scope bindings in a Groovy engine can be used by a Gremlin engine and vice versa.

To avoid scoping and thus, variable sharing, use

ScriptEngine engine = new GremlinScriptEngine();


  • See the JavaDoc on ScriptEngine for all the methods exposed by GremlinScriptEngine.
  • See the JavaDoc on ScriptEngineFactory for all the methods exposed by GremlinScriptEngineFactory.