[英]Java GraphTraversal output Gremlin query
如何从 Java GraphTraversal
object 查询 output Gremlin? 默认的 output ( graphTraversal.toString()
) 看起来像[HasStep([~label.eq(brand), name.eq(Nike), status.within([VALID])])]
不容易阅读。
Gremlin 提供了 GroovyTranslator class 来帮助解决这个问题。 这是一个例子。
// Simple traversal we can use for testing a few things
Traversal t =
g.V().has("airport","region","US-TX").
local(values("code","city").
fold());
// Generate the text form of the query from a Traversal
String query;
query = GroovyTranslator.of("g").
translate(t.asAdmin().getBytecode());
System.out.println("\nResults from GroovyTranslator on a traversal");
System.out.println(query);
这取自此处的一组示例: https://github.com/krlawrence/graph/blob/master/sample-code/RemoteWriteText.java
您可以在DefaultGraphTraversal
上使用getByteCode()
方法来获取 output gremlin 查询。
例如,考虑下图
Graph graph = TinkerGraph.open();
Vertex a = graph.addVertex(label, "person", "name", "Alex", "Age", "23");
Vertex b = graph.addVertex(label, "person", "name", "Jennifer", "Age", "20");
Vertex c = graph.addVertex(label, "person", "name", "Sophia", "Age", "22");
a.addEdge("friends_with", b);
a.addEdge("friends_with", c);
获取图遍历如下:
GraphTraversalSource gts = graph.traversal();
GraphTraversal graphTraversal =
gts.V().has("name","Alex").outE("friends_with").inV().has("age", P.lt(20));
现在,您可以将遍历作为字符串:
String traversalAsString = graphTraversal.asAdmin().getBytecode().toString();
它为您提供 output 为:
[[], [V(), has(name, Alex), outE(friends_with), inV(), has(age, lt(20))]]
它更具可读性,几乎就像您提供的查询一样。 如果您想替换[,]
,现在可以修改/解析字符串以获取实际查询,并添加将它们与.
就像在实际查询中一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.