我正在使用CosmosDB Graph 数据库来存储一些人的姓名、他们的婚姻以及他们在婚姻中拥有的孩子。在下图中,您将看到丈夫的第一次婚姻有一个孩子 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'))