将 Gremlin 查询作为字符串获取并在 java 中执行它,而无需将其提交给 GremlinServer



我有一个字符串格式的Gremlin查询(例如"g.V(("(。我想执行这个字符串而不将其提交给 GremlinServer。

我使用以下依赖项:

<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>3.3.1</version>
</dependency>

有什么办法吗?

你可以直接在 GremlinGroovyScriptEngine 中或通过 GremlinExecutor 执行 Gremlin字符串(它只是将字符串传递给GremlinGroovyScriptEngine但有一些附加功能(。只需将 Gremlin 字符串传递给相应的eval()方法,并从该脚本评估中返回结果。这基本上就是Gremlin Server在内部所做的。

您可能需要gremlin-groovy依赖项而不是gremlin-driver依赖项。

添加一个基于Stephen answer+comment的"完整"示例:

public static void main(String[] args) throws ScriptException, ExecutionException, InterruptedException {
Graph graph = TinkerGraph.open();
Configuration c = graph.configuration();
GraphTraversalSource g = graph.traversal();
// Creating graph
Vertex marko = g.addV("person").property("name","marko").property("age",29).next();
Vertex lop = g.addV("software").property("name","lop").property("lang","java").next();
g.addE("created").from(marko).to(lop).property("weight",0.6d).iterate();
g.io("test.xml").write().iterate(); // saving to file
//standard query
GraphTraversal<Vertex, Map<Object, Object>> javaQueryResult = g.V().hasLabel("person").valueMap();
// preparing GremlinExecutor
ConcurrentBindings b = new ConcurrentBindings();
b.putIfAbsent("g", g);
GremlinExecutor ge = GremlinExecutor.build().evaluationTimeout(15000L).globalBindings(b).create();
CompletableFuture<Object> evalResult = ge.eval("g.V().hasLabel('person').valueMap()");
GraphTraversal actualResult = (GraphTraversal) evalResult.get();
}

简单的调试应用,用于检查从字符串计算的结果与标准查询的比较情况。

使用 maven 依赖项tinkergraph-gremlingremlin-coregremlin-groovy,版本 3.4.6

最新更新