Gopi Gopi - 2 months ago 13
Scala Question

How to get all vertices of all outgoing edges from a vertex scala gremlin

I need to get all list of vertices label of all outgoing egdes from a vertex using scala gremlin.

My code looks like below,

val names :ListBuffer[String] = ListBuffer()
val toList: List[Vertex] = graph.V().hasLabel(100).outE().outV().toList()
for(vertex <- toList){
names += vertex.label()
}


Its returning the same label name for all vertex
Eg :
Vertex A is having outE to B,C,D . It returns the label of A.
Output:

ListBuffer(100, 100, 100)


Anything am i missing?

Answer

I believe you asking for the wrong vertex in the end. Honestly, I often make the same mistake. Maybe this is the traversal you looking for:

graph.V().hasLabel(100).outE().inV().label().toList()

If you like me and often get confused by inV() and outV() you can use otherV which gets the opposite vertex. Like so:

graph.V().hasLabel(100).outE().otherV().label().toList()

Finally you can even shorten your traversal by not explicitly stating the edge part:

graph.V().hasLabel(100).out().label().toList()

By using out() instead of outE() you don't need to specify you want the vertex, out() gets you the vertex directly.

Comments