【问题标题】:How to transform a Gremlin GraphSON in tree format to custom JSON tree format in CosmosDB?如何将树格式的 Gremlin GraphSON 转换为 CosmosDB 中的自定义 JSON 树格式?
【发布时间】:2019-11-05 15:49:26
【问题描述】:

我有以下在 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() 进行哪些更改才能达到结果?

【问题讨论】:

  • 嗨,你有没有机会做到这一点?我也想做同样的事情,最后我添加了一个带有大量正则表达式的帖子脚本来做到这一点......

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


【解决方案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}:[]]]]

【讨论】:

  • 对于第二个查询,我收到错误消息:ExceptionType : GraphAssertException\nExceptionMessage :\r\n\tcompositeObjField != null
  • 对于第一个查询,我收到错误消息:Unsupported Error: Gremlin op does not support by(traversal)\n
  • sorry....我的示例与 TinkerPop 的参考图实现背道而驰,它旨在成为展示预期 Gremlin 语义的标准,因此理论上,每个启用 TinkerPop 的图都应具有与它。不幸的是,CosmosDB 经常与预期的语义不完全匹配。我预计第一个查询会失败,因为我提到的语法是 3.4.x 的新语法,我认为 CosmosDB 还不支持它。我不太确定为什么第二个查询此时失败了。
  • 我之前用 SQL 和 LINQ 解决了这个问题,方法是选择所有节点,并在 for 循环中遍历它们以生成树。一年前我试图用neo4j 解决这个问题,但因为他们没有开箱即用的tree() 函数而无法解决。我倾向于使用g.V('grand_father').repeat(out()).emit().tree() 生成树的解决方案,对其进行字符串解析并生成我需要的自定义树。即在 Gremlin 服务器上进行双重处理 (1) 以在 API 级别再次生成第一棵树 (2) 以生成自定义树。
  • 这可能是您唯一的方法。我只建议您像我一样使用by() 将您的结果与您需要节省序列化成本和网络有效负载大小的数据配对。除了参考文档tinkerpop.apache.org/docs/current/reference/#tree-step 和实用的 Gremlin 书:kelvinlawrence.net/book/Gremlin-Graph-Guide.html 之外,真的没有其他文档可以用于tree()。除了我用它演示过的内容之外,它确实没有太多内容。非常简单的步骤。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-19
相关资源
最近更新 更多