简体   繁体   English

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

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

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

I want to use the GremlinAPI to generate a JSON output like the following which is a hierarchical tree structure. 我想使用GremlinAPI生成如下所示的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? 如何将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"
                        }
                    }
                }
            ]
        }
    ]
}

UPDATE 6/21/2019 更新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: 最好的办法可能是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Azure CosmosDB图(GREMLIN API)中的两个顶点获取信息 - Get information from two vertices in Azure CosmosDB Graph (GREMLIN API) 使用树的 Gremlin 查询(JSON 输出) - Gremlin Query (JSON Output) using tree 如何使用Gremlin删除图形模式 - How to delete a graph schema using Gremlin 如何使用 Gremlin 为用户生成推荐? - How to generate recommendations for a User using Gremlin? 如何通过使用Gremlin在图中通过公共同级对不同父级的所有同级节点进行分组 - How to group all sibling nodes from different parents by common sibling in a graph using Gremlin CosmosDB:使用现有属性创建新属性的 Gremlin 查询 - CosmosDB: Gremlin Query to create a new property using existing property 如何使用gremlin获取图数据库中边字段的总和? - How to get the sum of a edge field in graph database using gremlin? 如何使用 TinkerPop/Gremlin 处理图形数据的大型 .kryo 文件 - How to process large .kryo files for graph data using TinkerPop/Gremlin 如何使用 Gremlin 检查图形中是否存在节点/顶点/边 - How to check the node/vertex/Edge is present in the Graph using Gremlin 如何使用 C# 从 excel 工作表生成图表 - How to generate a graph from an excel worksheet using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM