Skip to content
Webinar - May 13th: How Criteo powers real-time decisions with a reduced footprintRegister now

Quick start

This Quick Start guide walks you through the essentials of deploying an Aerospike Database and building a simple application to interact with it. You’ll learn how to quickly spin up a local development environment using Docker, set up your project in your preferred programming language, connect to the database, and perform basic read and write operations.

Deploy Aerospike with Docker

The quickest and easiest way to get up and running quickly is with Docker.

The following command creates a single node Aerospike Enterprise database, using the latest version, equipped with a single node feature key and template configuration. No additional setup is needed.

Terminal window
docker run -d --name aerospike -p 3000-3002:3000-3002 aerospike/aerospike-server:latest

If you’d like to customize your deployment, see configuring Aerospike for more information. Aerospike has a variety of options when it comes to deploying the database.

  • AeroLab is a community-supported tool for quickly spinning up development clusters.
  • You can deploy containers directly using Docker or Kubernetes Operator.
  • Aerospike supports the three major cloud providers, AWS, GCP, and Azure.
  • Bare metal deployments are available with a variety of Linux distributions as well.

Set up your development environment

Grab your favorite IDE and create a new project.

For this quick start we’ll be using Maven for our build manager. After creating your project, add the following dependency to the pom.xml file.

<dependencies>
<dependency>
<groupId>com.aerospike</groupId>
<artifactId>aerospike-client</artifactId>
<version>7.0.0</version>
</dependency>
</dependencies>

If you’re using a different build manager, or looking for more information on getting started with the Java client, check out the Java client install docs.

Application basics

The following code block contains the necessary imports and basic application shell to get started.

package com.sample;
import com.aerospike.client.IAerospikeClient;
import com.aerospike.client.AerospikeClient;
import com.aerospike.client.AerospikeException;
import com.aerospike.client.Bin;
import com.aerospike.client.Host;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.policy.WritePolicy;
import com.aerospike.client.policy.Policy;
public class App
{
public static void main( String[] args )
{
}
}

Client connection

The minimum information you need to connect to an Aerospike database is the server IP address and port number. If you’re using the basic Docker install from step one, the following information should work just fine.

If your database is deployed with a cloud provider or installed elsewhere, you’ll need information specific to your cluster setup.

Create variables to store the connection information, as well as the namespace and set name.

Using the address and port from above, instantiate the Aerospike client.

String address = "127.0.0.1"; // Aerospike address
Integer port = 3000; // Aerospike port
String namespace = "test"; // Cluster namespace
String set = "foo"; // Set name within namespace
IAerospikeClient client = new AerospikeClient(address, port);

Write a record

  1. Create a WritePolicy to set the totalTimeout for writes.

    WritePolicy writePolicy = new WritePolicy();
    writePolicy.totalTimeout = 5000;
  2. Create the record key, a tuple consisting of namespace, set name, and user defined key

    Key key = new Key(namespace, set, "bar");
  3. Create a bin to store data within the new record

    Bin bin = new Bin("myBin", "Hello World!");
  4. Write the record to Aerospike.

    try {
    client.put(writePolicy, key, bin);
    System.out.println("Successfully wrote record");
    }
    catch (AerospikeException e) {
    e.printStackTrace();
    }

Read a record

  1. Create a Policy to set the totalTimeout for reads.

    Policy readPolicy = new Policy();
    readPolicy.totalTimeout = 5000;
  2. Using the same key from our write command, read the record back.

    try {
    Record record = client.get(readPolicy, key);
    System.out.format("Record: %s", record.bins);
    }
    catch (AerospikeException e) {
    e.printStackTrace();
    }
    finally {
    client.close();
    }

Check out the Develop topic for more in-depth code samples and content to help you get started!

Put it all together

Check out the full code sample
package com.sample;
import com.aerospike.client.IAerospikeClient;
import com.aerospike.client.AerospikeClient;
import com.aerospike.client.AerospikeException;
import com.aerospike.client.Bin;
import com.aerospike.client.Host;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.policy.WritePolicy;
import com.aerospike.client.policy.Policy;
public class App
{
public static void main( String[] args )
{
// ***
// Setup
// ***
String address = "127.0.0.1"; // Aerospike address
Integer port = 3000; // Aerospike port
String namespace = "test"; // Cluster namespace
String set = "foo"; // Set name within namespace
// Create the client and connect to the database
IAerospikeClient client = new AerospikeClient(address, port);
// ***
// Write a record
// ***
// Create a new write policy
WritePolicy writePolicy = new WritePolicy();
writePolicy.totalTimeout = 5000;
// Create the record key
// A tuple consisting of namespace, set name, and user defined key
Key key = new Key(namespace, set, "bar");
// Create a bin to store data within the new record
Bin bin = new Bin("myBin", "Hello World!");
//Write the record to your database
try {
client.put(writePolicy, key, bin);
System.out.println("Successfully wrote record");
}
catch (AerospikeException e) {
e.printStackTrace();
}
// ***
// Read back the record we just wrote
// ***
// Create a new read policy
Policy readPolicy = new Policy();
readPolicy.totalTimeout = 5000;
// Read the record
try {
Record record = client.get(readPolicy, key);
System.out.format("Record: %s", record.bins);
}
catch (AerospikeException e) {
e.printStackTrace();
}
finally {
client.close();
}
}
}
Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?