简体   繁体   English

Gremlin获取所有传入和传出的顶点,包括它们的边缘和方向

[英]Gremlin get all incoming and outgoing vertex, including their edges and directions

I spent a week at Gremlin shell trying to compose one query to get all incoming and outgoing vertexes, including their edges and directions. 我在Gremlin shell上花了一个星期试图编写一个查询来获取所有传入和传出的顶点,包括它们的边缘和方向。 All i tried everything. 我尝试了一切。

g.V("name","testname").bothE.as('both').select().back('both').bothV.as('bothV').select(){it.map()}

output i need is (just example structure ): 我需要的输出是(只是示例结构):

[v{'name':"testname"}]___[ine{edge_name:"nameofincomingedge"}]____[v{name:'nameofconnectedvertex'] [V { '名称': “测试名”}] ___ [啉{edge_name: “nameofincomingedge”}] ____ [V {名称: 'nameofconnectedvertex']

[v{'name':"testname"}]___[oute{edge_name:"nameofoutgoingedge"}]____[v{name:'nameofconnectedvertex'] [V { '名称': “测试名”}] ___ [欧特{edge_name: “nameofoutgoingedge”}] ____ [V {名称: 'nameofconnectedvertex']

So i just whant to get 1) all Vertices with exact name , edge of each this vertex (including type inE or outE), and connected Vertex. 所以我只想得到1)具有确切名称的所有顶点,每个顶点的边缘(包括inE或outE类型)和连接的Vertex。 And ideally after that i want to get their map() so i'l get complete object properties. 理想情况下,我想得到他们的map()所以我得到完整的对象属性。 i dont care about the output style, i just need all of information present, so i can manipulate with it after. 我不关心输出风格,我只需要所有信息,所以我可以用它来操纵它。 I need this to train my Gremlin, but Neo4j examples are welcome. 我需要这个训练我的Gremlin,但欢迎Neo4j的例子。 Thanks! 谢谢!

There's a variety of ways to approach this. 有很多方法可以解决这个问题。 Here's a few ideas that will hopefully inspire you to an answer: 这里有一些想法可以激发你的答案:

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

The transform converts the incoming vertex to a Map and does internal traversal over in/out edges. transform将传入的顶点转换为Map ,并在输入/输出边缘进行内部遍历。 You could also use path as follows to get a similar output: 您还可以使用如下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]]]}

I provided these answers using TinkerPop 2.x as that looked like what you were using as judged from the syntax. 我使用TinkerPop 2.x提供了这些答案,因为从语法判断看起来就像你使用的那样。 TinkerPop 3.x is now available and if you are just getting started, you should take a look at the latest that has to offer: TinkerPop 3.x现已推出,如果您刚刚开始使用,您应该看一下最新提供的产品:

http://tinkerpop.incubator.apache.org/ http://tinkerpop.incubator.apache.org/

Under 3.0 syntax you might do something like this: 在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]]

I know that you wanted to know what the direction of the edge in the output but that's easy enough to detect on analysis of the path. 我知道你想知道输出中边缘的方向,但是很容易在分析路径时检测到。

UPDATE: Here's the above query written with Daniel's suggestion of otherV usage: 更新:以上是用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]]

To see the data from this you can use by() to pick apart each Path object - The extension to the above query applies valueMap to each piece of each Path : 要查看此数据,您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM