Skip to main content

Developer Sandbox

Experiment with your code

Choose your programming language and click Connect to start the server. Once connected, enter your code in the box below and click Run, or shift + , to see the output. Use the terminal tab to issue shell commands and work with the Aerospike tools.

If you encounter any connection errors, refresh the page and reconnect.

Check out the code examples below to get started. For a guided experience, check out our Jupyter Notebook Tutorials.

Code examples

Java

Try intilizing the client and connecting to the cluster, then write a record and read it back with the following code:

// Import the Aerospike 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;

// Establish connection to the server
AerospikeClient client = new AerospikeClient("localhost", 3000);

// Create a key in namespace "test" and set "demo"
Key key = new Key("test", "demo", "putgetkey");

// Create bins to store the record data
Bin bin1 = new Bin("bin1", "Hello,");
Bin bin2 = new Bin("bin2", "World!");

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

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

Python

Try intilizing the the client and connecting to the cluster, then write a record and read it back with the following code:

# Import Aerospike Client
import aerospike

# Define config and connect to cluster
config = {
'hosts': [ ('127.0.0.1', 3000) ]
}

try:
client = aerospike.client(config).connect()
except:
import sys
print("Failed to connect to the cluster with", config['hosts'])
sys.exit(1)

# Define key "foo" in namspace "test" and set "demo"
key = ('test', 'demo', 'foo')

# Write a record
try:
client.put(key, {
'name': 'John Doe',
'age': 32,
'greeting': 'Hello, World!'
})
except Exception as e:
import sys
print("error: {0}".format(e), file=sys.stderr)
sys.exit(1)
print('Successfully written the record')

# Read back the record
(key, metadata, record) = client.get(key)

print("Record contents are", record)
print("Key's components are", key)
print("Metadata is", metadata)

Go

Try intilizing the the client and connecting to the cluster, then write a record and read it back with the following code:

// Import client library
import as "github.com/aerospike/aerospike-client-go/v5"

// Establish connection the server
client, err := as.NewClient("127.0.0.1", 3000)

// Create the key
key, err := as.NewKey("test", "testset", "key1")

// Create the bins
bin1 := as.NewBin("bin1", "value1")
bin2 := as.NewBin("bin2", "value2")

// Write a record
err = client.PutBins(nil, key, bin1, bin2)

// Read a record
record, err := client.Get(nil, key)

Node.js

Try intilizing the the client and connecting to the cluster, then write a record and read it back with the following code:

const Aerospike = require('aerospike')

let config = {
hosts: '127.0.0.1:3000'
}
let key = new Aerospike.Key('test', 'demo', 'demo')

Aerospike.connect(config)
.then(client => {
let bins = {
i: 123,
s: 'hello',
b: Buffer.from('world'),
d: new Aerospike.Double(3.1415),
g: new Aerospike.GeoJSON({type: 'Point', coordinates: [103.913, 1.308]}),
l: [1, 'a', {x: 'y'}],
m: {foo: 4, bar: 7}
}
let meta = { ttl: 10000 }
let policy = new Aerospike.WritePolicy({
exists: Aerospike.policy.exists.CREATE_OR_REPLACE
})

return client.put(key, bins, meta, policy)
.then(() => {
let ops = [
Aerospike.operations.incr('i', 1),
Aerospike.operations.read('i'),
Aerospike.lists.append('l', 'z'),
Aerospike.maps.removeByKey('m', 'bar')
]

return client.operate(key, ops)
})
.then(result => {
console.log(result.bins) // => { c: 4, i: 124, m: null }

return client.get(key)
})
.then(record => {
console.log(record.bins) // => { i: 124,
// s: 'hello',
// b: <Buffer 77 6f 72 6c 64>,
// d: 3.1415,
// g: '{"type":"Point","coordinates":[103.913,1.308]}',
// l: [ 1, 'a', { x: 'y' }, 'z' ],
// m: { foo: 4 } }
})
.then(() => client.close())
.catch(error => {
client.close()
return Promise.reject(error)
})
})
.catch(error => console.log(error))

C

Try intilizing the the client and connecting to the cluster, then write a record and read it back with the following code:

as_error err;
as_config config;
as_config_init(&config);

config.hosts[0] = { .addr = "127.0.0.1", .port = 3000 };

aerospike as;
aerospike_init(&as, &config);

aerospike_connect(&as, &err);

as_key key;
as_key_init(&key, "test", "demo_set", "test_key");

as_record rec;
as_record_inita(&rec, 2);
as_record_set_int64(&rec, "test-bin-1", 1234);
as_record_set_str(&rec, "test-bin-2", "test-bin-2-data");

aerospike_key_put(&as, &err, NULL, &key, &rec);

aerospike_close(&as);
aerospike_destroy(&as);

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.