Gremlin (programming language)

Gremlin is a domain-specific language hosted in Groovy language which itself is a superset of Java.

Gremlin is a graph language. While RDBMS uses JDBC and SQL, graph databases use Blueprints and Gremlin. Gremlin is a style of graph traversal that can be natively used in various JVM languages. Currently, Gremlin provides native support for Java, Groovy, and Scala.[1]

Gremlin works over those graph databases that implement the Blueprints property graph data model. Examples include TinkerGraph, Titan, Neo4j, OrientDB, DEX, Rexster, and Sail RDF Stores.

Syntax

Gremlin query language allows one to mutate the graph and retrieve elements (typically vertices and edges) via graph traversals.

The following query adds two user vertices (Alice and Bob) to the graph and a follows edge going from the bob vertex to the alice vertex, indicating that Bob follows Alice. The last line would retrieve all of Alice's followers (ie. Bob), traversing to all adjacent vertices via all incoming incident follows edges.

// Adds two vertices and an edge to the graph
alice = g.addVertex([type: 'user', name: 'Alice'])
bob = g.addVertex([type: 'user', name: 'Bob'])
g.addEdge(bob, alice, 'follows')
 
// Retrieve followers
g.V('type', 'user').has('name', 'Alice').inE('follows').outV()

Gremlin supports more complex queries such as recommendation engines or shortest path calculation. This query would return a list of five products a specific customer could buy:

g.V('customerId','ALFKI').as('customer')                      \
 .out('ordered').out('contains').out('is').as('products')     \
 .in('is').in('contains').in('ordered').except('customer')    \
 .out('ordered').out('contains').out('is').except('products') \
 .groupCount().cap().orderMap(T.decr)[0..<5].productName

References

  1. "Home · tinkerpop/gremlin Wiki · GitHub". Github.com. Retrieved 2012-03-08.

External links