Aerospike Quick Start
Select your programming language at the top of this page to tailor the quick start content to a specific Aerospike client. Checkout the Client Matrix for more information server compatability and client features to determine which client may work best for your application.
In this Quick Start, you will:
- Deploy an Aerospike database
- Set up your development environment
- Connect to the Aerospike database
- Write a record
- Read a record
Prerequisites
This Quick Start assumes a basic understanding of application development in Java, and experience issuing commands in a terminal.Step 1: Deploy Aerospike
If you already have a deployed Aerospike database, skip ahead to Step 2
To get up and running quickly, Docker and Aerospike work together nicely without much effort.
The following command creates a single node Aerospike Enterprise database, using the latest version, fully equipped with a single node feature key and template configuration. No additional setup is needed.
docker run -d --name aerospike -p 3000-3002:3000-3002 aerospike:[LATEST_VERSION]
If you'd like to customize your deployment, check out configuring Aerospike for more information.
Aerospike has a variety of options when it comes to deploying the database.
- AeroLab is an amazing community-supported tool that allows you to quickly spin up development clusters. using Docker, AWS, or GCP.
- Deploy containers directly through Docker, or using our Kubernetes Operator.
- Aerospike supports the three major cloud providers, AWS, GCP, and Azure.
- Bare metal deployments are also available with a variety of Linux distributions as well.
Step 2: 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.
Step 3: 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 )
{
}
}
Step 4: 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.
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
Using the address
and port
from above, instantiate the Aerospike client.
IAerospikeClient client = new AerospikeClient(address, port);
Step 5: Write a record
Create a WritePolicy
to set the totalTimeout
for writes.
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 Aerospike.
try {
client.put(writePolicy, key, bin);
System.out.println("Successfully wrote record");
}
catch (AerospikeException e) {
e.printStackTrace();
}
Step 6: Read a record
Create a Policy
to set the totalTimeout
for reads.
Policy readPolicy = new Policy();
readPolicy.totalTimeout = 5000;
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 Aerospike client usage for more in-depth code samples and content to help you get started!