Get the id + the map of a vertex on Gremlin?
Asked Answered
V

5

7

g.v(1).id

gives me vertex 1 id,

g.v(1).map

gives me vertex 1 properties.

But, how can I get a hash with id and propeties at the same time

Vella answered 9/9, 2011 at 13:23 Comment(0)
B
13

I know that it's an old question - so answers below will work on older versions of TinkerPop (3<); just if anyone (like me) stumbles upon this question and looks for a solution that works on TinkerPop 3 - the same result can be achieved by calling valueMap with 'true' argument, like this:

gremlin> g.v(1).valueMap(true)

reference may be found in docs here

Biles answered 17/3, 2017 at 23:47 Comment(1)
This has been deprecated. With the 3.4 release (see Upgrade Notes), you should use gremlin> g.v(1).valueMap().with(WithOptions.tokens)Ankylostomiasis
T
4

As of Gremlin 2.4.0 you can also do something like:

gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> g.v(1).out.map('name','age','id') 
==>{id=2, age=27, name=vadas}
==>{id=4, age=32, name=josh}
==>{id=3, age=null, name=lop}

Another alternative using transform():

gremlin> g.v(1).out.transform{[it.id,it.map()]}
==>[2, {age=27, name=vadas}]
==>[4, {age=32, name=josh}]
==>[3, {name=lop, lang=java}]
Tomfool answered 17/8, 2013 at 14:7 Comment(0)
C
1

if implementing with Java use

g.V(1).valueMap().with(WithOptions.tokens).toList()
Custer answered 13/11, 2019 at 16:37 Comment(0)
V
0

I've found a solution

tab = new Table()
g.v(1).as('properties').as('id').table(tab){it.id}{it.map}
tab
Vella answered 15/9, 2011 at 15:12 Comment(0)
A
0

Just extending on @Stephen's answer; to get the id and the map() output in a nice single Map for each Vertex, just use the plus or leftShift Map operations in the transform method.

Disclaimer: I'm using groovy, I haven't been able to test it in gremlin (I imagine it's exactly the same).

Groovy Code

println "==>" + g.v(1).out.transform{[id: it.id] + it.map()}.asList()

or

println "==>" + g.v(1).out.transform{[id: it.id] << it.map()}.asList()

Gives

==>[[id:2, age:27, name:vadas], [id:4, age:32, name:josh], [id:3, name:lop, lang:java]]
Alsacelorraine answered 13/4, 2015 at 4:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.