Srinath Ganesh
Srinath Ganesh

Reputation: 2558

Printing/Fetching Vertex values from a path

Just getting started with gremlin.

Printing out all the Vertex values worked out fine

gremlin> g.V().values()
==>testing 2
==>Cash Processing
==>Sales
==>Marketing
==>Accounting

I was able to find all the directly connected path between my Vertices.

gremlin> g.V().hasLabel('Process')
.repeat(both().simplePath())
.until(hasLabel('Process'))
.dedup().path()
==>[v[25],v[28]]
==>[v[25],v[26]]
==>[v[26],v[27]]
==>[v[26],v[25]]

Now am trying to print out the values in the path like ['Sales', 'Accounting'] instead of [v[25],v[28]]

Not been able to figure out a way yet


Already tried and failed with

  1. Unfold: Does not get me 1-1 mapping

    gremlin> g.V().hasLabel('Process').repeat(both().simplePath()).until(hasLabel('Process')).dedup().path().unfold().values() ==>Cash Processing ==>Accounting ==>Cash Processing ==>Sales ==>Sales ==>Marketing ==>Sales ==>Cash Processing

  2. Path seems to be of a different data-type and does not support .values() function

    gremlin> g.V().hasLabel('Process') .repeat(both().simplePath()) .until(hasLabel('Process')) .dedup().path().values()

org.apache.tinkerpop.gremlin.process.traversal.step.util.ImmutablePath cannot be cast to org.apache.tinkerpop.gremlin.structure.Element

  1. Tried the following google searches and didnt get the answer

  2. Found one at here that was for java but that didnt work for me

    l = []; g.V().....path().fill(l)

(but cant create list, Cannot set readonly property: list for class: org.apache.tinkerpop.gremlin.structure.VertexProperty$Cardinality )


I have running it on Gremlin console (running ./gremlin.sh)

Upvotes: 1

Views: 2336

Answers (1)

Florian Hockmann
Florian Hockmann

Reputation: 2809

You can use the by step to modulate the elements inside the path. For example by supplying valueMap(true) to by you get the properties of the vertices, together with the vertex labels and their ids:

gremlin> g.V().repeat(both().simplePath()).times(1).dedup().path().by(valueMap(true))
==>[[id:1,name:[marko],label:person,age:[29]],[id:3,name:[lop],lang:[java],label:software]]
==>[[id:1,name:[marko],label:person,age:[29]],[id:2,name:[vadas],label:person,age:[27]]]
==>[[id:1,name:[marko],label:person,age:[29]],[id:4,name:[josh],label:person,age:[32]]]
==>[[id:2,name:[vadas],label:person,age:[27]],[id:1,name:[marko],label:person,age:[29]]]
==>[[id:3,name:[lop],lang:[java],label:software],[id:6,name:[peter],label:person,age:[35]]]
==>[[id:4,name:[josh],label:person,age:[32]],[id:5,name:[ripple],lang:[java],label:software]]

I used the modern graph which is one of TinkerPop's toy graphs that are often used for such examples. Your output will look a bit different and you may want to use something else than valueMap(true) for the by modulator. The TinkerPop documentation of the path step itself contains two more advanced examples for path().by() that you might want to check out.

Upvotes: 5

Related Questions