简体   繁体   English

TinkerPop3 Gremlin遍历

[英]TinkerPop3 Gremlin traversal

在此处输入图片说明

First, Given a Node A, first we need to find out all nodes that could be reached through the "Friend" edges(A,B,C... all the blue nodes). 首先,给定一个节点A,首先我们需要找出可以通过“朋友”边缘到达的所有节点(A,B,C ...所有蓝色节点)。 I can achieve this by using the path()-step like bellow: 我可以通过使用下面的path()步骤来实现这一点:

 g.V().hasLabel("Person").has("name", "A")
                            .repeat(out("Friend"))
                            .until(out("Friend").count().is(0))
                            .path();

And then we can extract verticals from the path objects in java code. 然后,我们可以使用Java代码从路径对象中提取垂直对象。

But actually, we need to find out what books they read(the green ones). 但是实际上,我们需要找出他们读了哪些书(绿色的)。 But we can't extract the verticals from path() in gremlin. 但是我们不能从gremlin中的path()中提取垂直方向。

Is there any way we could do this in one gremlin traversal? 我们有什么办法可以一次遍历gremlin吗?


Edit: In fact, there is 2 situations: 编辑:实际上,有2种情况:

  1. from A, find out all persons connected through "Friend" edge. 从A中找出所有通过“朋友”边缘连接的人。 We did this by the traversal mentioned above. 我们通过上面提到的遍历来做到这一点。 (is there any better way? ie. extract these nodes directly in gremlin?) (是否有更好的方法?即直接在gremlin中提取这些节点?)

  2. from A, find out all persons then all books they read, and return the books, only the books. 从A中找出所有人,然后找出他们阅读的所有书籍,然后只归还书籍。

You could use the union step. 您可以使用并集步骤。

g.V()
 .hasLabel("person")
 .has("name", "A")
 .repeat(union(out("friend"),
               out("read")))
 .until(out("friend").count().is(0))
 .union(path(),
        out("read").path())

Should get you what you need. 应该可以满足您的需求。 With a graph defined as 图定义为

gremlin> :> graph.addVertex(T.label, "person", "name", "A")
==>v[0]
gremlin> :> graph.addVertex(T.label, "person", "name", "B")
==>v[2]
gremlin> :> graph.addVertex(T.label, "person", "name", "C")
==>v[4]
gremlin> :> graph.addVertex(T.label, "person", "name", "D")
==>v[6]
gremlin> :> a = g.V(0).next(); b = g.V(2).next(); a.addEdge("friend", b, "is", "is");
==>e[8][0-friend->2]
gremlin> :> b = g.V(2).next(); c = g.V(4).next(); a.addEdge("friend", b, "is", "is");
==>e[9][2-friend->4]
gremlin> :> a = g.V(0).next(); d = g.V(6).next(); a.addEdge("friend", b, "is", "is");
==>e[10][0-friend->6]
gremlin> :> graph.addVertex(T.label, "book", "name", "Huck Finn")
==>v[11]
gremlin> :> graph.addVertex(T.label, "book", "name", "Tom Sawyer")
==>v[13]
gremlin> :> graph.addVertex(T.label, "book", "name", "A Tale Of Two Cities")
==>v[15]
gremlin> :> a = g.V(0).next(); b = g.V(11).next(); a.addEdge("read", b, "is", "is");
==>e[17][0-read->11]
gremlin> :> a = g.V(2).next(); b = g.V(13).next(); a.addEdge("read", b, "is", "is");
==>e[18][2-read->13]
gremlin> :> a = g.V(4).next(); b = g.V(15).next(); a.addEdge("read", b, "is", "is");
==>e[19][4-read->15]

It yields 它产生

gremlin> :> g.V().hasLabel("person").has("name", "A").repeat(union(out("friend"), out("read"))).until(out("friend").count().is(0)).union(path(), out("read").path())
==>[v[0], v[6]]
==>[v[0], v[11]]
==>[v[0], v[2], v[4]]
==>[v[0], v[2], v[4], v[15]]
==>[v[0], v[2], v[13]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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