Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of store and select keywords in gremlin

Im new to gremlin. i followed the document on tinkerpop3. But i couldn't understand the use of store and select keywords.. Can anyone explain it simply?

like image 998
vineeth Avatar asked Mar 08 '23 12:03

vineeth


1 Answers

The store() step collects objects that pass through the traversal as a side-effect of the traversal. For example let's say I have this traversal (using the modern toy graph packaged in TinkerPop):

gremlin> g.V().has("name","marko").store("markos").outE()
==>e[9][1-created->3]
==>e[7][1-knows->2]
==>e[8][1-knows->4]

The traversal gets all the vertices named "marko" stores them in a list called "markos" and then traverse on their out edges with outE() and we get edges as the output. As an additional point, if we want to get that "markos" list out of our traversal we must cap() the traversal:

gremlin> g.V().has("name","marko").store("markos").outE().cap('markos')
==>[v[1]]

The store() step is a bit useless in this context, but imagine using it to tell Gremlin to collect things for you as he does his walk around the graph. Perhaps you could tell him to collect a bunch of edges for example:

gremlin> g.V().has("name","marko").outE().store("edges").inV().outE().store('edges').inV().cap('edges')
==>[e[9][1-created->3],e[7][1-knows->2],e[8][1-knows->4],e[10][4-created->5],e[11][4-created->3]]

You can also reference the list gathered by store() via via steps that allow access to side-effects. A common use case might be to ignore something already traversed:

gremlin> g.V().has("name","marko").store('marko').out().in()
==>v[1]
==>v[4]
==>v[6]
==>v[1]
==>v[1]
gremlin> g.V().has("name","marko").store('marko').out().in().where(without('marko'))
==>v[4]
==>v[6]

It is important to recall that store() is lazy in its work. It collects as the traversal executes. If you need the opposite of that then you should go with aggregate() which is eager in its evaluation (it exhausts the traversal to the point that aggregate() is called).

The select() keyword lets you access previous parts of a traversal and to pick apart maps. With respect to the former, you might need to access labelled steps:

gremlin> g.V().has("name","marko").as('a').outE().select('a')
==>v[1]
==>v[1]
==>v[1]
gremlin> g.V().has("name","marko").as('a').outE().inV().as('b').select('a','b')
==>[a:v[1],b:v[3]]
==>[a:v[1],b:v[2]]
==>[a:v[1],b:v[4]]

and as to the latter:

gremlin> g.V().has('name','marko').valueMap().select('name')
==>[marko]
gremlin> g.V().has('name','marko').valueMap().select(values)
==>[[marko],[29]]

I'm not sure that there are specific use cases for when you need select as it is generic sort of step that has wide applicability.

like image 132
stephen mallette Avatar answered Mar 10 '23 02:03

stephen mallette