如何从Java中输出Gremlin查询。GraphTraversal
对象?默认输出(graphTraversal.toString()
)看起来像 [HasStep([~label.eq(brand), name.eq(Nike), status.within([VALID])])]
这是不容易阅读的。
Gremlin提供了GroovyTranslator类来帮助解决这个问题。下面是一个例子。
// 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.comkrlawrencegraphblobmastersample-codeRemoteWriteText.java。
您可以使用 getByteCode()
办法 DefaultGraphTraversal
来获得输出的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);
得到一个如下的图形Traversal。
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();
它给你的输出为:
[[], [V(), has(name, Alex), outE(friends_with), inV(), has(age, lt(20))]]
它的可读性更强,几乎就像你提供的查询一样。如果你想的话,你现在可以对字符串进行修改,以获得实际的查询,比如将其替换为 [,]
,并将其与 .
像实际查询中。