Aerospike Graph Usage
Overview
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.
The Gremlin Console
Download the latest version of the Gremlin Console from the Apache website.
noteYou must have a Java runtime to use the Gremlin console.
After unzipping the package and navigating to the application folder, start the console with the following command:
./bin/gremlin.sh
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.
Add a new vertex with the
addV
function:g.addV('foo').property('company','aerospike').property('scale','unlimited')
Expected output:
v[-1]
Return the ID of your newly-created vertex:
g.V().has('company','aerospike')
Expected output:
v[-1]
Additional resources
Gremlin uses Groovy as its query language.
Kelvin Lawrence has written a thorough Gremlin manual.
Python client application
Install Gremlin-Python, a Gremlin client library for Python:
pip3 install gremlinpython
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'))
g.addV('foo').property('company','aerospike').property('scale','unlimited')
string = g.V().has('company','aerospike').valueMap(True).to_list()
print(string)
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. 172.17.0.3 is typical for Linux when Aerospike is running in Docker already
// but there may be some variance.
private static final String HOST = "172.17.0.3";
private static final int PORT = 8182;
private static final Cluster.Builder BUILDER = Cluster.build().addContactPoint(HOST).port(PORT).enableSsl(false);
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));
g.io("/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").
out().out().dedup().fold().
project("totalAirportCountFromSFO", "USAirportCountFromSFO").
by(__.unfold().count()).
by(__.unfold().has("country", "US").count()).
next();
System.out.println(SFO2Hop);
cluster.close();
}
}
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:
g.io("/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
Find the airport with code "DFW":
g.V().has('code','DFW')
Find the number of airports in this graph:
g.V().hasLabel('airport').count()
Find the number of flights going out of the airport with code "SFO":
g.V().has('code','SFO').outE().count()
Get all the cities with flights that are > 4000 miles:
g.E().has("dist", P.gt(4000L)).inV().values("city").dedup()
Find all the flights from London Heathrow (LHR) to airports in the USA:
g.V().has('code','LHR').out('route').has('country','US').values('code')
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.