Gremlin 获取所有传入和传出顶点,包括它们的边缘和方向 [英] Gremlin get all incoming and outgoing vertex, including their edges and directions

查看:22
本文介绍了Gremlin 获取所有传入和传出顶点,包括它们的边缘和方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Gremlin shell 花了一个星期试图编写一个查询获取所有传入和传出的顶点,包括它们的边和方向.所有我都试过了.

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.

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

所以我只想得到 1)所有具有确切名称的顶点、每个顶点的边缘(包括类型 inE 或 outE)以及连接的顶点.理想情况下,在那之后我想获得他们的 map() 以便我获得完整的对象属性.我不关心输出样式,我只需要提供所有信息,这样我就可以在之后对其进行操作.我需要这个来训练我的 Gremlin,但欢迎使用 Neo4j 示例.谢谢!

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!

推荐答案

有多种方法可以解决这个问题.这里有一些想法,希望能启发您找到答案:

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

transform 将传入的顶点转换为 Map 并在输入/输出边上进行内部遍历.您还可以按如下方式使用 path 来获得类似的输出:

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:

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 现已推出,如果您刚刚开始使用,您应该查看提供的最新版本:

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:

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

在 3.0 语法下,您可能会这样做:

Under 3.0 syntax you might do something like this:

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.

更新:上面的查询是根据 Daniel 对 otherV 用法的建议编写的:

UPDATE: Here's the above query written with Daniel's suggestion of otherV usage:

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 的每一块:

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:

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

这篇关于Gremlin 获取所有传入和传出顶点,包括它们的边缘和方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆