[英]Gremlin get all incoming and outgoing vertex, including their edges and directions
我在Gremlin shell上花了一個星期試圖編寫一個查詢來獲取所有傳入和傳出的頂點,包括它們的邊緣和方向。 我嘗試了一切。
g.V("name","testname").bothE.as('both').select().back('both').bothV.as('bothV').select(){it.map()}
我需要的輸出是(只是示例結構):
[V { '名稱': “測試名”}] ___ [啉{edge_name: “nameofincomingedge”}] ____ [V {名稱: 'nameofconnectedvertex']
[V { '名稱': “測試名”}] ___ [歐特{edge_name: “nameofoutgoingedge”}] ____ [V {名稱: 'nameofconnectedvertex']
所以我只想得到1)具有確切名稱的所有頂點,每個頂點的邊緣(包括inE或outE類型)和連接的Vertex。 理想情況下,我想得到他們的map()所以我得到完整的對象屬性。 我不關心輸出風格,我只需要所有信息,所以我可以用它來操縱它。 我需要這個訓練我的Gremlin,但歡迎Neo4j的例子。 謝謝!
有很多方法可以解決這個問題。 這里有一些想法可以激發你的答案:
gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> g.V('name','marko').transform{[v:it,inE:it.inE().as('e').outV().as('v').select().toList(),outE:it.outE().as('e').inV().as('v').select().toList()]}
==>{v=v[1], inE=[], outE=[[e:e[9][1-created->3], v:v[3]], [e:e[7][1-knows->2], v:v[2]], [e:e[8][1-knows->4], v:v[4]]]}
transform
將傳入的頂點轉換為Map
,並在輸入/輸出邊緣進行內部遍歷。 您還可以使用如下path
獲取類似的輸出:
gremlin> g.V('name','marko').transform{[v:it,inE:it.inE().outV().path().toList().toList(),outE:it.outE().inV().path().toList()]}
==>{v=v[1], inE=[], outE=[[v[1], e[9][1-created->3], v[3]], [v[1], e[7][1-knows->2], v[2]], [v[1], e[8][1-knows->4], v[4]]]}
我使用TinkerPop 2.x提供了這些答案,因為從語法判斷看起來就像你使用的那樣。 TinkerPop 3.x現已推出,如果您剛剛開始使用,您應該看一下最新提供的產品:
http://tinkerpop.incubator.apache.org/
在3.0語法下你可能會做這樣的事情:
gremlin> g.V().has('name','marko').as('a').bothE().bothV().where(neq('a')).path()
==>[v[1], e[9][1-created->3], v[3]]
==>[v[1], e[7][1-knows->2], v[2]]
==>[v[1], e[8][1-knows->4], v[4]]
我知道你想知道輸出中邊緣的方向,但是很容易在分析路徑時檢測到。
更新:以上是用Daniel建議使用otherV
的上述查詢:
gremlin> g.V().has('name','marko').bothE().otherV().path()
==>[v[1], e[9][1-created->3], v[3]]
==>[v[1], e[7][1-knows->2], v[2]]
==>[v[1], e[8][1-knows->4], v[4]]
要查看此數據,您可以使用by()
來分隔每個Path
對象 - 上述查詢的擴展將valueMap
應用於每個Path
每個部分:
gremlin> g.V().has('name','marko').bothE().otherV().path().by(__.valueMap(true))
==>[{label=person, name=[marko], id=1, age=[29]}, {label=created, weight=0.4, id=9}, {label=software, name=[lop], id=3, lang=[java]}]
==>[{label=person, name=[marko], id=1, age=[29]}, {label=knows, weight=0.5, id=7}, {label=person, name=[vadas], id=2, age=[27]}]
==>[{label=person, name=[marko], id=1, age=[29]}, {label=knows, weight=1.0, id=8}, {label=person, name=[josh], id=4, age=[32]}]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.