【问题标题】:Join query in gremlin, group results in a one to many relationship在 gremlin 中加入查询,组导致一对多关系
【发布时间】:2019-11-07 11:48:58
【问题描述】:

我必须输入顶点 Country 和 Airports,每个国家/地区对多个带有标签“hasAirport”的机场都有优势

我正在尝试加入查询,该查询将返回按其拥有的机场分组的国家/地区。

g.V().hasLabel("Country").as("country").out('hasAirport').as("airport").select("country", "airport").by(__.unfold().valueMap(true).fold()).toList()

如果我的图表中只有一个国家说美国有两个机场,则查询结果如下所示。

[   {
    "country": [
      {
        "name": "United States",
        "code": "USA",      "label": "Country",
        "id": 1565,
      }
    ],
    "airport": [
      {
        "id": 1234,
        "label": "Airport",
        "name": "San Francisco International Airport",      "code": "SFO"
      }
    ]   },   {
    "country": [
      {
        "name": "United States",
        "code": "USA",      "label": "Country",
        "id": 1565,
      }
    ],
    "airport": [
      {
        "id": 2345,
        "label": "Airport",
        "name": "Austin Bergstrom International Airport",       "code": "AUS"
      }
    ]   }  ]

有没有办法将多个机场组合成一个数组,如下所示

[
  {
    "country": [
      {
        "name": "United States",
        "code": "USA",
        "label": "Country",
        "id": 1565,
      }
    ],
    "airport": [
      {
        "id": 1234,
        "label": "Airport",
        "name": "San Francisco International Airport",
        "code": "SFO"
      },
      {
        "id": 2345,
        "label": "Airport",
        "name": "Austin Bergstrom International Airport",
        "code": "AUS"
      }
    ]
  }
 ]

【问题讨论】:

    标签: java gremlin tinkerpop amazon-neptune


    【解决方案1】:

    你需要使用project:

    g.V().hasLabel("Country")
    .project("country","airport")
    .by(valueMap(true))
    .by(out('hasAirport').valueMap(true).fold())
    

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 2019-04-22
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      相关资源
      最近更新 更多