1
votes

Hi I am new to Tinkerpop and Gremlin and I am struggling with a traversal I am trying to perform.

Basically I have a set of vertex ids which I want to traverse from and find a set of vertices from certain out edges. I want to associate the vertices following the out edges with the veracity it came from. For example: v1 -> e1 -> v2 v1 -> e2 -> v4 v3 -> e3 -> v5 v3 -> e4 -> v6 So v1 has 2 out going edges (e1 and e2) to v2 and v4 and v3 has 2 out going edges (e3 and e4) to v5 and v6. What I want is to get a result of a traversal that would look something like this [[[v1],[v2,v4]],[v3,[v5,v5]]]

Can someone help me with a traversal using Gremlin that would help me get that result? The closest I have come is with something like this graph.traversal().V(4264,40964144).as('people').map{it.get().vertices(OUT, 'codes_in')}.as('languages').union(select('people'), select('languages'))

1

1 Answers

2
votes

Here's one way you can do it demonstrated with the "modern" toy graph:

gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1,4).group().by().by(out().fold())
==>[v[1]:[v[3], v[2], v[4]], v[4]:[v[5], v[3]]]

It just uses group(). The first by() modulator represents the key of each group (in this case the start vertex) and then the second by() modulator is what you want in your group (in this case the out() vertex). Note that you must include fold() in the second by() to force iterate that internal traversal.

By the way, doing graph.traversal() is not a good pattern. Create the TraversalSource once and re-use it.