Use of by()

Can somebody explain the usecase of by() function in gremlin in very simple language.
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():
gremlin> g.V().groupCount()
==>[v[1]:1,v[2]:1,v[3]:1,v[4]:1,v[5]:1,v[6]:1]
gremlin> g.V().groupCount()
==>[v[1]:1,v[2]:1,v[3]:1,v[4]:1,v[5]:1,v[6]:1]
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)...
Jump to solution
4 Replies
Solution
spmallette
spmallette16mo ago
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():
gremlin> g.V().groupCount()
==>[v[1]:1,v[2]:1,v[3]:1,v[4]:1,v[5]:1,v[6]:1]
gremlin> g.V().groupCount()
==>[v[1]:1,v[2]:1,v[3]:1,v[4]:1,v[5]:1,v[6]:1]
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)
==>[software:2,person:4]
gremlin> g.V().groupCount().by(label)
==>[software:2,person:4]
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).
Faraz
FarazOP16mo ago
g.V(1).outE().group().by(label).by(inV().fold()).next() What does this mean??
spmallette
spmallette16mo ago
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():
gremlin> g.V(1).outE().group().by(label)
==>[created:[e[9][1-created->3]],knows:[e[7][1-knows->2],e[8][1-knows->4]]]
gremlin> g.V(1).outE().group().by(label)
==>[created:[e[9][1-created->3]],knows:[e[7][1-knows->2],e[8][1-knows->4]]]
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():
gremlin> g.V(1).outE().group().by(label).by(inV().fold())
==>[created:[v[3]],knows:[v[2],v[4]]]
gremlin> g.V(1).outE().group().by(label).by(inV().fold())
==>[created:[v[3]],knows:[v[2],v[4]]]
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().
Faraz
FarazOP16mo ago
Now it is clear.
Want results from more Discord servers?
Add your server