Skip to main content

Aerospike Hello World!

For an interactive Jupyter notebook experience: Binder

Hello World! in Java with Aerospike. This notebook requires Aerospike database running locally and that Java kernel has been installed. Visit Aerospike notebooks repo for additional details and the docker container.

Ensure database is running

This notebook requires that Aerospike database is running.

import io.github.spencerpark.ijava.IJava;
import io.github.spencerpark.jupyter.kernel.magic.common.Shell;
IJava.getKernelInstance().getMagics().registerMagics(Shell.class);
%sh asd

Download Aerospike client from POM

%%loadFromPOM
<dependencies>
<dependency>
<groupId>com.aerospike</groupId>
<artifactId>aerospike-client</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies>

Import the modules

Import the client library and other modules.

import com.aerospike.client.AerospikeClient;
import com.aerospike.client.policy.WritePolicy;
import com.aerospike.client.Bin;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.Value;
System.out.println("Client modules imported.");

Output:

Client modules imported.

Initialize the client

Initialize the client and connect to the cluster. The configuration is for Aerospike database running on port 3000 of localhost which is the default. Modify config if your environment is different (Aerospike database running on a different host or different port).

AerospikeClient client = new AerospikeClient("localhost", 3000);
System.out.println("Initialized the client and connected to the cluster.");

Output:

Initialized the client and connected to the cluster.

Understand records are addressable via a tuple of (namespace, set, userkey)

The three components namespace, set, and userkey (with set being optional) form the Primary Key (PK) or simply key, of the record. The key serves as a handle to the record, and using it, a record can be read or written. By default userkey is not stored on the server, only a hash (a byte array, the fourth component in the output below) which is the internal representation of the key is stored. For a detailed description of the data model see the Data Model overview

Key key = new Key("test", "demo", "foo");
System.out.println("Working with record key:");
System.out.println(key);

Output:

Working with record key:
test:demo:foo:f57ec18335f7100c0458f8a644bcbc766d93471e

Write a record

Aerospike is schema-less and records may be written without any other setup. Here the bins or fields: name, age and greeting, are being written to a record with the key as defined above.

Bin bin1 = new Bin("name", "John Doe");
Bin bin2 = new Bin("age", 32);
Bin bin3 = new Bin("greeting", "Hello World!");

// Write a record
client.put(null, key, bin1, bin2, bin3);
System.out.println("Successfully written the record.");

Output:

Successfully written the record.

Read a record

The record can be retrieved using the same key.

// Read the record
Record record = client.get(null, key);
System.out.println("Read back the record.");

Output:

Read back the record.

Display result

Print the record that was just retrieved. We are printing:

  1. The metadata with the record's generation (or version) and expiration time.
  2. The actual value of the record's bins.
System.out.println("Record values are:");
System.out.println(record);

Output:

Record values are:
(gen:3),(exp:351567215),(bins:(name:John Doe),(age:32),(gpa:4.3),(greeting:Hello World!))

Clean up

Finally close the client connection.

client.close();   
System.out.println("Connection closed.");

Output:

Connection closed.

All code in Java boilerplate

All the above code can also be written in the Java boilerplate format and run in a cell.

import com.aerospike.client.AerospikeClient;
import com.aerospike.client.policy.WritePolicy;
import com.aerospike.client.Bin;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.Value;

public class Test{
public static void putRecordGetRecord () {
AerospikeClient client = new AerospikeClient("localhost", 3000);

Key key = new Key("test", "demo", "putgetkey");
Bin bin1 = new Bin("bin1", "value1");
Bin bin2 = new Bin("bin2", "value2");

// Write a record
client.put(null, key, bin1, bin2);

// Read a record
Record record = client.get(null, key);
client.close();
System.out.println("Record values are:");
System.out.println(record);
}
}

Test.putRecordGetRecord()

Output:

Record values are:
(gen:1),(exp:351567216),(bins:(bin1:value1),(bin2:value2))

Next steps

Visit Aerospike notebooks repo to run additional Aerospike notebooks. To run a different notebook, download the notebook from the repo to your local machine, and then click on File->Open, and select Upload.