Over a million developers have joined DZone.

Using Gremlin with Neography

DZone's Guide to

Using Gremlin with Neography

· Database Zone ·
Free Resource

MariaDB TX, proven in production and driven by the community, is a complete database solution for any and every enterprise — a modern database for modern applications.

Gremlin is a domain specific language for traversing property graphs. Neo4j is one of the databases that can speak the gremlin language, and as promised I’ll show you how you can use it to implement friend recommendations as well as degrees of separation.

We can send any gremlin script to Neo4j via the REST API and neography using the execute_script command. Let’s implement suggestions_for so it sends a gremlin script to the server:

def suggestions_for(node)
  node_id = node["self"].split('/').last
                         filter{it != g.v(node_id)}.
                         name", {:node_id => node_id})

puts "Johnathan should become friends with #{suggestions_for(johnathan).join(', ')}"

# Johnathan should become friends with Mary, Phil

Let’s go through the gremlin steps


is our graph.


is the vertex with the id gathered from node_id (which will be passed as a parameter later). In gremlin a node is a vertex, and a relationship is an edge.


tells gremlin we want to traverse incoming relationships of type “friends” and we want to do this twice since we’re going to get friends of friends.


removes any duplicate nodes we found along the way… you know the popular kids.


grabs the name property of the nodes we found. You want to parameterize your script when possible to avoid re-parsing and improve performance.

How about degrees of separation?

def degrees_of_separation(start_node, destination_node)
  start_node_id = start_node["self"].split('/').last
  destination_node_id = destination_node["self"].split('/').last
                         in.loop('x'){it.loops <= 4 & 
                                      it.object.id != destination_node_id}.
                         filter{it.id == destination_node_id}.
                         paths{it.name}", {:start_node_id => start_node_id,
                                           :destination_node_id => destination_node_id })

degrees_of_separation(johnathan, mary).each do |path|
  puts "#{(path.size - 1 )} degrees: " + path.join(' => friends => ') 

# 3 degrees: Johnathan => friends => Mark => friends => Phil => friends => Mary
# 2 degrees: Johnathan => friends => Mark => friends => Mary

This one is a bit more tricky. We once again start with our graph


then go to the start node (or vertex) 


we are going to name this step as x with the 


command because we’ll want to come back here later.


tells gremlin to traverse incoming relationships and keep looping but go back to x when the condition that follows becomes false. Gremlin keeps track of the number of loops you’ve taken and here we tell it to stop at 4 

it.loops <= 4

and stop if we reach our destination along the path

it.object.id != destination_node_id}

We avoid repeating elements in the path with


and we filter the results that end in our destination node with

filter{it.id == destination_node_id}

and get the names of the nodes in the path with 


We then pass our parameters

{:start_node_id => start_node_id,
 :destination_node_id => destination_node_id }

Gremlin is very powerful and is aided by the whole Tinkerpop stack. You’ll need some time and a little something to get you in right state of mind but if you can grok it then reading the gremlin wiki will expand your mind. If you prefer to watch instead of read, this video will give you a pretty good introduction to the Gremlin language.

MariaDB AX is an open source database for modern analytics: distributed, columnar and easy to use.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}