I need to write a gremlin query that can sum 2 or more properties on a set of vertices and return them as separate values.
Statement 1:
g.V().has('label1').values('p1').sum()
This will return the sum of the p1 values of all the 'label1' vertices - let's say that's 100
Statement 2:
g.V().has('label1').values('p2').sum()
This will return the sum of the p2 values of all the 'label1' vertices - let's say that's 200
Statement 3:
g.V().has('label1').values('p1','p2').sum()
Tried the above statement, but as expected that doesn't work, it will return 300, the sum of all p1, and p2 properties of 'label1'
I need a query that will return the sum of p1, and the sum p2 in one result. Maybe there's a way I could "collapse" or fold a set of vertices into one vertex and aggregate properties by certain rules... in my case for sum.
You can group all values by their key:
g.V().has("label1").properties("p1","p2").
group().by(key).by(value().sum())
EDIT
Proof that it works:
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV().property("p1", 1).property("p2", 10).iterate()
gremlin> g.addV().property("p1", 2).property("p2", 20).iterate()
gremlin> g.V().properties("p1", "p2").group().by(key).by(value().sum())
==>[p1:3,p2:30]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With