Math operation on multiple node properties without using Lambda

731 views
Skip to first unread message

freakin...@gmail.com

unread,
Aug 26, 2018, 4:16:14 AM8/26/18
to Gremlin-users
Hi guys,

I'm working on a gremlin query which would require calculating a value based on multiple properties of the selected vertices. E.g. the vertices at the end of my traversal all contain property A and B, and I'd like to calculate C = A+B for all selected vertices.

This should be trivial if we can use lambda, but since I'm using Neptune which doesn't support lambda, what's the best way, or if it's possible to perform this kind of calculation based on different properties in Neptune?

Thanks.

HadoopMarc

unread,
Aug 26, 2018, 10:01:20 AM8/26/18
to Gremlin-users
The following will do for your current usecase:

gremlin> g=TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin
> g.V(1).property('period',5)
==>v[1]
gremlin
> g.V(2).property('period',5)
==>v[2]
gremlin
> g.V().has('age').has('period').valueMap(true)
==>[period:[5],label:person,name:[marko],id:1,age:[29]]
==>[period:[5],label:person,name:[vadas],id:2,age:[27]]
gremlin
> g.V().has('age').has('period').map(values('age', 'period').sum())
==>34
==>32
gremlin
> g.V().has('age').has('period').project('a').by(values('age', 'period').sum())
==>[a:34]
==>[a:32]


In TinkerPop 3.3.x you also have the Math() step for more general math expressions than just sum(), but I guess this is not available in Neptune yet.

Cheers,     Marc


Op zondag 26 augustus 2018 10:16:14 UTC+2 schreef freakin...@gmail.com:

Olav Laudy

unread,
Aug 26, 2018, 1:58:06 PM8/26/18
to gremli...@googlegroups.com
The math() step IS available in Neptune!  It wasn't during the trial period, but they upgraded.  



--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/658f1d05-0ed1-4518-8c2d-9115eac96a93%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Kuppitz

unread,
Aug 26, 2018, 6:32:46 PM8/26/18
to gremli...@googlegroups.com
The syntax for math() is a bit weird in this case as you have to use a step label which otherwise wouldn't be necessary:

g.V(1,2).as("a").math("_+a").by("age").by("period")

Anyway, a third solution would be good old sack():

g.V(1,2).sack(assign).by('age').sack(sum).by('period').sack()

I ran some simple profile() tests and the differences between each method were negligible, but sack() always outperformed the other two by a few nanos. Now, given that you can pretty much ignore the performance factor, I think you should use whatever you think is most readable.

Cheers,
Daniel


Freaking Red

unread,
Aug 27, 2018, 1:13:07 PM8/27/18
to gremli...@googlegroups.com
Thanks guys for the response!

Turns out, one of the math operation I need is multiplication, so I came up with something like g.V().local(values("age", "period").fold(1, mult)) not sure how efficient it is, but math() definitely would be more versatile and I'll try it out~

Reply all
Reply to author
Forward
0 new messages