简体   繁体   English

使用 Gremlin (AWS Neptune),如何从具有特定条件的起始节点遍历边缘获取长度为 n 的所有路径?

[英]Using Gremlin (AWS Neptune), how can I get all paths of length n from a starting node traversing edges with specific criteria?

Starting with node 1, I want to return all the paths (edges and vertices, with id/label/properties) within n hops following any outbound edges, or following inbound edges with a property predicate (p > 50).从节点 1 开始,我想在任何出站边之后的 n 个跃点内返回所有路径(边和顶点,带有 id/label/properties),或者在带有属性谓词(p > 50)的入站边之后返回。

Ideally the paths shouldn't contain any cycles, so a path shouldn't contain the same node twice.理想情况下,路径不应包含任何循环,因此路径不应包含两次相同的节点。

The property p is not present on every edge.属性 p 并不存在于每条边上。

g.addV().property(id, 1).as('1').
  addV().property(id, 2).as('2').
  addV().property(id, 3).as('3').
  addV().property(id, 4).as('4').
  addV().property(id, 5).as('5').
  addV().property(id, 6).as('6').
  addV().property(id, 7).as('7').
  addV().property(id, 8).as('8').
  addE('pointsAt').from('1').to('2').
  addE('pointsAt').from('3').to('1').
  addE('pointsAt').from('4').to('1').property('p', 10).
  addE('pointsAt').from('5').to('1').property('p', 100).
  addE('pointsAt').from('2').to('6').
  addE('pointsAt').from('7').to('2').
  addE('pointsAt').from('8').to('2').property('p', 100).
  iterate()

Assuming we start at Vertex 1 the paths would look like:假设我们从顶点 1 开始,路径将如下所示:

1>2
1>2>6
1>2>8
1>5
  • 1-2 is included because it's outbound包含1-2 是因为它是出站的
  • 1-3 is excluded because it is inbound to 1 and doesn't have p 1-3 被排除在外,因为它是入站到 1 并且没有 p
  • 1-4 is excluded because it is inbound and (p > 50) is false 1-4 被排除,因为它是入站的并且 (p > 50) 为假
  • 1-5 is included because it is inbound and (p > 50) is true包含1-5 是因为它是入站的并且 (p > 50) 为真
  • 2-6 is included because it's outbound包含2-6 是因为它是出站的
  • 2-7 is excluded because it is inbound to 2 and doesn't have p 2-7 被排除在外,因为它是入站到 2 并且没有 p
  • 2-8 is included because it is inbound to 2 and p > 50包含2-8 是因为它是入站到 2 并且 p > 50

I've experimented with many different approaches and I can't seem to get anything close to what I am looking for.我已经尝试了许多不同的方法,但我似乎无法得到任何接近我正在寻找的东西。

I believe this is what you're looking for:我相信这就是您正在寻找的:

g.V(1).
   repeat(
      union(
         outE(),inE().has('p',gt(50))
      ).
      otherV().simplePath()).
   emit().
   times(2).
   path().
      by(valueMap().with(WithOptions.tokens))

The repeat() and times() steps dictate that this is a recursive traversal going to a depth of 2. The union() step and containing arguments follow your requirements to include all outgoing edges and only incoming edges with ap property greater than 50. The emit() step forces the repeat() step to stream all paths as they are found. repeat() 和 times() 步骤表明这是一个深度为 2 的递归遍历。 union() 步骤并包含 arguments 遵循您的要求,包括所有传出边和仅 ap 属性大于 50 的传入边。 emit() 步骤强制对 stream 找到所有路径执行 repeat() 步骤。 If you didn't include this, you would only get the paths found of length 2 (declared in the times() step).如果你不包括这个,你只会得到长度为 2 的路径(在 times() 步骤中声明)。

To wrap this up, we use path() and by() to output the paths found and all of the ids, labels, and properties for each vertex and edge in the path.总结一下,我们使用 path() 和 by() 到 output 找到的路径以及路径中每个顶点和边的所有 id、标签和属性。

The output of this from the graph that you provided looks like this:您提供的图表中的 output 如下所示:

==>[[id:1,label:vertex],[id:0,label:pointsAt],[id:2,label:vertex]]
==>[[id:1,label:vertex],[id:3,label:pointsAt,p:100],[id:5,label:vertex]]
==>[[id:1,label:vertex],[id:0,label:pointsAt],[id:2,label:vertex],[id:4,label:pointsAt],[id:6,label:vertex]]
==>[[id:1,label:vertex],[id:0,label:pointsAt],[id:2,label:vertex],[id:6,label:pointsAt,p:100],[id:8,label:vertex]]

暂无
暂无

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

相关问题 如何在 Amazon Neptune 中使用 Gremlin 有条件地添加顶点和多条边? - How can I conditionally add a vertex and multiple edges using Gremlin in Amazon Neptune? 如何使用Gremlin查找包含在一组N个顶点中的对中的特定长度的所有路径 - How to find all paths up to specific length among pairs contained in a set of N vertices with Gremlin 如何使用 gremlin QL 递归地获取所有子顶点及其边缘(两者的属性)? - How can I get all child vertices and its edges (properties for both) recursively using gremlin QL? 使用pythongremlin删除AWS Neptune的所有边缘 - Drop all edges on AWS Neptune using pythongremlin Gremlin:AWS Neptune - 获取图中每个节点的所有叶节点作为 CSV - Gremlin : AWS Neptune - Get all Leaf Nodes for each Node in the Graph as CSV Gremlin 查询以计算边数 - MemoryLimitExceededException AWS Neptune - Gremlin Query to count Edges - MemoryLimitExceededException AWS Neptune Python/Gremlin/AWS Neptune:给定一个节点,最终所有祖先 - Python/Gremlin/AWS Neptune: Given a node, final all ancestors 我们可以将 Wikidata 转储加载到 AWS Neptune 并使用 Gremlin 进行查询吗? - Can we load the Wikidata dumps to AWS Neptune and query using Gremlin? 在哪里可以找到 AWS Neptune 使用的 Tinkerpop Gremlin 版本 - Where can I find out what version of Tinkerpop Gremlin, AWS Neptune is using 如何在没有变量的情况下在 AWS Neptune 中进行复杂的 Gremlin 查询? - How can I make complex Gremlin queries in AWS Neptune without variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM