Gremlin Tinkerpop返回一个对象路径,如何访问各个顶点

Gremlin Tinkerpop返回一个对象路径,如何访问各个顶点,gremlin,tinkerpop,tinkerpop3,Gremlin,Tinkerpop,Tinkerpop3,我可以看出这已经被问了好几次了,但我看不出一个适合我的答案 我在Java应用程序中使用它,我想返回顶点的ArrayList或包含顶点iD的字符串的ArrayList。我必须使用Path()步骤,因为我需要以正确的顺序返回顶点,但是我得到的只是一个包含2个对象的列表,看起来像是源顶点作为对象,整个路径作为对象,我不能操纵或使用数据吗 我的遍历是: List<Object> routeList; routeList = g.V(sourceID).shortestPath().with

我可以看出这已经被问了好几次了,但我看不出一个适合我的答案

我在Java应用程序中使用它,我想返回顶点的ArrayList或包含顶点iD的字符串的ArrayList。我必须使用Path()步骤,因为我需要以正确的顺序返回顶点,但是我得到的只是一个包含2个对象的列表,看起来像是源顶点作为对象,整个路径作为对象,我不能操纵或使用数据吗

我的遍历是:

List<Object> routeList;

routeList = g.V(sourceID).shortestPath().with(ShortestPath.edges, Direction.OUT)
                .with(ShortestPath.distance, "weight").with(ShortestPath.target, hasId(targetId))
                .with(ShortestPath.includeEdges, false).path().unfold().toList();
我真的需要以一种可用的格式返回路径。我相信以前有人担任过这个职位,可以帮助我。谢谢。

一般来说,对于path()查询,Gremlin将向您返回一个path对象,您可以对其进行迭代。下面是一个来自Gremlin控制台的示例

gremlin> p = g.V(1).out().limit(1).path().next()
==>v[1]
==>v[135]
gremlin> p.class
==>class org.apache.tinkerpop.gremlin.process.traversal.step.util.ImmutablePath
gremlin> p[1]
==>v[135]
gremlin> p[0]
==>v[1]       
gremlin> p.size()
==>2
gremlin> for (e in p) {println(e)}
v[1]
v[135]    
相关的JavaDoc如下所示:

使用Java,您可以做类似的事情

List<Path> p = g.V("1").out().limit(1).path().toList();
p.get(0).forEach(e -> System.out.println(e));
以下是用于ReferencePath的JavaDoc

您好,谢谢您给我展示了这个例子,它似乎对我不起作用-路径路径列表;routeList=g.V(sourceID).shortestPath().with(shortestPath.edges,Direction.OUT).with(shortestPath.distance,“weight”).with(shortestPath.target,hasId(targetId)).with(shortestPath.includeededges,false).path().next();如果我尝试遍历Path对象,我会收到indexoutofbounds,表示Path的大小只有2。这仍然是同一个问题。我将添加一个简单的Java示例-您能验证至少这对您有效吗?请注意,您的特定查询返回的是ReferencePath对象,因此您需要首先从中提取路径。是的,当我打印出路径的类别和大小时,我得到的信息与你的不同:i/System.out:class org.apache.tinkerpop.gremlin.structure.util.reference.ReferencePath i/System.out:2我想我们当时在同一个波长上!你知道我是怎么做到的吗?不用担心,如果不知道的话,我可以查一些文件?
List<Path> p = g.V("1").out().limit(1).path().toList();
p.get(0).forEach(e -> System.out.println(e));
gremlin> routeList.class
==>class org.apache.tinkerpop.gremlin.structure.util.reference.ReferencePath
gremlin> routeList[0].class
==>class org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceVertex
gremlin> routeList[1].class
==>class org.apache.tinkerpop.gremlin.structure.util.reference.ReferencePath
gremlin> for (v in routeList[1]) {println v}
v[3]
v[8]
v[44]