【问题标题】:Gremlin query to group by multiple columns (from vertex, and edge)Gremlin 查询按多列分组(从顶点和边)
【发布时间】:2022-01-06 19:22:53
【问题描述】:

我有一个开发者顶点和一个项目顶点以及一个带有开始日期和结束日期属性的边。 开发人员在给定的时间点只能在一个项目上工作。有时他们会被用户错误地分配给具有相同开始日期的多个项目。

  1. 我需要了解开发人员是否分配了多个项目 给他们相同的开始日期。如果是,那么我需要打印

      001 Akash 2021-06-01  2
    
  2. 如果您查看第三位开发人员,他从 2021 年 7 月 1 日开始被分配到 2 个项目。但其中一个是有效的,因为它有一个结束日期 对应的记录开始日期为 2021-09-01。在 我需要列出的第二份报告

{"id":"003_P003","label":"works_in","start_date":"2021-07-01","end_date":"2021-07-05"} 是重复的和奇数的。

我尝试了 #1 的以下查询,但它没有显示 ID、开始日期和计数。它只显示开发者的 ID 和计数

 g.V().
  hasLabel('developer').
  outE('works_in').
  groupCount().by(outV().id()).
  groupCount().by('start_date').
  limit(2).
  unfold().
  toList()

然后我尝试了接下来的两个,但没有成功并显示错误消息“'Column' object is not callable”(我正在使用 Python Germlin

 g.V().
  hasLabel('developer').
  outE('works_in').by(values(outV().id(), 'start_date')).
  groupCount().
  unfold().
  toList() 

下面的下一个也抛出错误。

 g.V().
  hasLabel('developer').
  outE('works_in').
  groupCount().by(outV().id(), 'start_date').
  unfold().
  toList()

#2 我还没有开始。不过,我不确定是否可以使用 Gremlin 完成。

开发者(顶点)

[{"id":"001","label":"developer","name":"Akash","skill":"c#"},
{"id":"002","label":"developer","name":"John","skill":"react"}, 
{"id":"003","label":"developer","name":"Bruno","skill":"python"}]

项目(顶点)

[{"id":"P001","label":"project","name":"Web App"},{"id":"P002","label":"project","name":"Smart Contract"}, {"id":"P003","label":"project","name":"Migrate to AWS"}]

works_in(边缘)

    [{"id":"001_P001","label":"works_in","start_date":"2021-06-01","end_date":"2021-12-31"},
    {"id":"002_P002","label":"works_in","start_date":"2021-01-01","end_date":"2021-12-31"}, 
    {"id":"001_P002","label":"works_in","start_date":"2021-06-01","end_date":"2021-06-30"},
    
    {"id":"003_P003","label":"works_in","start_date":"2021-01-01","end_date":"2021-06-30"},
    {"id":"003_P003","label":"works_in","start_date":"2021-07-01","end_date":"2021-07-05"},
    {"id":"003_P002","label":"works_in","start_date":"2021-07-01","end_date":"2021-08-31"},
    {"id":"003_P002","label":"works_in","start_date":"2021-09-01","end_date":"2021-12-31"}

]

感谢任何帮助。 除了 Tinkerpop 的官方文档之外,我还在寻找教程/课程来更好地理解 Gremlin 查询。

【问题讨论】:

  • 我认为您错过了添加示例数据集。
  • 我只在问题中给出了顶点和边的样本数据。你是说 csv 文件吗?
  • 知道了。一般来说,最好发布 gremlin 查询,以便轻松创建示例数据集。

标签: gremlin amazon-neptune gremlinpython


【解决方案1】:

查询第一个用例:

   gremlin> g.V().
......1>   hasLabel('developer').
......2>   local(
......3>     __.as('b').
......4>     outE().
......5>     project('id', 'name', 'startDate').
......6>       by(select('b').values('id')).
......7>       by(select('b').values('name')).
......8>       by(values('startDate')).
......9>     groupCount().
.....10>     unfold().as('a').
.....11>     select(values).
.....12>     is(gt(1)).
.....13>     select('a')).
.....14>   local(
.....15>     union(select(keys).unfold().select(values), select(values)).fold())
==>[003,bruno,2021-07-01,2]
==>[001,akash,2021-06-01,2]

查询第二个用例

我无法为第二个用例编写任何直接查询。

【讨论】:

  • 谢谢。任何了解 gremlin 查询结构的文档/教程,查看您的查询,我明白我仍然在努力掌握 gremlin 查询结构,就像我尝试不同的查询来解决这个问题时一样,我从来没有使用过本地/联合我从来没有想到。因此,在理解 Gremlin 查询的工作原理方面肯定存在很大差距,
  • 我发现 Kelvin 的这本书很有帮助kelvinlawrence.net/book/Gremlin-Graph-Guide.html
猜你喜欢
  • 2021-09-14
  • 2021-12-07
  • 1970-01-01
  • 2018-02-06
  • 2021-10-11
  • 2015-04-19
  • 2015-07-06
  • 2017-10-24
  • 1970-01-01
相关资源
最近更新 更多