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

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

问题描述

我在Gremlin shell上花了一周的时间试图编写一个查询到
获取所有传入和传出的顶点,包括它们的边和方向。

  gV(name,testname)。bothE.as('both')。select ().back('both')。bothV.as('bothV').select(){it.map()} 

$ b $

[v {'name':testname}] ___ [ine {edge_name :nameofincomingedge}] ____ [v {name:'nameofconnectedvertex']



[v {'name':testname}] ___ [oute {edge_name:所以我只想得到1)具有确切名称的所有顶点,每个这个顶点的边(包括其中的顶点)键入inE或outE),并连接顶点。理想情况下,我想获得他们的地图(),所以我会得到完整的对象属性。我不在乎输出风格,我只需要所有的信息,所以我可以在后面操作。我需要这个来训练我的Gremlin,但是Neo4j的例子很受欢迎。谢谢!

解决方案

有很多方法可以解决这个问题。这里有几个想法可以激发你的答案:

  gremlin> g = TinkerGraphFactory.createTinkerGraph()
==> tinkergraph [vertices:6 edges:6]
gremlin> GV( '姓名', '马尔科')变换{[ν:它,INE:it.inE()作为( 'E')OUTV()作为( 'V')选择()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-know-> 2],v:v [2]],[e:e [8] [1-know-> 4],v:v [4]]]}
pre>

transform 将传入顶点转换为 Map 并进行内部遍历输入/输出边缘。您还可以使用 path 来获得类似的输出:

 的gremlin> 。GV( '姓名', '马尔科')变换{[ν:它,INE:it.inE()OUTV()路径()toList()toList(),OUTE:。it.outE()。 inE = [],outE = [[v [1],e [9] [1])inV()。path()。toList()]} 
==> 3],v [3],[v [1],e [7] [1-know-> 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> gV()。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-知道 - > 4],v [4]]

我知道你想知道输出中边缘的方向,但很容易通过分析路径来检测。 / p>

更新:以下是Daniel提供的 otherV 用法的建议:

  gremlin> gV()。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-know-> 4],v [4]]

为了查看数据,你可以使用 by()来区分每个 Path object - 对上述查询的扩展将 valueMap 应用于每个路径

  gremlin>通过(__。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]}]
pre>

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"}]___[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!

解决方案

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:

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:

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

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.

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

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天全站免登陆