Graph database — Using Cosmos DB (Gremlin API)

Caio Moreno
3 min readOct 23, 2018

--

In computing, a graph database is a database that uses graph structures for semantic queries with nodes, edges and properties to represent and store data. A key concept of the system is the graph (or edge or relationship), which directly relates data items in the store. The relationships allow data in the store to be linked together directly, and in many cases retrieved with one operation.
(Source:
Wikipedia)

Azure Cosmos DB (Gremlin API)

Azure Cosmos DB supports Apache Tinkerpop’s graph traversal language, Gremlin, which is a Gremlin API for creating graph entities, and performing graph query operations. You can use the Gremlin language to create graph entities (vertices and edges), modify properties within those entities, perform queries and traversals, and delete entities.

Azure Cosmos DB brings enterprise-ready features to graph databases. This includes global distribution, independent scaling of storage and throughput, predictable single-digit millisecond latencies, automatic indexing, SLAs, read availability for database accounts spanning two or more Azure regions. Because Azure Cosmos DB supports TinkerPop/Gremlin, you can easily migrate applications written using another graph database without having to make code changes. Additionally, by virtue of Gremlin support, Azure Cosmos DB seamlessly integrates with TinkerPop-enabled analytics frameworks like Apache Spark GraphX. (Source: Microsoft)

Graph Example

This graph has the following vertex types (called “label” in Gremlin):

  • People: The graph has three people, Robin, Thomas, and Ben
  • Interests: Their interests, in this example, the game of Football
  • Devices: The devices that people use
  • Operating Systems: The operating systems that the devices run on

We represent the relationships between these entities via the following edge types/labels:

  • Knows: For example, “Thomas knows Robin”
  • Interested: To represent the interests of the people in our graph, for example, “Ben is interested in Football”
  • RunsOS: Laptop runs the Windows OS
  • Uses: To represent which device a person uses. For example, Robin uses a Motorola phone with serial number 77

Learn more here.

How to create a graph DB using Azure

New -> Databases -> Cosmos DB -> Use the API Gremlin (graph)

Azure Cosmos DB (Graph API) Playground

g.addV('person').property('id', 'caio.1').property('firstName', 'Caio').property('lastName', 'Moreno').property('city', 'London').property('age', '35')
g.addV('person').property('id', 'anaeliza.1').property('firstName', 'Ana Eliza').property('lastName', 'Motta').property('city', 'London').property('age', '5')
g.addV('person').property('id', 'helena.1').property('firstName', 'Helena').property('lastName', 'Motta').property('city', 'London').property('age', '2')
g.V('caio.1').addE('knows').to(g.V('helena.1'))
g.V('caio.1').addE('knows').to(g.V('anaeliza.1'))
g.V()
g.V().hasLabel('person').order().by('city', decr)

Add more people and company to the network

g.addV('company').property('id', 'company.1').property('companyName', 'Company A').property('city', 'London')
g.addV('person').property('id', 'josias.1').property('firstName', 'Josias').property('lastName', 'Smith').property('city', 'London').property('age', '22')
g.addV('person').property('id', 'joseph.1').property('firstName', 'Joseph').property('lastName', 'Bruce').property('city', 'London').property('age', '33')
g.V('caio.1').addE('works_at').to(g.V('company.1'))
g.V('josias.1').addE('works_at').to(g.V('company.1'))
g.V('joseph.1').addE('works_at').to(g.V('company.1'))

View the graph example

Learn more:
http://tinkerpop.apache.org/docs/current/reference/
https://docs.microsoft.com/en-us/azure/cosmos-db/gremlin-support
https://en.wikipedia.org/wiki/Graph_database
https://tinkerpop.apache.org/gremlin.html
https://en.wikipedia.org/wiki/Gremlin_(programming_language)
https://blog.maximerouiller.com/post/graph-databases-101-with-cosmos-db/

--

--

Caio Moreno
Caio Moreno

Written by Caio Moreno

Solutions Architect @databricks | Professor | PhD | Ex-Microsoft | Ex-Avanade/Accenture | Ex-Pentaho/Hitachi | Ex-AOL | Ex-IT4biz CEO. (Opinions are my own)

No responses yet