首页 > 解决方案 > 使用树的 Gremlin 查询(JSON 输出)

问题描述

询问:

g.withSack(0).V().hasLabel('A').has('label_A','A').union(__.emit().repeat(sack(sum).by(constant(1)).in()),emit().repeat(sack(sum).by(constant(-1)).out())).project('level','properties').by(sack()).by(tree().by(valueMap().by(unfold())).unfold())

输出:

{
    "level": 1,
    "properties": {
        "key": {
            "label_A": "A"
            
        },
        "value": {
            "{label_A=A}": {}
        }
    }
},
{
    "level": 2,
    "properties": {
        "key": {
            "label_A": "A"
          
        },
        "value": {
            "{label_A=A}": {}
            }
        }
    }
}

获取 json 格式的键而不是值。请建议更改查询以实现 json 格式的值。

标签: graphgremlinjanusgraph

解决方案


该步骤本质上以实例tree()的形式返回树结构,因此输出与您的预期有关。在这种情况下,我想知道您是否需要使用并且可以改用 with ,因为它似乎在没有添加结构的情况下完成了相同的结果:MapMaptree()path()

g.withSack(0).
  V().hasLabel('A').has('label_A','A').
  union(__.emit().repeat(sack(sum).by(constant(1)).in()),
        emit().repeat(sack(sum).by(constant(-1)).out())).
  project('level','properties').
    by(sack()).
    by(path().by(elementMap()))

推荐阅读