Gremlin Graph database statements

By Brian Fitzgerald

Introduction

This blog post covers some basic Gremlin data operations on a graph database. The database platforms tested are TinkerPop implementations in Azure Cosmos DB and AWS Neptune.

Initialization

Azure

Create a Cosmos DB account. For API, select “Gremlin (graph)”.

In data explorer, create a new graph.

AWS

Create a Neptune database and an EC2 instance. Setup your security group. Connect via gremlin.sh as described in the manual.

Insert Data

All statements in this blog post will run as well on Azure and AWS, but there are differences in the output display.

Add vertices.

g.addV('person').property('firstName', 'Marko')
g.addV('person').property('firstName', 'Stephen')
g.addV('person').property('firstName', 'Ben')
g.addV('person').property('firstName', 'Mary')

Add edges.

g.V().has('firstName','Marko').addE('knows').to(g.V().has('firstName','Stephen'))
g.V().has('firstName','Marko').addE('knows').to(g.V().has('firstName','Ben'))
g.V().has('firstName','Marko').addE('knows').to(g.V().has('firstName','Mary'))

Update data (set properties)

Set vertex properties

g.V().has('person','firstName','Marko').property('lastName','Rodriguez')

The output in this section is from  Azure Cosmos DB Data explorer:

[
  {
    "id": "7cf848d1-0b87-412c-87fe-267e6f259a86",
    "label": "person",
    "type": "vertex",
    "properties": {
      "firstName": [
        {
          "id": "b45eb2be-a0b7-4c55-a956-85b985b88b35",
          "value": "Marko"
        }
      ],
      "lastName": [
        {
          "id": "a28f96e7-8e17-4be7-8a15-a3a0d3aeba87",
          "value": "Rodriguez"
        }
      ]
    }
  }
]

Notice that each vertex property has an “id” field. In this example, the “lastName” “id” is “a28f96e7-8e17-4be7-8a15-a3a0d3aeba87”. If you update a property, even by setting it to the same value, the “id” changes.

g.V().has('person','firstName','Marko').property('lastName','Rodriguez')
[
  {
    "id": "7cf848d1-0b87-412c-87fe-267e6f259a86",
    "label": "person",
    "type": "vertex",
    "properties": {
      "firstName": [
        {
          "id": "b45eb2be-a0b7-4c55-a956-85b985b88b35",
          "value": "Marko"
        }
      ],
      "lastName": [
        {
          "id": "0d5e4855-4d78-45d9-85c2-de774ca21e87",
          "value": "Rodriguez"
        }
      ]
    }
  }
]

Now the “lastName” “id” is “0d5e4855-4d78-45d9-85c2-de774ca21e87”

Set edge properties

Add a weight property to each edge.

g.V().has('person','firstName','Marko').outE('knows').as('e').inV().has('firstName','Stephen').select('e').property('weight', 0.055)
g.V().has('person','firstName','Marko').outE('knows').as('e').inV().has('firstName','Ben').select('e').property('weight', 0.075)
g.V().has('person','firstName','Marko').outE('knows').as('e').inV().has('firstName','Mary').select('e').property('weight', 0.075)

Notice that edge properties to not have an “id” attribute:

g.V().has('person','firstName','Marko').outE('knows').as('e').inV().has('firstName','Stephen').select('e')
[
  {
    "id": "a37cdd6d-9e72-44f9-b36a-256f824eba64",
    "label": "knows",
    "type": "edge",
    "inVLabel": "person",
    "outVLabel": "person",
    "inV": "a9067638-e56d-4fd0-9522-c955c4062fa3",
    "outV": "7cf848d1-0b87-412c-87fe-267e6f259a86",
    "properties": {
      "weight": 0.055
    }
  }
]

Search

Search for Stephen

g.V().has('person','firstName','Stephen')

Azure Cosmos DB

Here is what the output looks like in Azure Cosmos DB Data Explorer.

[
  {
    "id": "a9067638-e56d-4fd0-9522-c955c4062fa3",
    "label": "person",
    "type": "vertex",
    "properties": {
      "firstName": [
        {
          "id": "3ac1eb02-6255-4787-a32c-2b61359f127c",
          "value": "Stephen"
        }
      ]
    }
  }
]

AWS

By comparison, here is gremlin console display.

gremlin> g.V().has('person','firstName','Stephen')
==>v[32b57f9f-703e-722a-ac4c-2cca662a7bd9]

Only the vertex id is displayed.

Here are some more search queries. Find one edge from Marko with the highest weight.

g.V().has('person','firstName','Marko').outE('knows').order().by('weight', decr).limit(1)

[
  {
    "id": "7c13d3a5-efca-4fc6-8ef4-38924e80c5eb",
    "label": "knows",
    "type": "edge",
    "inVLabel": "person",
    "outVLabel": "person",
    "inV": "1bcbdc44-6b0d-47c6-bbdb-96665b034847",
    "outV": "7cf848d1-0b87-412c-87fe-267e6f259a86",
    "properties": {
      "weight": 0.075
    }
  }
]

Find one vertex adjacent to Marko connected by an edge with the highest weight.

g.V().has('person','firstName','Marko').outE('knows').order().by('weight', decr).limit(1).inV().properties('firstName').value()
[
  {
    "id": "1bcbdc44-6b0d-47c6-bbdb-96665b034847",
    "label": "person",
    "type": "vertex",
    "properties": {
      "firstName": [
        {
          "id": "ebdf5e09-c511-472f-8b14-405fad956146",
          "value": "Ben"
        }
      ]
    }
  }
]

Notice that that vertex “Ben” id “1bcbdc44-6b0d-47c6-bbdb-96665b034847” matches edge inV.

Drop vertices

Drop one vertex.

g.V().has('firstName','Mary').drop()

Drop all vertices

g.V().drop()

All edges get dropped as well.

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