Skip to content

Gremlin on the Graph

Joshua Shinavier edited this page Mar 25, 2017 · 1 revision

This page will explain how to query over a Semantic Synchrony knowledge graph at the command line using Gremlin.

Step 1: get Gremlin Console

Download Gremlin Console by clicking the link near the top of the Apache TinkerPop homepage and choosing a mirror. This assumes that you are serving your knowledge graph using the latest release of Gremlin Server.

Copy the ZIP file to an appropriate location and uncompress it:

unzip apache-tinkerpop-gremlin-console-*-bin.zip
cd apache-tinkerpop-gremlin-console-*

Step 3: install the Neo4j plugin

Start Gremlin Console with:

bin/gremlin.sh

At the Gremlin-Groovy prompt, first install the Neo4j plugin, then quit. Rather than the 3.2.4 below, use Gremlin Server's current version number.

:install org.apache.tinkerpop neo4j-gremlin 3.2.4
:q

This only needs to be done once.

Step 4: stop Gremlin Server

For the following, we will connect directly to Neo4j, rather than going through a Gremlin Server remote. To keep Gremlin Server and Gremlin Console from fighting over the graph directory, stop Gremlin Server by killing its process. You can restart it later after you exit Gremlin Console.

Step 5: connect to your graph

Start Gremlin Console again. At the Gremlin-Groovy prompt, copy and paste the following, using the actual path to your Neo4j directory:

:plugin use tinkerpop.neo4j
graph = Neo4jGraph.open("/path/to/smsn-neo4j")
g = graph.traversal()

Step 6: query!

Now you can execute Gremlin queries over your knowledge graph. Try:

// find the oldest atoms in your graph
g.V().has('created').order().by('created').limit(10).values('value')

or

// find the atoms with the highest in-degree
g.V().order().by(inE('first').count(), decr).limit(10).values('value')  

or

// look up a specific atom by id (use "C-c r" in Brain-mode to copy an atom's id to the clipboard)
g.V().has('idV', 'c0VSSAMqOQSYFV2f').properties()

// show the children of that atom, in order
g.V().has('idV', 'c0VSSAMqOQSYFV2f').out('notes').repeat(out('rest')).emit().out('first').values('value')

Happy traversing...