Introduction to Gremlin

Posted by n3integration on August 1, 2015

Gremlin is a domain-specific language for querying or mutating the contents of a graph database. While other graph query languages exist, the advantage of Gremlin is that it is supported across the major graph databases: Neo4j , OrientDB, and Titan Graph DB.

Graph Primer

In a graph there are two basic types: Vertex (or node) and Edge (or relationship). A Vertex can have 1..* properties, whereas an Edge has a single label.

Gremlin Usage

Add a new Vertex to the graph

gremlin> v1 = g.addVertex([firstname: 'John', lastname: 'Miller'])
gremlin> v2 = g.addVertex([firstname: 'James', lastname: 'Ryan'])

Query all Vertices in a graph

gremlin> g.V

Filter a Vertex by its unique identifier

gremlin> g.v(<id>)

Add a property to an existing Vertex

gremlin> g.v(<id>).setProperty('rank', 'private')

Display all the properties of an existing Vertex

gremlin> g.v(<id>).map

Display the value of a single Vertex property

gremlin> g.v(<id>).property('rank')

Filter all Vertices that have a property with a given value

gremlin> g.V.has('rank', 'private')

Filter all Vertices that have a numeric property in a given range

gremlin> g.V.has('property', T.gte, <value>)
gremlin> g.V.interval('property', <min>, <max>)

Filter out duplicate results

gremlin> g.V.dedup()

Add a new Edge to the graph

gremlin> e1 = g.addEdge(v1, v2, 'in_search_of')

Query all Edges in a graph

gremlin> g.E

Filter an Edge by its unique identifier

gremlin> g.e(<id>)

Find all of the incoming/outgoing relationships from a given Vertex

gremlin> g.v(<id>).inE
gremlin> g.v(<id>).outE

Find all related incoming/outgoing Vertices

gremlin> g.v(<id>).in
gremlin> g.v(<id>).out

Find all related incoming/outgoing Vertices of a given relationship

gremlin> g.v(<id>).in('in_search_of')
gremlin> g.v(<id>).out('in_search_of')

Remove a Vertex or an Edge

gremlin> g.v(<id>).remove()
gremlin> g.removeVertex(g.v(<id>))
gremlin> g.e(<id>).remove()
gremlin> g.removeEdge(g.e(<id>))

Gremlin also provides two basic aggregate functions (i.e. count and mean)

gremlin> g.V.count()
gremlin> g.V.<numeric property>.mean()

Gremlin feature reference