All the examples I've seen using the gremlin API to query a CosmosDB graph use vertices that have one level of properties. But what if we want represent our vertices as JSON documents?
user.name = "Mike"
user.location.lat = "37.7749"
user.location.lng = "122.4194"
Sometimes nested properties should be split out as separate vertices and linked via edges, but often this is unnecessary.
What is the recommended approach for this? Should there simply be an adapter class that flattens/unflattens the vertices as they enter and leave the DB? This seems straightforward but very costly in terms of performance.
There is a way to write nested properties using Gremlin API, and Cosmos DB supports. However the schema requirements for this don't map to the JSON document format in the way you described.
Gremlin vertex properties can have multiple values per key, as well as meta-properties per value (aka. nested properties).
I'd recommend reading Tinkerpop reference on Vertex Properties
Here 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 vertex property ('name', 'marko) (2) - Adds a nested property on the ('name', 'marko) property
And here is an example of the json document that would be stored in CosmosDB with 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'
}
}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With