Printing/Fetching Vertex values from a path

浪子不回头ぞ 提交于 2019-12-11 06:06:00

问题


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

    • gremlin print a path
    • gremlin get values in a path
    • and few more word twisting
  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)


回答1:


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.



来源:https://stackoverflow.com/questions/49212299/printing-fetching-vertex-values-from-a-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!