Gremlin Query Language
Gremlin is JanusGraph’s query language used to retrieve data from and modify data in the graph. Gremlin is a path-oriented language which succinctly expresses complex graph traversals and mutation operations. Gremlin is a functional language whereby traversal operators are chained together to form path-like expressions. For example, "from Hercules, traverse to his father and then his father’s father and return the grandfather’s name."
Gremlin is a component of Apache TinkerPop. It is developed independently from JanusGraph and is supported by most graph databases. By building applications on top of JanusGraph through the Gremlin query language, users avoid vendor-lock in because their application can be migrated to other graph databases supporting Gremlin.
This section is a brief overview of the Gremlin query language. For more information on Gremlin, refer to the following resources:
Practical Gremlin: An online book by Kelvin R. Lawrence providing an in-depth overview of Gremlin and it's interaction with JanusGraph.
Complete Gremlin Manual: Reference manual for all of the Gremlin steps.
Gremlin Console Tutorial: Learn how to use the Gremlin Console effectively to traverse and analyze a graph interactively.
Gremlin Recipes: A collection of best practices and common traversal patterns for Gremlin.
Gremlin Language Variants: Learn how to embed Gremlin in a host programming language.
Gremlin for SQL developers: Learn Gremlin using typical patterns found when querying data with SQL.
In addition to these resources, Connecting to JanusGraph explains how Gremlin can be used in different programming languages to query a JanusGraph Server.
A Gremlin query is a chain of operations/functions that are evaluated from left to right. A simple grandfather query is provided below over the Graph of the Gods dataset discussed in Getting Started.
The query above can be read:
g: for the current graph traversal.
V: for all vertices in the graph
has('name', 'hercules'): filters the vertices down to those with name property "hercules" (there is only one).
out('father'): traverse outgoing father edge’s from Hercules.
- ‘out('father')`: traverse outgoing father edge’s from Hercules’ father’s vertex (i.e. Jupiter).
name: get the name property of the "hercules" vertex’s grandfather.
Taken together, these steps form a path-like traversal query. Each step can be decomposed and its results demonstrated. This style of building up a traversal/query is useful when constructing larger, complex query chains.
1 2 3 4 5 6 7 8 9 10
For a sanity check, it is usually good to look at the properties of each return, not the assigned long id.
1 2 3 4 5 6
Note the related traversal that shows the entire father family tree branch of Hercules. This more complicated traversal is provided in order to demonstrate the flexibility and expressivity of the language. A competent grasp of Gremlin provides the JanusGraph user the ability to fluently navigate the underlying graph structure.
1 2 3
Some more traversal examples are provided below.
1 2 3 4 5 6 7 8 9 10 11 12 13
Given that The Graph of the Gods only has one battler (Hercules), another battler (for the sake of example) is added to the graph with Gremlin showcasing how vertices and edges are added to the graph.
1 2 3 4 5 6 7 8 9 10
When adding a vertex, an optional vertex label can be provided. An edge
label must be specified when adding edges. Properties as key-value pairs
can be set on both vertices and edges. When a property key is defined
with SET or LIST cardinality,
addProperty must be used when adding a
respective property to a vertex.
The example above has 4 chained functions:
name is shorthand for
values('name')). The function
signatures of each are itemized below, where
V is vertex and
any object, where
V is a subset of
out: V -> V
in: V -> V
except: U -> U
values: V -> U
When chaining together functions, the incoming type must match the
outgoing type, where
U matches anything. Thus, the "co-battled/ally"
traversal above is correct.
The Gremlin overview presented in this section focused on the Gremlin-Groovy language implementation used in the Gremlin Console. Refer to Connecting to JanusGraph for information about connecting to JanusGraph with other languages than Groovy and independent of the Gremlin Console.
Iterating the Traversal
One convenient feature of the Gremlin Console is that it automatically iterates all results from a query executed from the gremlin> prompt. This works well within the REPL environment as it shows you the results as a String. As you transition towards writing a Gremlin application, it is important to understand how to iterate a traversal explicitly because your application’s traversals will not iterate automatically. These are some of the common ways to iterate the Traversal:
iterate()- Zero results are expected or can be ignored.
next()- Get one result. Make sure to check
next(int n)- Get the next
nresults. Make sure to check
toList()- Get all results as a list. If there are no results, an empty list is returned.
A Java code example is shown below to demonstrate these concepts:
1 2 3 4 5 6 7 8 9 10 11 12 13