如何使用Gremlin从CosmosDB图生成自定义JSON输出? [英] How to generate a custom JSON output from a CosmosDB Graph using Gremlin?
问题描述
我正在使用 CosmosDB 图形数据库存储一些人的姓名,他们的婚姻以及他们婚姻之外的孩子.在下图中,您将看到该人丈夫的第一次婚姻有孩子A ,而第二次婚姻的孩子B ./p>
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
我想使用 GremlinAPI 来生成如下所示的JSON输出,它是一个分层树结构.
I want to use the GremlinAPI to generate a JSON output like the following which is a hierarchical tree structure.
如何将 person
构造为节点,将 relationship
构造为 edge ,以转换图形到自定义JSON输出?
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"
}
}
}
]
}
]
}
更新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'))
推荐答案
我认为没有任何方法可以使结构化您所描述的格式的结果.最好的办法可能是:
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:
- 获取包含所有关系的树
- 在客户端处理此树并将其转换为所需的结构
要获取完整的树,请执行以下操作:
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)
这篇关于如何使用Gremlin从CosmosDB图生成自定义JSON输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!