[英]Java GraphTraversal output Gremlin query
How to output Gremlin query from a Java GraphTraversal
object?如何从 Java GraphTraversal
object 查询 output Gremlin? The default output ( graphTraversal.toString()
) looks like [HasStep([~label.eq(brand), name.eq(Nike), status.within([VALID])])]
which is not easy to read.默认的 output ( graphTraversal.toString()
) 看起来像[HasStep([~label.eq(brand), name.eq(Nike), status.within([VALID])])]
不容易阅读。
Gremlin provides the GroovyTranslator class to help with that. Gremlin 提供了 GroovyTranslator class 来帮助解决这个问题。 Here is an example.这是一个例子。
// 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);
This is taken from a set of examples located here: https://github.com/krlawrence/graph/blob/master/sample-code/RemoteWriteText.java这取自此处的一组示例: https://github.com/krlawrence/graph/blob/master/sample-code/RemoteWriteText.java
You can use getByteCode()
method on a DefaultGraphTraversal
to get output gremlin query.您可以在DefaultGraphTraversal
上使用getByteCode()
方法来获取 output gremlin 查询。
For example, consider the following graph例如,考虑下图
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);
Get a graph Traversal as following:获取图遍历如下:
GraphTraversalSource gts = graph.traversal();
GraphTraversal graphTraversal =
gts.V().has("name","Alex").outE("friends_with").inV().has("age", P.lt(20));
Now you can get your traversal as a String as:现在,您可以将遍历作为字符串:
String traversalAsString = graphTraversal.asAdmin().getBytecode().toString();
It gives you output as:它为您提供 output 为:
[[], [V(), has(name, Alex), outE(friends_with), inV(), has(age, lt(20))]]
It is much more readable, almost like the one you have provided as the query.它更具可读性,几乎就像您提供的查询一样。 You can now modify/parse the string to get the actual query if you want like replacing [,]
, adding joining them with .
如果您想替换[,]
,现在可以修改/解析字符串以获取实际查询,并添加将它们与.
like in actual query.就像在实际查询中一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.