GroupBy查询顶点列表

时间:2019-04-20 18:52:58

标签: graph-databases gremlin tinkerpop tinkerpop3 amazon-neptune

假设我要查询一个或多个属性上带有“ group-by”的海王星图,并且我也想获取顶点列表。

比方说,我想按(“城市”,“年龄”)分组,并且也要获取顶点列表:

[
  {"city": "SFO", "age": 29, "persons": [v[1], ...]},
  {"city": "SFO", "age": 30, "persons": [v[10], v[13], ...]}, 
  ...
]

或者,使用其属性(如valueMap)返回顶点:

[
  {"city": "SFO", "age": 29, "persons": [[id:1,label:person,name:[marko],age:[29],city:[SFO]], ...]},
  ...
]

AFAIK,Neptune不支持lambda或变量分配。有没有办法只遍历一次而没有lambda?

更新:我能够获取顶点,但是没有其属性(使用valueMap)。

查询:

g.V().hasLabel("person").group().
   by(values("city", "age").fold()).
   by(fold().
     match(__.as("p").unfold().values("city").as("city"),
           __.as("p").unfold().values("age").as("age"),
           __.as("p").fold().unfold().as("persons")).
     select("city", "age", "persons")).
   select(values).
   next()

输出:

==>[city:SFO,age:29,persons:[v[1]]]
==>[city:SFO,age:27,persons:[v[2],v[23]]]
...

1 个答案:

答案 0 :(得分:1)

如果我理解正确,那么...

g.V().hasLabel("person").
  group().
    by(values("city", "age").fold())

...或...

g.V().hasLabel("person").
  group().
    by(valueMap("city", "age").by(unfold()))

...已经为您提供了所需的内容,仅与重新确定结果有关。要将键和值中的地图合并在一起,可以执行以下操作:

g.V().hasLabel("person").
  group().
    by(valueMap("city", "age").by(unfold())).
  unfold().
  map(union(select(keys), 
            project("persons").
              by(values)).
      unfold().
      group().
        by(keys).
        by(select(values)))

在现代玩具图上执行此操作(将city替换为name)将产生以下结果:

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin>     g.V().hasLabel("person").
......1>       group().
......2>         by(valueMap("name", "age").by(unfold())).
......3>       unfold().
......4>       map(union(select(keys), 
......5>                 project("persons").
......6>                   by(values)).
......7>           unfold().
......8>           group().
......9>             by(keys).
.....10>             by(select(values)))
==>[persons:[v[2]],name:vadas,age:27]
==>[persons:[v[4]],name:josh,age:32]
==>[persons:[v[1]],name:marko,age:29]
==>[persons:[v[6]],name:peter,age:35]