英文:
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("test").or(
__.out("membership").hasLabel("location"),
__.out("composition").hasLabel("aws_vpc"))
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("membership").hasLabel("location")
或 __.out("composition").hasLabel("aws_vpc")
,就会立即满足条件,因此您甚至不会遍历所有这些路径(对于过滤操作来说,这是一件好事)。
如果您想要返回您描述的所有数据,您需要以一种方式编写查询,以便遍历所有数据并将其转换为要返回的格式。在您的情况下,一个简单的方法是使用 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("membership").hasLabel("location")
or __.out("composition").hasLabel("aws_vpc")
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('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())
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论