Skip to main content

Simple Put-Get Example

For an interactive Jupyter notebook experience: Launch in Binder#

A simple example of put and get calls in Aerospike.

This notebook requires Aerospike datbase running locally 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.

Import the module#

The Aerospike client must be imported.

import aerospike

Configure the client#

This configuration is for aerospike running on port 3000 of localhost which is the default. If your environment is different (Aerospike server running on a different host or different port, etc)

config = {  'hosts': [ ('127.0.0.1', 3000) ]}

Create a client and connect it to the cluster#

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

Records are addressable via a tuple of (namespace, set, key)#

These three components (with set being optionsl) form the key. Using this key records may be read or written. For a detailed description of the data model see the Data Model overview

key = ('test', 'demo', 'foo')

Writing a record#

Aerospike is schema-less and records may be written without any other setup. Here a record with two bins (name and age) is being written to a record with they key defined above.

try:  # Write a record  client.put(key, {    'name': 'John Doe',    'age': 32  })except Exception as e:  import sys  print("error: {0}".format(e), file=sys.stderr)

Reading a record#

This same record may be retrieved using the same key.

(key, metadata, record) = client.get(key)

Display result#

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

  1. The components of the key which are: namespace, the set, a userkey (by default there is no user key), and a hash which is the internal representation of the key.
  2. The metadata with the time to live and the record's generation.
  3. The actual value of the record with two bins.

Lastly it is important to clean up the client we created at the beginning.

print("record contents are", record)print("key components are", key)print("metadata is", metadata)# Close the connection to the Aerospike clusterclient.close()