【问题标题】:How to generate a custom JSON output from a CosmosDB Graph using Gremlin?如何使用 Gremlin 从 CosmosDB 图形生成自定义 JSON 输出?
【发布时间】:2019-06-21 12:47:40
【问题描述】:

我正在使用 CosmosDB 图形数据库来存储一些人的姓名、他们的婚姻以及他们从婚姻中获得的孩子。在下图中,您将看到 丈夫 有一个第一次婚姻的 孩子 A 和第二次婚姻的 孩子 B

       Father of Husband                 Mother of Husband        GRAND FATHER & GRAND MOTHER
                +---------------+--------------+
                            Marriage
                                |
   +------------+---------------+--------------+-----------+      FATHER & MOTHER
Ex Wife A   Marriage         Husband       Marriage      Wife B
                |                              |
            Child A                         Child B               ME & STEP BROTHERS

我想使用GremlinAPI 生成如下 JSON 输出,这是一个层次树结构。

如何将person 构造为节点,将relationship 构造为,以将图形转换为自定义 JSON 输出?

{
    "marriage": {
        "husband": {
            "name": "Father Of Husband"
        },
        "wife": {
            "name": "Mother Of Husband"
        }
    },
    "children": [
        {
            "marriage": {
                "husband": {
                    "name": "Husband"
                },
                "wife": {
                    "name": "Wife A"
                }
            },
            "children": [
                {
                    "marriage": {
                        "husband": {
                            "name": "Child A"
                        },
                        "wife": {
                            "name": "Unknown"
                        }
                    }
                }
            ]
        },
        {
            "marriage": {
                "husband": {
                    "name": "Husband"
                },
                "wife": {
                    "name": "Wife B"
                }
            },
            "children": [
                {
                    "marriage": {
                        "husband": {
                            "name": "Child B"
                        },
                        "wife": {
                            "name": "Unknown"
                        }
                    }
                }
            ]
        }
    ]
}

2019 年 6 月 21 日更新

我创建了以下查询来创建顶点和边:

g.V().drop()
g.addV('person').property(id, 'father_of_husband').property('name', 'Father Of Husband').property('title', 'husband')
g.addV('person').property(id, 'mother_of_husband').property('name', 'Mother Of Husband').property('title', 'wife')
g.addV('person').property(id, 'husband').property('name', 'Husband').property('title', 'husband')
g.addV('person').property(id, 'ex_wife_a').property('name', 'Ex Wife A').property('title', 'wife')
g.addV('person').property(id, 'wife_b').property('name', 'Wife B').property('title', 'wife')
g.addV('person').property(id, 'child_a').property('name', 'Child A').property('title', 'husband')
g.addV('person').property(id, 'child_b').property('name', 'Child B').property('title', 'wife')

g.addV('marriage').property(id, 'marriage_f_m').property('name', 'Marriage F & M')
g.addV('marriage').property(id, 'marriage_ex_wife_a_h').property('name', 'Marriage EWA & H')
g.addV('marriage').property(id, 'marriage_wife_b_h').property('name', 'Marriage WB & H')

g.V('father_of_husband').addE('married').to(g.V('marriage_f_m'))
g.V('mother_of_husband').addE('married').to(g.V('marriage_f_m'))

g.V('ex_wife_a').addE('married').to(g.V('marriage_ex_wife_a_h'))
g.V('husband').addE('married').to(g.V('marriage_ex_wife_a_h'))

g.V('wife_b').addE('married').to(g.V('marriage_wife_b_h'))
g.V('husband').addE('married').to(g.V('marriage_wife_b_h'))

g.V('husband').addE('child_of').to(g.V('marriage_f_m'))
g.V('child_a').addE('child_of').to(g.V('marriage_ex_wife_a_h'))
g.V('child_b').addE('child_of').to(g.V('marriage_wife_b_h'))

【问题讨论】:

  • 图表很好,但最好获取一些实际示例数据,例如:stackoverflow.com/questions/51388315/… 这样您就可以获得工作/测试的 Gremlin 遍历作为答案
  • 我用可用于创建顶点和边的查询更新了问题。它们在 CosmosDB 中成功运行。我不知道有什么在线游乐场可以让我运行这些查询并让您轻松访问。
  • TinkerGraph 是游乐场! :)
  • 我没有找到像 JavaScript playground 这样可以运行查询的在线游乐场。 CosmosDB Gremlin 控制台是私有的。我运行了一些基本的查询,这是我的知识水平。
  • 鉴于您希望返回的数据,我猜您想从特定的婚姻(特别是“marriage_f_m”)开始遍历 - 对吗?

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


【解决方案1】:

我认为没有任何方法可以按照您描述的格式构建结果。最好的办法可能是:

  • 获取包含所有关系的树
  • 在客户端处理此树并将其转换为所需的结构

要获得完整的树,您可以:

g.V('marriage_f_m').
  repeat(__.both().simplePath()).
    emit().
  tree()

...或包含边缘标签以简化重组:

g.V('marriage_f_m').
  repeat(__.bothE().otherV().simplePath()).
    emit().
  tree().
    by().
    by(label)

【讨论】:

    猜你喜欢
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 2018-03-17
    相关资源
    最近更新 更多