如何在CosmosDB中将树格式的Gremlin GraphSON转换为自定义JSON树格式?

时间:2019-06-23 17:42:38

标签: azure-cosmosdb graph-databases gremlin azure-cosmosdb-gremlinapi

我有以下在CosmosDB中成功运行的Gremlin查询:

g.addV('person').property(id, 'grand_father').property('name', 'Grand Father')
g.addV('person').property(id, 'father').property('name', 'Father')
g.addV('person').property(id, 'child').property('name', 'Child')
g.V('grand_father').addE('father_of').to(V('father'))
g.V('father').addE('father_of').to(V('child'))

我运行了查询g.V('grand_father').repeat(out()).emit().tree(),该查询生成以下输出:

[
  {
    "grand_father": {
      "key": {
        "id": "grand_father",
        "label": "person",
        "type": "vertex",
        "properties": {
          "name": [
            {
              "id": "2b687c65-6490-4846-a5ef-1b7d67e51916",
              "value": "Grand Father"
            }
          ]
        }
      },
      "value": {
        "father": {
          "key": {
            "id": "father",
            "label": "person",
            "type": "vertex",
            "properties": {
              "name": [
                {
                  "id": "c1f75463-8aa5-4c15-854d-88be0ec9cdc9",
                  "value": "Father"
                }
              ]
            }
          },
          "value": {
            "child": {
              "key": {
                "id": "child",
                "label": "person",
                "type": "vertex",
                "properties": {
                  "name": [
                    {
                      "id": "d74d6286-5fa9-4b90-9619-1f173d5da53e",
                      "value": "Child"
                    }
                  ]
                }
              },
              "value": {}
            }
          }
        }
      }
    }
  }
]

我想再次转换上面的GraphSON树,以生成以下格式的自定义层次树。

{
   "person":{
      "name":"Grand Father"
   },
   "children":[
      {
         "person":{
            "name":"Father"
         },
         "children":[
            {
               "person":{
                  "name":"Child"
               }
            }
         ]
      }
   ]
}

我需要对g.V('grand_father').repeat(out()).emit().tree()进行哪些更改才能获得结果?

1 个答案:

答案 0 :(得分:1)

我认为以下内容非常接近您的要求:

gremlin> g.V('grand_father').
......1>   repeat(out()).
......2>     emit().
......3>   tree().
......4>     by(group().
......5>          by(label).
......6>          by(valueMap('name').by(unfold())).
......7>        unfold().unfold())
==>[person={name=Grand Father}:[person={name=Father}:[person={name=Child}:[]]]]

请注意,tree()采用by()调制器,该调制器将作为参数的匿名遍历应用到树的每个项目。我没有在JSON输出中捕获“子级”叶子,但是也许这可以使您足够接近目标,而不会带来更多复杂性。请注意,CosmosDB可能尚不支持valueMap('name').by(unfold())技巧,在这种情况下,您可以删除by(unfold())并留以List来包装值,将其替换为project('name').by('name')或替换并使用展开下面显示的valueMap()的“旧”方式:

gremlin> g.V('grand_father').
......1>   repeat(out()).
......2>     emit().
......3>   tree().
......4>     by(group().
......5>          by(label).
......6>          by(valueMap('name').
......7>             unfold().
......8>             group().
......9>               by(select(keys)).
.....10>               by(select(values).unfold())).
.....11>        unfold().unfold())
==>[person={name=Grand Father}:[person={name=Father}:[person={name=Child}:[]]]]