Use of by()
Can somebody explain the usecase of by() function in gremlin in very simple language.
Solution:Jump to solution
by()
is a step modulator, meaning it modifies the step it is being applied to in some way by giving it some additional instruction. an easy example to see this with is groupCount()
:
Calling groupCount()
without modulation implies the default behavior of grouping on the incoming traverser (i.e. the current Vertex
). Each Vertex
is simply counted once as a result as they are each unique entities. If we want to change that grouping behavior, we modulate that step with by()
, like:
```gremlin> g.V().groupCount().by(label)...4 Replies
Solution
by()
is a step modulator, meaning it modifies the step it is being applied to in some way by giving it some additional instruction. an easy example to see this with is groupCount()
:
Calling groupCount()
without modulation implies the default behavior of grouping on the incoming traverser (i.e. the current Vertex
). Each Vertex
is simply counted once as a result as they are each unique entities. If we want to change that grouping behavior, we modulate that step with by()
, like:
Now we're saying, go ahead and group count the vertices but use the label of the Vertex
for the grouping. it is important to note that what by()
does is dependent on the step to which it is applied (and that on its own it really doesn't do anything at all).g.V(1).outE().group().by(label).by(inV().fold()).next()
What does this mean??
by()
can be applied to the same step multiple times. how many times and the behavior it implies is again dependent on the step. the first by()
for group()
is the same as what i already presented for groupCount()
:
we group on the Edge
label, so you get a Map
with two keys (i.e. one for each edge label). Now let's add the second by()
:
the second by()
is applied to the values of the Map
. each value (i.e. the list of edges) is given to inV().fold()
and is therefore convert the list of edges into a list of the incoming vertices for each edge. by()
typically grabs the first item returned from the stream, so you have to apply your own reducing operations if you need the entire stream processed - in this case that means adding fold()
.Now it is clear.