Graph 如何使用Gremlin从CosmosDB图生成自定义JSON输出?

Graph 如何使用Gremlin从CosmosDB图生成自定义JSON输出?,graph,tree,azure-cosmosdb,gremlin,azure-cosmosdb-gremlinapi,Graph,Tree,Azure Cosmosdb,Gremlin,Azure Cosmosdb Gremlinapi,我正在使用CosmosDB图形数据库来存储一些人的名字,他们的婚姻和他们婚姻中的孩子。在下图中,您将看到丈夫的第一次婚姻中有一个孩子a,第二次婚姻中有一个孩子B Father of Husband Mother of Husband GRAND FATHER & GRAND MOTHER +---------------+--------------+

我正在使用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
我想使用生成JSON输出,如下所示,这是一个层次树结构

如何将person构造为节点,将关系构造为边,以将图形转换为自定义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'))


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

获取包含所有关系的树 在客户端处理此树并将其转换为所需的结构 要获取完整的树,请执行以下操作:

g.V('marriage_f_m').
  repeat(__.both().simplePath()).
    emit().
  tree()
。。。或包含边缘标签以简化重组:

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

该图很好,但最好得到一些实际的示例数据,如下面的示例:这样,您可以得到一个正在工作/测试的Gremlin遍历作为答案。我用可用于创建顶点和边的查询更新了问题。他们在宇宙飞船上成功运行。我不知道网上有什么游乐场,我可以在那里运行这些查询,使您能够轻松访问。TinkerGraph就是游乐场!:我没有找到像JavaScript游乐场这样的在线游乐场来运行查询。CosmosDB Gremlin控制台是私有的。我运行了一些基本的查询,这是我的知识水平。考虑到您希望返回的数据,我猜您希望从特定的婚姻开始遍历,特别是婚姻,对吗?