【问题标题】:Writing an "outer join" query in Gremlin for AWS Neptune (without using lambda steps)在 Gremlin 中为 AWS Neptune 编写“外部联接”查询(不使用 lambda 步骤)
【发布时间】:2019-10-16 05:53:02
【问题描述】:

我想根据以下条件返回用户的不同 git 分支:

  • 不要返回主分支
  • 返回没有关联拉取请求的分支
  • 当没有拉取请求打开或合并时,返回与拉取请求关联的分支。

所以考虑下面的样本数据,

user = graph.addVertex(label, 'User', 'name', 'John')
branch1 = graph.addVertex(label, 'Branch', 'name', 'branch1')
branch2 = graph.addVertex(label, 'Branch', 'name', 'branch2')
branch3 = graph.addVertex(label, 'Branch', 'name', 'branch3')
branchmaster = graph.addVertex(label, 'Branch', 'name', 'master')
user.addEdge('AUTHOR_OF', branch1)
user.addEdge('AUTHOR_OF', branch2)
user.addEdge('AUTHOR_OF', branch3)
user.addEdge('AUTHOR_OF', branchmaster)
pr2 = graph.addVertex(label, 'PullRequest', 'name', 'pr2', 'state', 'OPEN')
pr3 = graph.addVertex(label, 'PullRequest', 'name', 'pr3', 'state', 'DECLINED')
branch2.addEdge('SOURCE_OF', pr2)
branch3.addEdge('SOURCE_OF', pr3)
pr22 = graph.addVertex(label, 'PullRequest', 'name', 'pr22', 'state', 'MERGED')
branch2.addEdge('SOURCE_OF', pr22)
pr23 = graph.addVertex(label, 'PullRequest', 'name', 'pr23', 'state', 'DECLINED')
branch2.addEdge('SOURCE_OF', pr23)

我想返回 branch1(因为没有关联的 PR)和 branch3(因为关联的 PR 被拒绝)

以下查询在 AWS Neptune 上不起作用,因为 Neptune 不支持 lambda 步骤:

g.V().hasLabel('User')
  .out('AUTHOR_OF')
  .hasLabel('Branch')
  .has('name', neq('master'))
  .where(out('SOURCE_OF')
    .hasLabel('PullRequest').values('state').fold()
    .filter{ !(it.get().contains('OPEN') || it.get().contains('MERGED')) })
  .dedup()
  .order().by('updated_at', desc)

【问题讨论】:

    标签: gremlin amazon-neptune


    【解决方案1】:

    您可以改用within 谓词:

    g.V().hasLabel('User')
      .out('AUTHOR_OF')
      .hasLabel('Branch')
      .has('name', neq('master'))
      .where(__.not(out('SOURCE_OF').hasLabel('PullRequest').has('state', within(['OPEN','MERGED']))))
      .dedup()
      .order().by('updated_at', desc)
    

    【讨论】:

    • 好答案 - 或许可以考虑 P.without() 并通过删除 not() 来提高可读性
    • 谢谢,请注意,我们还需要返回没有“SOURCE_OF”的分支,所以这样我就可以单次遍历。另一种方法是使用 'or' 并执行两次 'out' 遍历,有和没有'not',在我看来这是不太可取的。
    • 我明白了 - 有道理。我错过了那个微妙之处
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2021-12-13
    • 2021-10-15
    • 1970-01-01
    • 2019-06-04
    • 2019-08-10
    • 2021-06-05
    相关资源
    最近更新 更多