将“父级顶点”和“子级”组合到主/子json数组中

时间:2018-04-04 21:46:08

标签: azure-cosmosdb gremlin tinkerpop3 graph-traversal

我首先使用azure cosmos db。

一个人works_for多个办事处。每个办公室可以IsMaster或不IsMaster。如果是master_of,它可以works_for边缘到另一个Office。每个AccessLevel边缘都有一个人works_for属性。

逻辑:给定一个人名,让所有办事处IsMaster ='真'的人IsMaster。然后返回从AccessLevel获取works_for的所有IsMaster以及每个master_of通过AccessLevel边缘和相应works_for到{{1}的相应子办公室}}

我的最终目标是在给定人名下面生成一个JSON对象:

[
  {
    "Master": {
      "Name": "Seattle",
      "AccessLevel": "Admin"
    },
    "Subs": [
      {
        "Name": "Portland",
        "AccessLevel": "NonAdmin"
      },
      {
        "Name": "Vancouver",
        "AccessLevel": "Admin"
      }
    ]
  },
  {
    "Master": {
      "Name": "New York",
      "AccessLevel": "NonAdmin"
    },
    "Subs": [
      {
        "Name": "Boston",
        "AccessLevel": "NonAdmin"
      },
      {
        "Name": "Orlando",
        "AccessLevel": "Admin"
      }
    ]
  }
]

enter image description here

g.addV('Office').property('Name','Seattle').property('IsMaster','true').as('ow1')
.addV('Office').property('Name','Portland').property('IsMaster','false').as('ow2')
.addV('Office').property('Name','Vancouver').property('IsMaster','false').as('ow3')
.addV('Office').property('Name','New York').property('IsMaster','true').as('oe1')
.addV('Office').property('Name','Boston').property('IsMaster','false').as('oe2')
.addV('Office').property('Name','Orlando').property('IsMaster','false').as('oe3')
.addV('Person').property('Name','Zodiak').as('p')
.select('p').addE('works_for').property('AccessLevel','Admin').to(g.V().hasLabel('Office').has('Name', 'Seattle'))
.select('p').addE('works_for').property('AccessLevel','NonAdmin').to(g.V().hasLabel('Office').has('Name', 'Portland'))
.select('p').addE('works_for').property('AccessLevel','Admin').to(g.V().hasLabel('Office').has('Name', 'Vancouver'))
.select('p').addE('works_for').property('AccessLevel','NonAdmin').to(g.V().hasLabel('Office').has('Name', 'New York'))
.select('p').addE('works_for').property('AccessLevel','NonAdmin').to(g.V().hasLabel('Office').has('Name', 'Boston'))
.select('p').addE('works_for').property('AccessLevel','Admin').to(g.V().hasLabel('Office').has('Name', 'Orlando'))
.select('ow1').addE('master_of').to(g.V().hasLabel('Office').has('Name','Portland'))
.select('ow1').addE('master_of').to(g.V().hasLabel('Office').has('Name','Vancouver'))
.select('oe1').addE('master_of').to(g.V().hasLabel('Office').has('Name','Boston'))
.select('oe1').addE('master_of').to(g.V().hasLabel('Office').has('Name','Orlando'))

更新:我的一条边缘缺少.by()引用的'AccessLevel'属性。没有该属性查询失败。使用coalesce()我之前将其设置为"foobar"值:

outE("works_for").as("e").coalesce(values('AccessLevel'), property('AccessLevel','foo')).select('e').

1 个答案:

答案 0 :(得分:3)

您的查询(创建图表的那个)是否真的可以在CosmosDB中运行?如果其他人想要关注,请点击这里创建没有嵌套g.V()的图表的查询:

g.addV('Office').property('Name','Seattle').property('IsMaster','true').as('ow1').
  addV('Office').property('Name','Portland').property('IsMaster','false').as('ow2').
  addV('Office').property('Name','Vancouver').property('IsMaster','false').as('ow3').
  addV('Office').property('Name','New York').property('IsMaster','true').as('oe1').
  addV('Office').property('Name','Boston').property('IsMaster','false').as('oe2').
  addV('Office').property('Name','Orlando').property('IsMaster','false').as('oe3').
  addV('Person').property('Name','Zodiak').as('p').
  addE('works_for').property('AccessLevel','Admin').to('ow1').select('p').
  addE('works_for').property('AccessLevel','NonAdmin').to('ow2').select('p').
  addE('works_for').property('AccessLevel','Admin').to('ow3').select('p').
  addE('works_for').property('AccessLevel','NonAdmin').to('oe1').select('p').
  addE('works_for').property('AccessLevel','NonAdmin').to('oe2').select('p').
  addE('works_for').property('AccessLevel','Admin').to('oe3').select('ow1').
  addE('master_of').to('ow2').select('ow1').
  addE('master_of').to('ow3').select('oe1').
  addE('master_of').to('oe2').select('oe1').
  addE('master_of').to('oe3')

产生所需结果的查询:

g.V().has("Person","Name","Zodiak").as("p").
  outE("works_for").as("e").
  inV().has("IsMaster","true").
  project("Master","Subs").
    by(project("Name","AccessLevel").
         by("Name").
         by(select("e").by("AccessLevel"))).
    by(out("master_of").as("o").
       inE("works_for").where(outV().as("p")).
       project("Name","AccessLevel").
         by(select("o").by("Name")).
         by("AccessLevel").fold())