Gremlin Query Language


PuppyGraph supports Gremlin query language to effectively retrieve data from the data sources.
Gremlin is a query language developed as part of the Apache TinkerPop, specially designed for traversing graph databases. Gremlin stands out as a functional language where queries are constructed using chained traversal steps, allowing for expressive and intricate data exploration in graph structures. Gremlin has been widely adopted by numerous graph database solutions.
PuppyGraph is a data analytics engine features fast data analytics queries from external data sources. It does not support manipulating the data directly from Gremlin language.

Gremlin console

Apache Tinkerpop provides gremlin client libraries for variety of programming languages. This document uses the official gremlin console as an example to learn the basic Gremlin language.
PuppyGraph provides a quick way to access the gremlin console. If we already followed the Getting Started guide and deployed a local PuppyGraph server, the gremlin console can be accessed from the PuppyGraph web UI. There are other ways to use our own gremlin console, the guides can be found in Gremlin official guide.

Example graph

Following the Getting Started guide, we already setup a demo modern graph data with PuppyGraph that can be used for the rest of the document as an example. You may also use your own graph data.

Hello world

  • g.V() all vertexes.
  • g.E() all edges.
  • g.V().count() total number of vertexes.
  • g.E().count() total number of edges.
gremlin> g.V()
gremlin> g.E()
gremlin> g.V().count()
gremlin> g.E().count()

Basic traversal

Usually graph traversal starts with a single vertex or a set of vertexes. hasStep can be used to filter the vertex and start the traversal.
gremlin> marko = g.V().has('person','name','marko').next()
gremlin> peopleMarkoKnows = g.V().has('person','name','marko').out('knows').toList()
Basic traversal steps are called "vertex steps" in gremlin.
The vertex steps (flatMap) are fundamental to the Gremlin language. Via these steps, its possible to "move" on the graph — i.e. traverse.
  • out(string…​): Move to the outgoing adjacent vertices given the edge labels.
  • in(string…​): Move to the incoming adjacent vertices given the edge labels.
  • both(string…​): Move to both the incoming and outgoing adjacent vertices given the edge labels.
  • outE(string…​): Move to the outgoing incident edges given the edge labels.
  • inE(string…​): Move to the incoming incident edges given the edge labels.
  • bothE(string…​): Move to both the incoming and outgoing incident edges given the edge labels.
  • outV(): Move to the outgoing vertex.
  • inV(): Move to the incoming vertex.
  • bothV(): Move to both vertices.
  • otherV() : Move to the vertex that was not the vertex that was moved from.
The vertex steps can be chained together to form more complex traversals.
gremlin> g.V().has('person','name','marko').out('knows').outE('created').otherV()

Common steps


Filters can be applied along the traversal, has() step can be used to filter on the attributes.
gremlin> g.V().has('person','name','marko').out('knows').outE('created').valueMap()
gremlin> g.V().has('person','name','marko').out('knows').outE('created').has('weight', gt(0.5))
gremlin> g.V().has('person','name','marko').out('knows').outE('created').has('weight', gt(0.5)).otherV()
hasLabel() and hasId() steps are special variants of the has() step.
gremlin> g.V().hasLabel('person')
gremlin> g.V().hasId('software:::v3')
PuppyGraph id has a special format label:::id. The id format helps PuppyGraph to uniquely identify an element.
where() step can be used for another common use case: filter by traversal.
gremlin> personMarkoKnowsHasCreated = g.V().has('person','name','marko').out('knows').where(out('created'))
and() or() step can be used to combine multiple filter by traversals.
gremlin> g.V().and(out('created'), out('knows'))
gremlin> g.V().or(__.in('created'), out('knows'))

Projections and references

Gremlin has many ways to branch a traversal. project() step is a very convenient way to branch the traversal while keeping the reference to all branches.
gremlin> g.V().hasLabel('person').project('person', 'knowsCount', 'createdCount').by(identity()).by(out('knows').count()).by(out('created').count()).order().by('knowsCount', desc)
==>{person=v[person:::v1], knowsCount=2, createdCount=1}
==>{person=v[person:::v4], knowsCount=0, createdCount=2}
==>{person=v[person:::v6], knowsCount=0, createdCount=1}
==>{person=v[person:::v2], knowsCount=0, createdCount=0}
as() and select() step can be used to reference previous steps of the traversal and start from there.
gremlin> personWhoCreatedSoftware = g.V().as('creator').out('created').select('creator').dedup()


path() step can be used to return the whole traversal path (instead of just the end result).
gremlin> g.V().has('person','name','marko').out('knows').outE('created').has('weight', gt(0.5)).otherV().path()
==>path[v[person:::v1], v[person:::v4], e[created:::e10][person:::v4-created->software:::v5], v[software:::v5]]
dedup() step can be used to only return distinct results.
gremlin> g.V().in()
gremlin> g.V().in().dedup()
count() step can be used to return the total count of the result.
gremlin> g.V().has('person','name','marko').out('knows').outE('created').otherV().count()
order() step ben can be used to sort the results.
gremlin> g.V().has('person','name','marko').out('knows').order().by('age')
limit() step can be used to limit the number of the result set.
gremlin> g.V().has('person','name','marko').out('knows').outE('created').otherV().limit(1)
profile() step can be used to printout the query profile.
gremlin> g.V().has('person','name','marko').out('knows').outE('created').otherV().limit(1).profile()
==>Traversal Metrics
Step Count Traversers Time (ms) % Dur
PuppyGraphStep(vertex,[]) 1 1 46.862 99.65
EdgeOtherVertexStep 1 1 0.162 0.35
>TOTAL - - 47.024 -

Official specification

For detailed Gremlin language specification, please refer to the Gremlin official website.
Last modified 4mo ago