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
spmallette9mo 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
Faraz9mo ago
g.V(1).outE().group().by(label).by(inV().fold()).next() What does this mean??
spmallette
spmallette9mo 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
Faraz9mo ago
Now it is clear.
Want results from more Discord servers?
Add your server
More Posts
@GremlinDSL support in the GremlinLangScriptEngineHi, I recently sent a pull-request into the github ArcadeDB repository to add support binding custoRepeatStep does not appear to respect barriersI was digging into some traversal performance and had something similar to the following: ``` g.V(<Trying to find a Vertex using a variable injected earlier in the traversalI am trying to add a series of vertices and edges to an existing graph. The newly created Vertex wiDoes Gremlin support API for CRUD operations?Currently using g.V() for read and g.addV() for write.Individual Vertex per property or Vertex with grouped propertiesI'm building an identity graph that also stores User profile data - things like email address, phoneFilter out empty resultsgremlin> g.V().hasLabel('metadata').valueMap() ==>{} ==>{} ==>{oncall_roster=[oncall_schedule]} HowQuestion on running queries in windows env.I get an error `RuntimeError: Event loop is closed`, but after troubleshooting I notice that my scriPropertiesStep.hashcode() not always uniqueAs background... We're working with Gremlin (groovy) to write queries against an in memory graph modConditionally update one vertex property when another property matches a certain provided valuehttps://stackoverflow.com/questions/76971695/update-vertex-properties-when-property-a-matches-properTrying to run a local version for a test, what is the correct serializer?Windows machine, local host. I can't find the Running this, https://github.com/bricaud/gremlin-serv