From BITPlan Wiki
Jump to: navigation, search
Gremlin programming language.png
Gremlin is the graph traversal language of Apache TinkerPop. Gremlin is a functional, data-flow language that enables users to succinctly express complex traversals on (or queries of) their application's property graph. Every Gremlin traversal is composed of a sequence of (potentially nested) steps. A step performs an atomic operation on the data stream. Every step is either a map-step (transforming the objects in the stream), a filter-step (removing objects from the stream), or a sideEffect-step (computing statistics about the stream). The Gremlin step library extends on these 3-fundamental operations to provide users a rich collection of steps that they can compose in order to ask any conceivable question they may have of their data for Gremlin is Turing Complete.

Explaining Gremlin

There are different level on which gremlin can be explained:

  1. Mathematical background
  2. Generic API
  3. Specific API (Java)
  4. Specific Example


One of the core concepts of tinkerpop/gremlin is the GraphTraversal It's interface has a generic definition as:

public interface GraphTraversal<S,E> extends Traversal<S,E>

and at the Author Marko Rodriguez explains the ideas behind using an generic approach vor handling Graphs. The Java implementation is available on github.

S is a generic Start class, and E is a generic End class as explained in the Apache Tinkerpop documentation.

A graph Traversal is the starting point for working with a graph. The convention is to name it




The Modern example

The "modern" graph is shipped with gremlin as a standard example. tinkerpop-modern.png

The graph has 6 edges and 6 vertices.

JUnit Testcase

  public void testTraversal() {
    Graph graph = TinkerFactory.createModern();
    GraphTraversalSource g = graph.traversal();

E() gives you access to the edges of a graph traversal. V() gives you access to the vertices of a graph traversal. In the above example we simply cound the edges and vertices and check our assumption that there are 6 edges and 6 vertices in the modern example graph.


As explained in Gremlin_Basics: "The Gremlin graph traversal language defines approximately 30 steps which can be understood as the instruction set of the Gremlin traversal machine. These steps are useful in practice, with typically only 10 or so of them being applied in the majority of cases. Each of the provided steps can be understood as being a specification of one of the 5 general types enumerated below". step-types.png

General Steps

filter Step

Continues processing based on the given filter condition.

JUnit Test

  public void testFilter() {

There are 3 vertices having outgoing edges and 4 vertices having incoming edges in the modern example graph. There are 4 edges having a weight>=0.4;

map Step

A map step transforms the current step element to a new element (which may be empty). see also

JUnit Test

  public void testMap() {
    List<Edge> outEdges = g().V().map(outE()).toList();
    List<Object> edges = g().E().map(has("weight",0.4)).toList();
    for (Object edge:edges) {
      assertTrue(edge instanceof Edge);

There are 6 vertices having a name property. There are 4 vertices with a "person" label. There are 2 vertices with the lang property having the value "java".There are 3 vertices having out edges. The toList() call returns a list of Edges. There are 2 edges having a weight of 0.4. The map step toList() returns a list of the edges for this last example (which are returned as generic objects).

flatMap Step

A flatMap step transforms the current step in a one to many fashion.

JUnit Test

  public void testflatMap() {
    List<Edge> outEdges = g().V().flatMap(outE()).toList();
    List<Object> edges = g().E().flatMap(has("weight",0.4)).toList();
    for (Object edge:edges) {
      assertTrue(edge instanceof Edge);

Note the difference to the testMap step. Only the outE() parameter behaves different. In the map() case only the first Edge is considered - in the flatMap case all edges are considered.

sideEffect Step

A sideEffect steps performs some operation on the traverser and passes it to the next step.

JUnit Test

  public void testSideEffect() {

The sideffect in this example JUnit test case adds edges "on the fly".

branch Step

Split the traverser

JUnit Test

  public void testBranch() {

What links here


Stackoverflow Questions


Practical Gremlin: An Apache TinkerPop Tutorial by Kelvin Lawrence

load PDF

Traversing Graphs with Gremlin