Code Sandbox
Overview
This is a sandbox application where you can write client code in several languages and interact with a remotely-hosted Aerospike Database instance. The database includes sample data that tracks UFO sightings.
Experiment with your code
The default namespace for the sandbox is sandbox
and there are 5,000 records loaded into the set ufodata
.
Check out the code samples below or add your own data to get started.
Code Editor
Terminal
- Blank
- Setup
- Create
- Read
- Update
- Delete
xxxxxxxxxx
// Drop your own code in here
// The `sandbox` namespace is prepopulated with records
// and the `test` namespace is empty
Console
Select your language and server version, then click connect to get started
First time here?
Our sandbox lets you run code and interact with an Aerospike enterprise cluster, right from your browser.
Code Samples
Java
- Setup
- Create
- Read
- Update
- Delete
- Shell
Setup your environment by importing the Aerospike client library and other modules.
// Import the Aerospike client library and other modules
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;
import java.util.List;
import com.aerospike.client.AerospikeClient;
import com.aerospike.client.AerospikeException;
import com.aerospike.client.policy.Policy;
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;
import com.aerospike.client.cdt.MapOrder;
import com.aerospike.client.cdt.MapPolicy;
import com.aerospike.client.cdt.MapOperation;
import com.aerospike.client.policy.RecordExistsAction;
// This is for formatting output in the sandbox
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Gson gson = new GsonBuilder().setPrettyPrinting().create();
// Define base policy
Policy policy = new Policy();
policy.sendKey = true;
System.out.println("Setup complete");
Write a new record into the UFO tracking database.
// Establish connection to the server
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
// Initialize a write policy
WritePolicy writePolicy = new WritePolicy(policy);
// Create a key in namespace "sandbox" and set "ufodata"
Key key = new Key("sandbox", "ufodata", 5001);
// Create a list of shapes to add to the report map
ArrayList<String> shape = new ArrayList<String>();
shape.add("circle");
shape.add("flash");
shape.add("disc");
// Create the report map
Map reportMap = new TreeMap<String, Object>();
reportMap.put("city", "Ann Arbor");
reportMap.put("state", "Michigan");
reportMap.put("shape", shape);
reportMap.put("duration", "5 minutes");
reportMap.put("summary", "Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!");
// Format coordinates as a GeoJSON string
// GeoJSON takes coordinates as (Longitude, Lattitude)
private String generatePoint(double lat, double lng){
return String.format("{\"type\":\"Point\",\"coordinates\":[%f,%f]}", lng, lat);
}
// Create the bins as Bin("binName", value)
Bin occurred = new Bin("occurred", 20220531);
Bin reported = new Bin("reported", 20220601);
Bin posted = new Bin("posted", 20220601);
Bin report = new Bin("report", reportMap, MapOrder.KEY_ORDERED);
Bin location = new Bin("location", Value.getAsGeoJSON(generatePoint(42.2808,83.7430)));
// Write the record to Aerospike
try {
client.put(writePolicy, key, occurred, reported, posted, report, location);
Record record = client.get(null, key);
System.out.format("Create succeeded\nKey: %s\nRecord: %s\n", key.userKey, gson.toJson(record.bins));
}
catch (AerospikeException ae) {
System.out.format("Create Failed\nError: %s", ae.getMessage());
}
finally {
// Close the client
client.close();
}
Read a record from the UFO tracking database.
// Establish connection to the server
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
// Read all bins from a record
try{
Record record = client.get(null, key);
if(record != null){
System.out.format("Key: %s\nRecord: %s", key.userKey, gson.toJson(record.bins));
}
else{
System.out.format("Key not found\nKey: %s", key.userKey);
}
}
catch (AerospikeException ae) {
System.out.format("Error: %s", ae.getMessage());
}
finally {
// Close the client
client.close();
}
Read specific bins from a record.
// Establish connection to the server
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
// Read specific bins from a record
try {
Record record = client.get(null, key, "report", "location");
if(record != null){
System.out.format("Key: %s\nRecord: %s", key.userKey, gson.toJson(record.bins));
}
else{
System.out.format("Key not found\nKey: %s", key.userKey);
}
}
catch (AerospikeException ae) {
System.out.format("Error: %s", ae.getMessage());
}
finally {
// Close the client
client.close();
}
Batch read all bins from multiple records.
// Establish connection to the server
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
// Create the keys
Key[] keys = new Key[10];
for (int i = 0; i < 10; i++) {
keys[i] = new Key("sandbox", "ufodata", (i + 1));
}
// Read the records
try {
Record[] records = client.get(null, keys);
for (int i = 0; i < records.length; i++) {
if (records[i] != null) {
System.out.format("Key: %s\nRecord: %s\n", keys[i].userKey, gson.toJson(records[i].bins));
}
else {
System.out.format("Key not found\nKey: %s", keys[i].userKey);
}
}
}
catch (AerospikeException ae) {
System.out.format("Error: %s", ae.getMessage());
}
finally {
// Close the client
client.close();
}
Update a record in the UFO tracking database.
The put
operation handles Create (Insert) and Update. The recordExistsAction
specified within the write-policy defines the operation semantics when the record already exists, with the following variants:
CREATE_ONLY
: Create if record doesn't exist already.UPDATE
: Create if record doesn't exist, update otherwise.UPDATE_ONLY
: Update only, if record exists.REPLACE
: Create if record doesn't exist, replace otherwise.REPLACE_ONLY
: Replace only, if record exists.
Using the write policy from the record creation, update the posted
bins value for the record only if it exists. Try changing the policy to get different results.
// Establish connection to the server
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
// Set the update policy on the write policy from record creation
writePolicy.recordExistsAction = RecordExistsAction.UPDATE_ONLY;
// Update the record
try {
posted = new Bin("posted", 20220602);
client.put(writePolicy, key, posted);
Record record = client.get(null, key, "posted");
System.out.format("Update succeeded\nBin: %s\n", gson.toJson(record.bins));
}
catch (AerospikeException ae) {
System.out.format("Update Failed\nError: %s", ae.getMessage());
}
finally {
// Close the client
client.close();
}
A type specific update could also be used for integer and string types. Set the posted
value back to it's original value with an add operation.
// Establish connection to the server
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
// Update the record
try{
posted = new Bin("posted", -1);
client.add(writePolicy, key, posted);
Record record = client.get(null, key, "posted");
System.out.format("Update succeeded\nBin: %s\n", gson.toJson(record.bins));
}
catch (AerospikeException ae) {
System.out.format("Update Failed\nError: %s", ae.getMessage());
}
finally {
// Close the client
client.close();
}
Delete a record from the UFO tracking database.
Delete a record using the defaults.
By default, Aerospike deletes data in a very efficient way, reclaiming the memory instantaneously by only dropping the primary index entry for the deleted object.
// Establish connection to the server
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
// Delete the record
try {
client.delete(writePolicy, key);
System.out.format("Record deleted\n");
// Check if record exists
boolean exists = client.exists(null, key);
System.out.format("Exists: %b\n", exists);
}
catch (AerospikeException ae) {
System.out.format("Delete Failed\nError: %s", ae.getMessage());
}
finally {
// Close the client
client.close();
}
Delete a record using durable delete policy.
When a durable delete is issued a tombstone is written. The tombstone-write is persisted to disk along with other similarities to a record-update.
// Establish connection to the server
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
// Enable durable delete on the write policy from record creation
writePolicy.durableDelete = true;
// Delete the record
try {
client.delete(writePolicy, key);
System.out.format("Record deleted\n");
// Check if record exists
boolean exists = client.exists(null, key);
System.out.format("Exists: %b\n", exists);
}
catch (AerospikeException ae) {
System.out.format("Delete Failed\nError: %s", ae.getMessage());
}
finally {
// Close the client
client.close();
}
Checkout durable deletes for more information on deleting records.
The Terminal tab of the sandbox allows you to run shell commands and utilize Aerospike tools.
asadm
Aerospike Admin is an interactive Python utility used for getting summary info for the current health of a cluster, administering the access control list (ACL), managing user defined functions (UDFs), managing secondary indexes (sindex), changing dynamic configurations, and tuning commands across the cluster.
Open the asadm prompt with:
asadm
then, get some stats for the ufodata set in the sandbox namespace.
show statistics set for sandbox ufodata
You can enter privileged mode to run management and asinfo commands.
enable
Then at the asadm+
prompt we can manage our cluster. Create a secondary index in the Aerospike cluster.
manage sindex create numeric occurred_idx ns sandbox set ufodata bin occurred
New for Aerospike Server 6.1
Aerospike Server 6.1 adds support for index cardinality information. Run the following asadm command to see statistics on the occurred_idx
created above.
show statistics sindex from sandbox occurred_idx
Check out Aerospike Admin to learn more.
asinfo
asinfo is a command-line utility that provides an interface to Aerospike's cluster command and control functions. This includes the ability to change server configuration parameters while the Aerospike is running. We can use asinfo to get the same statistics that asadm provided.
asinfo -v "sets/sandbox/ufodata" -l
Or dynamically set configuration parameters.
asinfo -h 127.0.0.1 -p 3000 -v "log-set:id=0;migrate=debug" # Set the migrate log level to debug
Configurations set through asinfo will only persist until the server is restarted. Add them to the configuration file if the change requires persistence.
Check out asinfo to learn more.
AQL
AQL is a data browser for examination of the DB and a tool for lightweight administrative tasks for maintaining.
Open the AQL prompt with:
aql
then use AQL to browse data, whether it be all records or specific records.
SELECT * FROM sandbox.ufodata WHERE PK=5001
Or, using the secondary index created through asadm, filter on indexed bins.
SELECT * FROM sandbox.ufodata WHERE occurred BETWEEN 20200101 AND 20200201
Check out AQL to learn more.
Next steps
Visit the Aerospike notebooks repository to run additional Aerospike notebooks.
Explore the sample data locally
Download the sandbox dataset from the Interactive Notebooks GitHub repository. Sandbox Dataset
This file is a full database backup made with the Aerospike Backup tool. You can transfer the data to a new Aerospike Database instance, regardless of version, by restoring this backup with Aerospike Restore.