[英]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: 我使用GremlinGroovyScriptEngine,它是gremlin服务器的一部分,用于评估字符串gremlin查询-像这样:
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): ...一切都很好,直到我开始在查询中使用janus-graph特定元素-这样(最后一个字符串):
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. 解决方法是,在查询中定义整个类名org.janusgraph.core.Cardinality.SINGLE
。
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: Janus特定的导入在JanusGraphGremlinPlugin类中定义,我在gremlin-script-engine初始化期间以这种方式使用:
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. 似乎engineManager
没有设置任何插件,因为在创建引擎engine.getPlugins().size()
给出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
). ...但它接收已弃用的org.apache.tinkerpop.gremlin.groovy.plugin.GremlinPlugin
类的实例List
(由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. 而且JanusGraphGremlinPlugin类是org.apache.tinkerpop.gremlin.jsr223.AbstractGremlinPlugin
后代,因此无法在.loadPlugins()
方法中使用。
Do you know how it is possible to use JanusGraphGremlinPlugin class to add janus-specific imports to gremlin-groovy-engine? 您知道如何使用JanusGraphGremlinPlugin类将janus特定的导入添加到gremlin-groovy-engine吗?
You need to add the plugin to the GremlinScriptEngineManager
instance: 您需要将插件添加到GremlinScriptEngineManager
实例:
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. 只要在实例化引擎之前添加了插件,它就应该起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.