How to generate a custom JSON output from a CosmosDB Graph using Gremlin?

霸气de小男生 提交于 2020-01-16 19:08:10

问题


I'm using the CosmosDB Graph database to store the names of a few people, their marriages and the children whom they have out of the marriages. In the following diagram, you will see that the person Husband has a Child A from his first marriage and Child B from his second marriage.

       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

I want to use the GremlinAPI to generate a JSON output like the following which is a hierarchical tree structure.

How do I structure the person as a node and the relationship as an edge to convert the graph to a custom JSON output?

{
    "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"
                        }
                    }
                }
            ]
        }
    ]
}

UPDATE 6/21/2019

I created the following query to create the vertex and edges:

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'))


回答1:


I don't think there's any way to get the result structured in the format you've described. The best thing to do is probably:

  • get a tree containing all relationships
  • process this tree on the client-side and transform it into the desired structure

To get the full tree, you'd do:

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

... or with edge labels included to simplify the restructuring:

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


来源:https://stackoverflow.com/questions/56703768/how-to-generate-a-custom-json-output-from-a-cosmosdb-graph-using-gremlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!