Azure Cosmos DB – Graph DB

Using Graph DB is a good way to show relationships between entities (more specifically a vertex and edges).

This is why you would use this approach over classic SQL Server / TSQL code. I am not sure if you can do this with the latter, something like:

AGraph

With Cosmos DB this is done via the Gremlin API, so you would use Gremlin (based on tinkerpop – http://tinkerpop.apache.org/) to create the vertex and edges and also to query it.

Assume I have created the database as shown below I then show (basics) gremlin.

AddGrapg

So to create my first vertex using gremlin I run, which is myself, aged 19 setting  car property to yes.

 g.addV(‘person’).property(‘id’,’Arun’).property(‘age’,19).property(‘car’,’yes’)

me

There I am. Lets build this up slightly and confirm.

 g.addV(‘person’).property(‘id’,’Tommy’).property(‘age’,55).property(‘car’,’yes’)

g.addV(‘person’).property(‘id’,’Brent’).property(‘age’,39).property(‘car’,’yes’)

g.V()

results

The graph will show not structural relationships because we have no defined edges – before adding edges I add some details around cars.

 g.addV(‘car).property(‘id’,’BMW’).property(‘car’,’yes’)

g.addV(‘car’).property(‘id’,’Merc’).property(‘car’,’yes’)

Edge relationships are next via .addE

g.V().has(‘id’,’Arun’).addE(‘Drives’).to(g.V().has(‘id’,’BMW’))

g.V().has(‘id’,’Brent’).addE(‘Drives’).to(g.V().has(‘id’,’BMW’))

g.V().has(‘id’,’Brent’).addE(‘Drives’).to(g.V().has(‘id’,’Merc’))

g.V()

cars

 

So as the code stated, Brent has 2 cars, both BMW and Mercedes and Arun has a BMW.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s