Skip to main content

Aerospike Graph Usage


There are several options for connecting to and interacting with Aerospike Graph. Some possibilities are:

  • The Gremlin Console, an interactive command-line terminal for sending queries and receiving responses.

  • A client application. This page provides code samples for Python and Java client code.

  • A Jupyter Notebook.

The Gremlin Console

  1. Download the latest version of the Gremlin Console from the Apache website.


    You must have a Java runtime to use the Gremlin console.

  2. After unzipping the package and navigating to the application folder, start the console with the following command:

  3. Connect to the Aerospike Graph Service via the Graph Docker image. See the installation instructions if you haven't started a Graph Docker image:

g = traversal().withRemote(DriverRemoteConnection.using("GREMLIN_SERVER_IP_ADDRESS", 8182, "g"));

Expected output:

graphtraversalsource[emptygraph[empty], standard]

Replace GREMLIN_SERVER_IP_ADDRESS with the accessible IP address of your Aerospike Graph Docker image.

  1. Add a new vertex with the addV function:


    Expected output:

  2. Return the ID of your newly-created vertex:


    Expected output:


Additional resources

Python client application

  1. Install Gremlin-Python, a Gremlin client library for Python:

    pip3 install gremlinpython
  2. Gremlin-Python implements many of the functions found in Gremlin. The following example code establishes a connection with a remote Gremlin server, creates a new vertex, and reads it back:

    from gremlin_python.process.anonymous_traversal import traversal
    from gremlin_python.process.traversal import IO
    from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

    if __name__ == '__main__':
    # Create GraphTraversalSource to remote server.
    g = traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin', 'g'))
    string = g.V().has('company','aerospike').valueMap(True).to_list()

Java client application

The following example Java code uses the air-routes data set, which is included with the Aerospike Graph Docker image. See the installation instructions if you haven't started a Aerospike Graph Docker image.

package com.aerospike.firefly.benchmark;

import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
import org.apache.tinkerpop.gremlin.process.traversal.IO;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;

import java.util.Map;
import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;

public class FireflySample {
// Use the command: docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER_ID
// to get the HOST IP address below. is typical for Linux when Aerospike is running in Docker already
// but there may be some variance.
private static final String HOST = "";
private static final int PORT = 8182;
private static final Cluster.Builder BUILDER =;

public static void main(final String[] args) {
System.out.println("Creating the Cluster.");
final Cluster cluster = BUILDER.create();
System.out.println("Creating the GraphTraversalSource.");
final GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster));"/opt/air-routes/air-routes-50k.graphml").with(IO.reader, IO.graphml).read().iterate()

// We can use .next() to terminate this traversal because it only returns 1 item
Map<String, Object> SFO2Hop = g.V().
has("code", "SFO").
project("totalAirportCountFromSFO", "USAirportCountFromSFO").
by(__.unfold().has("country", "US").count()).

The air-routes data set

Kelvin Lawrence has compiled and made public a data set containing information about airlines, airports around the world, and routes between them, designed for use with a graph database. The data set is large enough to be interesting and useful, but small enough to be practical for testing and experimentation purposes. It is included with the Aerospike Graph Docker image, in the /opt/air-routes/ directory.

You can use the Gremlin console to load the air-routes data set into Aerospike Graph with the following command:"/opt/air-routes/air-routes-50k.graphml").with(IO.reader, IO.graphml).read()

The air-routes data set may take a few minutes to load, depending on your host hardware and network configuration.

Sample queries

  1. Find the airport with code "DFW":

  2. Find the number of airports in this graph:

  3. Find the number of flights going out of the airport with code "SFO":

  4. Get all the cities with flights that are > 4000 miles:

  5. Find all the flights from London Heathrow (LHR) to airports in the USA:

  6. Find all the unique locations in the world and in the US that I can get to from SFO through a 2 hop flight:

    g.V().has("code", "SFO").out().out().dedup().fold().project("totalAirportCountFromSFO",    "USAirportCountFromSFO").by(__.unfold().count()).by(__.unfold().has("country", "US").count())

To get performance metrics for a query, append .profile() to the end of the command.