michaelSc
michaelSc

Reputation: 91

Get the id + the map of a vertex on Gremlin?

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

Upvotes: 7

Views: 12990

Answers (5)

Tarnished-Coder
Tarnished-Coder

Reputation: 378

if implementing with Java use

g.V(1).valueMap().with(WithOptions.tokens).toList()

Upvotes: 1

grreeenn
grreeenn

Reputation: 2545

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

Upvotes: 13

Nick Grealy
Nick Grealy

Reputation: 25864

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]]

Upvotes: 0

stephen mallette
stephen mallette

Reputation: 46216

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}]

Upvotes: 4

michaelSc
michaelSc

Reputation: 23

I've found a solution

tab = new Table()
g.v(1).as('properties').as('id').table(tab){it.id}{it.map}
tab

Upvotes: 0

Related Questions