Reputation: 111
I can see this has been asked a few times but i couldn't see an answer that works for me.
I am using this in a Java application and i want to return an ArrayList of either vertices or just of Strings containing the vertex iD. I have to use the Path() step as i need the vertices returned in the correct order, however i get just a List of having 2 Objects, what seems like the source Vertex as an Object and the whole Path as an Object, I can not manipulate or use the data?
My traversal is:
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();
The output of the list is:
v[L00-041]
path[v[L00-041], v[L00-042], ...etc.... v[L00-043], v[L00-031]]
I really need to be returning the path in a usable format. I am sure someone has been in this position before and can help me out. Thanks.
Upvotes: 1
Views: 1506
Reputation: 14371
In general for path() queries Gremlin will return a Path object to you that you can iterate through. Here is an example from the Gremlin Console.
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]
The relevant JavaDoc is here:
Using Java you can do something like this.
List<Path> p = g.V("1").out().limit(1).path().toList();
p.get(0).forEach(e -> System.out.println(e));
However, when working with the shortestPath() step the returned result is a ReferencePath that contains the starting vertex and the path that was walked. You can access it from the console as follows and can also do similar to the example above from Java. Like Path, ReferencePath is iterable.
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]
Here is the JavaDoc for ReferencePath https://tinkerpop.apache.org/javadocs/current/full/org/apache/tinkerpop/gremlin/structure/util/reference/ReferencePath.html
Upvotes: 1