将 Gremlin 查询作为字符串获取并在 java 中执行,而不将其提交给 GremlinServer
Get Gremlin query as a String and execute it in java without submitting it to the GremlinServer
我有一个字符串格式的 Gremlin 查询(例如 "g.V()")。我想执行这个字符串而不将它提交给 GremlinServer。
我使用以下依赖项:
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>3.3.1</version>
</dependency>
有什么办法吗?
您可以直接在 GremlinGroovyScriptEngine or through the GremlinExecutor 中执行 Gremlin 字符串(它只是将字符串传递给 GremlinGroovyScriptEngine
但有一些附加功能)。简单地将 Gremlin 字符串传递给适当的 eval()
方法并从该脚本评估中获取结果。这基本上就是 Gremlin 服务器在内部所做的。
您可能需要 gremlin-groovy
依赖项而不是 gremlin-driver
。
添加一个 'complete' 基于 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-gremlin
gremlin-core
gremlin-groovy
,版本 3.4.6
我有一个字符串格式的 Gremlin 查询(例如 "g.V()")。我想执行这个字符串而不将它提交给 GremlinServer。
我使用以下依赖项:
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>3.3.1</version>
</dependency>
有什么办法吗?
您可以直接在 GremlinGroovyScriptEngine or through the GremlinExecutor 中执行 Gremlin 字符串(它只是将字符串传递给 GremlinGroovyScriptEngine
但有一些附加功能)。简单地将 Gremlin 字符串传递给适当的 eval()
方法并从该脚本评估中获取结果。这基本上就是 Gremlin 服务器在内部所做的。
您可能需要 gremlin-groovy
依赖项而不是 gremlin-driver
。
添加一个 'complete' 基于 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-gremlin
gremlin-core
gremlin-groovy
,版本 3.4.6