Skip to main content

Aerospike Hello World!

For an interactive Jupyter notebook experience: Binder

Hello, World! in Python with Aerospike.

This notebook requires Aerospike database running on localhost and that python and the Aerospike python client have been installed (pip install aerospike). Visit Aerospike notebooks repo for additional details and the docker container.

Ensure database is running

This notebook requires that Aerospike database is running.

!asd >& /dev/null
!pgrep -x asd >/dev/null && echo "Aerospike database is running!" || echo "**Aerospike database is not running!**"


Aerospike database is running!

Import the module

Import the client library.

import aerospike
print("Client module imported")


Client module imported

Configure the client

The configuration is for Aerospike database running on port 3000 of localhost (IP which is the default. Modify config if your environment is different (Aerospike database running on a different host or different port).

config = {
'hosts': [ ('', 3000) ]
print("Configuring with seed host:", config['hosts'])


Configuring with seed host: [('', 3000)]

Create client object and connect to the cluster

client = aerospike.client(config).connect()
import sys
print("Failed to connect to the cluster with", config['hosts'])
print("Connected to the cluster")


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. For a detailed description of the data model see the Data Model overview

key = ('test', 'demo', 'foo')
print('Working with record key ', key)


Working with record key  ('test', 'demo', 'foo')

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.

# Write a record
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)
print('Successfully written the record')


Successfully written the record

Read a record

The record may be retrieved using the same key.

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


Read back the record

Display result

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

  1. The components of the key which are: namespace, set, and userkey. By default userkey is not stored on server, only a hash (appearing as bytearray in the output below) which is the internal representation of the key is stored.
  2. The metadata with the time-to-live and the record's generation or version.
  3. The actual value of the record's bins.
print("Record contents are", record)
print("Key's components are", key)
print("Metadata is", metadata)


Record contents are {'name': 'John Doe', 'age': 32, 'gpa': 4.3, 'greeting': 'Hello, World!'}
Key's components are ('test', 'demo', None, bytearray(b'\xf5~\xc1\x835\xf7\x10\x0c\x04X\xf8\xa6D\xbc\xbcvm\x93G\x1e'))
Metadata is {'ttl': 2592000, 'gen': 2}

Clean up

Finally close the client we created at the beginning.

# Close the connection to the Aerospike cluster
print('Connection closed.')


Connection closed.

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.