简体   繁体   中英

How to add janus graph imports to gremlin groovy script engine?

I use GremlinGroovyScriptEngine, which is part of gremlin-server to evaluate string gremlin queries - like this:

final ScriptEngine engine = new GremlinGroovyScriptEngine();
engine.eval("g.V().count().next();");

... everything was good until I have started to use janus-graph specific elements in queries - like that (last string):

final ScriptEngine engine = new GremlinGroovyScriptEngine();

//== Set binding with traversal/graph/transaction to script engine ===
JanusGraphManagement mgmt = jg.openManagement();
SimpleBindings trBinding = new SimpleBindings();
trBinding.putAll(this.bindings);
trBinding.put("mgmt", mgmt);
engine.setBindings(trBinding, ScriptContext.ENGINE_SCOPE);

result = engine.eval("mgmt.makePropertyKey('zzzzzz').dataType(String.class).cardinality(Cardinality.SINGLE).make();");

... in that case I got:

MissingPropertyException: No such property: SINGLE for class: org.apache.tinkerpop.gremlin.structure.VertexProperty$Cardinality

As workaround I define whole class name org.janusgraph.core.Cardinality.SINGLE in query.

As I understand it is possible to set up all specific import to script engine during its creation. Janus specific imports is defined in JanusGraphGremlinPlugin class which I use during gremlin-script-engine initialization in this way:

JanusGraphGremlinPlugin graphGremlinPlugin = JanusGraphGremlinPlugin.instance();

GremlinScriptEngineManager engineManager = new CachedGremlinScriptEngineManager();

/* Create gremlin script engine */
GremlinGroovyScriptEngine engine = GremlinGroovyScriptEngine.class
.cast(engineManager.getEngineByName("gremlin-groovy"));

... but it does not work. It seems that engineManager not set any plugins because after creation of engine engine.getPlugins().size() gives 0.

Also there is direct method of engine for loading plugin:

...
 engine.loadPlugins(Collections.singletonList(graphGremlinPlugin))
...

... but it receive List of instances of org.apache.tinkerpop.gremlin.groovy.plugin.GremlinPlugin class which is deprecated (replaced by org.apache.tinkerpop.gremlin.jsr223.GremlinPlugin ). Moreover JanusGraphGremlinPlugin class is descendant of org.apache.tinkerpop.gremlin.jsr223.AbstractGremlinPlugin so that it cannot be used in .loadPlugins() method.

Do you know how it is possible to use JanusGraphGremlinPlugin class to add janus-specific imports to gremlin-groovy-engine?

You need to add the plugin to the GremlinScriptEngineManager instance:

GremlinScriptEngineManager engineManager = new CachedGremlinScriptEngineManager();
engineManager.addPlugin(JanusGraphGremlinPlugin.instance())
engine = engineManager.getEngineByName("gremlin-groovy")

As long as the plugin is added before you instantiate the engine, it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM