如何打印出 Gremlin 遍历的所有结果

huangapple go评论70阅读模式
英文:

how to print all result from Gremlin traversal

问题

Sure, here's the translated content:

我有一个拓扑,它是
aws_vpc<------(组成)------test---(隶属)---->location

因此我使用以下查询

graph.traversal().V().hasLabel("test").or(
                        __.out("隶属").hasLabel("location"),
                        __.out("组成").hasLabel("aws_vpc"))

来选择它,但如何打印出所有元素的名称,
我希望输出:test,隶属,location,组成,aws_vpc。

有办法实现这个吗?

英文:

I have a topology, it is
aws_vpc<------(composition)------test---(membership)---->location

So I use query

graph.traversal().V().hasLabel(&quot;test&quot;).or(
                    __.out(&quot;membership&quot;).hasLabel(&quot;location&quot;),
                    __.out(&quot;composition&quot;).hasLabel(&quot;aws_vpc&quot;))

to select it, but how to print all elements' name,
I want to output : test, membership, location,composition, aws_vpc.

Is there a way to achieve this?

答案1

得分: 1

你编写了一个遍历,仅检测“test”顶点是否具有传出的与“location”顶点相邻的“membership”边缘,或具有传出的与“aws_vpc”顶点相邻的“composition”边缘,因此该遍历将仅返回与该筛选条件匹配的“test”顶点。它不会比此更多地“选择”任何内容。实际上,or() 一旦在按照它们提供给 or() 的顺序返回单个 __.out(&quot;membership&quot;).hasLabel(&quot;location&quot;)__.out(&quot;composition&quot;).hasLabel(&quot;aws_vpc&quot;),就会立即满足条件,因此您甚至不会遍历所有这些路径(对于过滤操作来说,这是一件好事)。

如果您想要返回您描述的所有数据,您需要以一种方式编写查询,以便遍历所有数据并将其转换为要返回的格式。在您的情况下,一个简单的方法是使用 project()

g.V().hasLabel('test').
  project('data','memberships', 'compositions').
    by(__.elementMap()).
    by(__.outE("membership").as('e').
          inV().hasLabel("location").as('v').
          select('e','v').
            by(elementMap()).
          fold()).
    by(__.outE("composition").as('e').
          inV().hasLabel("aws_vpc").as('v').
          select('e','v').
            by(elementMap()).
          fold())

这将对每个“test”顶点进行转换,并将其转换为具有三个键的 Map:"data","memberships" 和 "competitions",然后每个 by() 调节器指定了对正在转换的当前“test”顶点的处理方式,并将其放入相应的键中。请注意,我选择了 select() 来获取边缘和顶点的组合,但如果您愿意,这也可以是 project() 步骤。关键是要以 fold() 结束,以便将每个“test”顶点的边缘数据流减少为可以放入相关的“memberships”和“compositions”键中的数据列表。

英文:

You've written a traversal that only detects if "test" vertices have outgoing "membership" edges that have adjacent "location" vertices OR outgoing "composition" edges that have adjacent "aws_vpc" vertices, so all that traversal will return is "test" vertices that match that filter. It does not "select" anything more than that. In fact, the or() is immediately satisfied as soon as a single __.out(&quot;membership&quot;).hasLabel(&quot;location&quot;) or __.out(&quot;composition&quot;).hasLabel(&quot;aws_vpc&quot;) is returned in the order they are provided to or() so you don't even traverse all of those paths (which is a good thing for a filtering operation).

If you want to return all of the data you describe, you need to write your query in such a way so as to traverse it all and transform it into a format to return. A simple way to do this in your case would be to use project():

g.V().hasLabel(&#39;test&#39;).
  project(&#39;data&#39;,&#39;memberships&#39;, &#39;compositions&#39;).
    by(__.elementMap()).
    by(__.outE(&quot;membership&quot;).as(&#39;e&#39;).
          inV().hasLabel(&quot;location&quot;).as(&#39;v&#39;).
          select(&#39;e&#39;,&#39;v&#39;).
            by(elementMap()).
          fold()).
    by(__.outE(&quot;composition&quot;).as(&#39;e&#39;).
          inV().hasLabel(&quot;aws_vpc&quot;).as(&#39;v&#39;).
          select(&#39;e&#39;,&#39;v&#39;).
            by(elementMap()).
          fold())

This takes each "test" vertex and transforms it to a Map with three keys: "data", "memberships" and "competitions" and then each by() modulator specifies what to do with the current "test" vertex being transformed and places it in the respective key. Note that I chose select() to get the edge and vertex combinations but that could have been a project() step as well if you liked. The key there is to end with fold() so that you reduce the stream of edge data for each "test" vertex to a List of data that can put in the related "memberships" and "compositions" keys.

huangapple
  • 本文由 发表于 2020年9月17日 19:08:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63936749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定