Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 589
michaelSc Avatar asked Sep 09 '11 13:09

michaelSc


People also ask

How do you get the vertex ID Gremlin?

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.

How do you get edges in Gremlins?

To create a new edge between two vertices, use the addEdge(v1, v2, label) method. The edge will be created with the label specified. In the example below two vertices are created and assigned to a variable (Gremlin is based on Groovy), then an edge is created between them.

How do you get rid of the vertex in Gremlins?

If you have the vertex already then simply v. remove() . Any of the 3 will serve.

What is out in Gremlin?

outV Outgoing vertex. inV Incoming vertex. How on earth Incoming vertex = the vertex at the other end of the outgoing edge. If anything, i would think that the vertex at the other end of the outgoing edge if the OutV and not InV.


3 Answers

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

like image 183
grreeenn Avatar answered Sep 21 '22 07:09

grreeenn


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}]
like image 38
stephen mallette Avatar answered Sep 21 '22 07:09

stephen mallette


if implementing with Java use

g.V(1).valueMap().with(WithOptions.tokens).toList()
like image 38
shapan dashore Avatar answered Sep 21 '22 07:09

shapan dashore