Gremlin: A Graph-Based Programming Language
Monday, August 23, 2010 at 7:12AM |
Derek Stainer In this presentation, Marko Rodriguez, discusses Gremlin, a graph programming language.
What is Gremlin?
Gremlin is a Turing-complete, graph-based programming language developed for key/value-pair multi-relational graphs called property graphs. Gremlin makes extensive use of XPath 1.0 to support complex graph traversals. Connectors exist to various graph databases and frameworks. This language has application in the areas of graph query, analysis, and manipulation.
There are several interesting facts about Gremlin. First, Gremlin is JSR-223 compliant which allows the language itself to be embedded in Java applications, much like Jython, JRuby or Groovy code fragments can. In addition, Gremlin is considered a DSL (domain specific language) and NOT an API. Gremlin is designed to work with several different types of graph databases via Blueprints.
Two important things that Gremlin provides:
- Gremlin provides a means to elegantly map single-relational graph analysis algorithms over to the multi-relational graph domain
- Gremlin provides an elegant way to do automated reasoning in multi-relational graphs using path expressions
Gremlin works with a type of graph called a property graph. A property graph has the following properties:
- Vertices and edges are labeled with unique identifiers
- Edges are directed, labeled, and can form loops
- Multiple edges of the same label can exist for the same vertex pair
- Vertices and edges can have any number of key/value pair properties/attributes
So as an example of a typical Gremlin expression you have the following:
./outE[@label=‘knows’]/inV[matches(@name,‘va.{3}’) and @age > 21]/@name
Here is how this expression would be evaluated:
- Get the current object(s)
- outE[@label=‘knows’]: Get the outgoing edges of the current object(s), where their labels equal ‘knows’
- inV[matches(@name,‘va.{3}’) and @age > 21]: Get the incoming vertices of those ‘knows’ edges, where the names of those vertices are 5 characters long, start with ‘va’, and whose age is greater than 21
- @name: get the name of those particular incoming vertices
The language is simple, easy to understand and very concise. Marko dives into several other examples of expressions, as well as, language specifics like types, expressions and variables.
If you are planning on working with graph databases, take a look at Gremlin.

