java Gremlin 获取所有传入和传出顶点,包括它们的边缘和方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33676566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gremlin get all incoming and outgoing vertex, including their edges and directions
提问by Dmitry Yudin
I spent a week at Gremlin shell trying to compose one query to get all incoming and outgoing vertexes, including their edges and directions. All i tried everything.
我花了一个星期在 Gremlin shell 尝试编写一个查询来获取所有传入和传出的顶点,包括它们的边缘和方向。所有我都试过了。
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{'name':"testname"}]___[ine{edge_name:"nameofincomingedge"}]____[v{name:'nameofconnectedvertex']
[v{'name':"testname"}]___[oute{edge_name:"nameofoutgoingedge"}]____[v{name:'nameofconnectedvertex']
[v{'name':"testname"}]___[oute{edge_name:"nameofoutgoingedge"}]____[v{name:'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. And ideally after that i want to get their map() so i'l get complete object properties. 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. Thanks!
所以我只想得到 1)所有具有确切名称的顶点,每个这个顶点的边缘(包括类型 inE 或 outE),以及连接的顶点。理想情况下,在那之后我想获得他们的 map() 以便我获得完整的对象属性。我不关心输出样式,我只需要提供所有信息,这样我就可以在之后对其进行操作。我需要这个来训练我的 Gremlin,但欢迎使用 Neo4j 示例。谢谢!
回答by stephen mallette
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. You could also use path
as follows to get a similar output:
该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]]]}
I provided these answers using TinkerPop 2.x as that looked like what you were using as judged from the syntax. 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 2.x 提供了这些答案,因为根据语法判断,它看起来像您使用的答案。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]}]