【问题标题】:Azure Cosmos Graph How to select vertex properties to return?Azure Cosmos Graph 如何选择要返回的顶点属性?
【发布时间】:2017-10-27 07:38:00
【问题描述】:

如果我有这样的顶点:

{
  "id": "1",
  "label": "user",
  "type": "vertex",
  "outE": {
    "worksAt": [
      {
        "id": "6e47aa14-0a3a-4e45-8ac4-043ec9f32b50",
        "inV": "spaceneedle.com.br"
      }
    ]
  },
  "properties": {
    "name": [
      {
        "id": "cce42090-efc5-4bb2-9576-922d19164d98",
        "value": "Murilo"
      }
    ],
    "domain": [
      {
        "id": "murilo|domain",
        "value": "spaceneedle.com.br"
      }
    ]
  }
}

是否可以使用 gremlin 选择要返回的属性以具有如下对象?

{
  "id": "1",
  "name": "Murilo"
}

谢谢!

【问题讨论】:

    标签: azure-cosmosdb gremlin


    【解决方案1】:

    我将使用 TinkerPop“现代”玩具图来演示一些选项:

    gremlin> graph = TinkerFactory.createModern()
    ==>tinkergraph[vertices:6 edges:6]
    gremlin> g = graph.traversal()
    ==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
    

    你可以这样做:

    gremlin> g.V(1).valueMap(true,'name')
    ==>[label:person,name:[marko],id:1]
    

    但这包括顶点标签并将“名称”包装在列表中(以考虑多属性)。因此,虽然快速/简单,但它与您要求的输出并不完全匹配。为了获得特定的输出,我会使用project() step,它看起来像这样:

    gremlin> g.V(1).project("id","name").by(id).by('name')
    ==>[id:1,name:marko]
    

    如果您有混合的正在投影的顶点,其中一些可能没有某些属性,您可以使用coalesce() 或类似方法来确保默认值:

    gremlin> g.V().project('id','name','age').by(id).by('name').by(coalesce(values('age'),constant('none')))
    ==>[id:1,name:marko,age:45]
    ==>[id:2,name:vadas,age:27]
    ==>[id:3,name:lop,age:none]
    ==>[id:4,name:josh,age:32]
    ==>[id:5,name:ripple,age:none]
    ==>[id:6,name:peter,age:35]
    

    【讨论】:

    • 这正是我所需要的!:g.V(1).project("id","name").by(id).by('name') 工作正常!谢谢!!!
    • 我发现了一个使用项目的情况。当我有具有不同属性的节点时,查询会返回错误。即使属性不存在,有没有办法检索数据?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 2010-10-14
    相关资源
    最近更新 更多