在 CosmosDB 中将顶点存储为 JSON

storing a vertex as JSON in CosmosDB

我见过的所有使用 gremlin API 查询 CosmosDB 图的示例都使用具有一级属性的顶点。但是,如果我们想将顶点表示为 JSON 文档怎么办?

user.name = "Mike"
user.location.lat = "37.7749"
user.location.lng = "122.4194"

有时嵌套属性应该拆分为单独的顶点并通过边链接,但这通常是不必要的。

推荐的方法是什么?是否应该只存在一个适配器 class 来 flattens/unflattens 顶点进入和离开数据库?这看起来很简单,但在性能方面非常昂贵。

有一种使用 Gremlin API 编写嵌套属性的方法,并且 Cosmos DB 支持。但是,此架构要求不会按照您描述的方式映射到 JSON 文档格式。

Gremlin 顶点属性每个键可以有多个值,每个值可以有元属性(也称为嵌套属性)。

我建议阅读 Tinkerpop reference on Vertex Properties

以下是如何通过 gremlin 将嵌套属性添加到顶点 属性:

g.V('vertex_id').property('name', 'marko') // (1)
g.V('vertex_id').properties('name').hasValue('marko').property('metaprop', 'value') // (2)

(1) - 添加一个顶点 属性 ('name', 'marko) (2) - 在 ('name', 'marko) 属性

上添加嵌套 属性

下面是 json 文档的示例,该文档将使用顶点 属性 架构存储在 CosmosDB 中:

{
  id: <ID>,
  label: 'person',
  name: [{
    id: <ID>,
    _value: 'marko',
    _meta : {
      metaprop: 'value'
    }
  }],
  address: [
    {
      id: <ID>,
      _value: 'street 1',
      _meta: {
        type: 'street',
        somethingElse: 'value'
      }
    },
    {
      id: <ID>,
      _value: 'new york',
      _meta: {
        type: 'city',
        anotherMeta: 'something'
      }
    }
  ]
}