storing a vertex as JSON in CosmosDB
Asked Answered
S

1

6

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.

Spinose answered 16/5, 2017 at 18:50 Comment(0)
C
4

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'
      }
    }
  ]
}
Caleb answered 18/8, 2017 at 1:33 Comment(5)
Thanks, I did not know about vertex properties. This does not seem sufficient for my use case however as I would still need to write an adapter to translate between the raw JSON and this structure. Also I don't know if I could create an index on a nested property, which is something I would need.Spinose
To clarify: If you're aim is to ingest raw JSON into CosmosDB graph vertices, then you are correct, and the current support will require you to adapt your data model to the Gremlin vertex structure. Do you have an example/sample of your JSON documents that you can share? We are aiming to support a format which would make writing/reading JSON with Gremlin easier/more fluent and avoid specific schema requirements. This work is in-progress and we will provide more details on it in the future.Caleb
Hi Olivier, I cannot provide example data but I would look to the graph API that ArangoDB provides. It is pretty much as simple as it can be and let's you pass any valid JSON to create a new vertex: docs.arangodb.com/3.2/HTTP/Gharial/Vertices.htmlSpinose
I have raw json that i dont need to query but do need to persist and think it would be great if the vertex could save that jsonKaryn
And ive hit a roadblock stringifying the json because of embeded already escaped characters within the jsonKaryn

© 2022 - 2024 — McMap. All rights reserved.