Saving vertex as JSON in CosmosDB

All the examples I've seen using the gremlin API to query the CosmosDB graph use vertices that have the same property level. But what if we want to represent our vertices as JSON documents?

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

      

Sometimes nested properties need to be split as separate vertices and connected via edges, but often this is not necessary.

What is the recommended approach for this? Should there be an adapter class that flattens / unwinds vertices as they are entered and exits the DB? It seems simple, but very expensive in terms of performance.

+3


source to share


1 answer


There is a way to write nested properties using the Gremlin API, and there is also support for Cosmos DB. However, the requirements for this schema do not map to the JSON document format in the way you described.

Gremlin vertex properties can have multiple values ​​for each key, as well as meta properties for the value (attached properties).

I would recommend reading Tinkerpop's link to vertex properties

This is how you can add nested properties to a vertex property via gremlin:



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

      

(1) - adds a top property ('name', 'marko) (2) - adds an attached property to the (' name ',' marko) property

And here is an example json document to be stored in CosmosDB using the vertex property schema:

{
  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'
      }
    }
  ]
}

      

+2


source







All Articles