简体   繁体   English

AWS 海王星上的 gremlin 遍历

[英]gremlin traversal on AWS neptune

I have a graph structure like this-我有一个像这样的图形结构-

Node1(Console)   <----Uses---  Node2(Name, Age) -----plays----> Node3(Game)
 

So, i have three nodes -所以,我有三个节点 -

Node1 is console like a PS3/ Nintendo. Node1 是一个类似于 PS3/Nintendo 的控制台。

Node2 is a person with properties name and Age. Node2 是具有属性 name 和 Age 的人。

Node3 is game node which holds game name like 'warcraft'. Node3 是游戏节点,其游戏名称如“魔兽”。

Now I want to have a gremlin query which tells me that.现在我想要一个 gremlin 查询来告诉我这一点。 How many users (Node2) who has the console like PS3, plays a game like 'warcraft'有多少用户(Node2)拥有像 PS3 这样的控制台,玩像“魔兽”这样的游戏

I think i need to start the traverse from Node2, filter it based on Node1 property ie console as as 'PS3' and plays game like 'warcraft'我想我需要从 Node2 开始遍历,根据 Node1 属性过滤它,即控制台为“PS3”并玩“魔兽”之类的游戏

I am new to gremlin and using some thing like this -我是 gremlin 的新手并使用这样的东西 -

g.V().hasLabel('user').outE('uses').inV().has('console', 'ps3').count()

the above query only answers half of my required result.上面的查询只回答了我所需结果的一半。 How do i filter Node2 based on plays relationship as well.我如何也根据播放关系过滤 Node2。

Any help is appreciated.任何帮助表示赞赏。

There are multiple ways to write the query.有多种方法可以编写查询。

Option 1: Start from console选项 1:从控制台开始

gV().has('console', 'ps3').in('uses').where(out('plays').has('game', 'warcraft')).valueMap('name')

Let me explain the structure here:让我在这里解释一下结构:

gV().has('console', 'ps3') --> Select all vertices which have a property with key as console and value as ps3 gV().has('console', 'ps3') --> Select 所有具有键为console且值为ps3的属性的顶点

in('uses') --> From the set of previous vertices, jump to incoming vertices via an edge that has the label uses . in('uses') --> 从先前的顶点集合中,通过具有uses的边跳转到传入顶点。 At this stage, we would have player vertices in our solution.在这个阶段,我们的解决方案中会有玩家顶点。

where(out('plays').has('game', 'warcraft')) --> Apply a filter on existing solutions. where(out('plays').has('game', 'warcraft')) --> 对现有解决方案应用过滤器。 Since we are using where we would not jump/traverse to the next step of vertices.由于我们使用的where我们不会跳转/遍历到下一步顶点的地方。

valueMap('name') --> Project one or more properties if existing solutions which are player vertices. valueMap('name') --> 如果现有解决方案是玩家顶点,则投影一个或多个属性。

Option 2: Another way to write above query选项 2:编写上述查询的另一种方式

gV().has('console', 'ps3').in('uses').as('myusers').out('plays').has('game', 'warcraft').select('myusers').by('name')

as('myusers') --> Provides a reference/alias to the vertices at this stage. as('myusers') --> 在此阶段为顶点提供引用/别名。 Note that it does not store all the results at this stage instead it just provides a reference to the type of vertices at this point in the query.请注意,它不会存储此阶段的所有结果,而只是提供对查询中此时顶点类型的引用。

out('plays').has('game', 'warcraft') --> Unlike previous time when we did not jump since we were using where , this time we jump onto the game vertices. out('plays').has('game', 'warcraft') --> 与上一次我们使用where后没有跳转不同,这次我们跳转到game顶点。

select('myusers').by('name') --> since we want to project the users but the current solutions are game vertices, we need to select the user vertices which we do using the reference we stored earlier. select('myusers').by('name') --> 因为我们想要投影用户,但当前的解决方案是游戏顶点,我们需要 select 使用我们之前存储的参考所做的用户顶点。

Option 3: Start from user选项 3:从用户开始

gV().hasLabel('user').where(out('plays').has('game','warcraft')).where(out('uses').has('console','ps3')).valueMap('name')

There are more ways to write this query such as using path() but I won't go into details here.有更多方法可以编写此查询,例如使用path()但我不会在这里详细介绍 go。

Since you are beginning to learn Gremlin, I would recommend that you start with https://kelvinlawrence.net/book/Gremlin-Graph-Guide.html既然你开始学习 Gremlin,我建议你从https://kelvinlawrence.net/book/Gremlin-Graph-Guide.html开始

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

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