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.
The Aerospike client must be imported.
import aerospike
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) ]
}
try:
client = aerospike.client(config).connect()
except:
import sys
print("failed to connect to the cluster with", config['hosts'])
sys.exit(1)
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')
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)
This same record may be retrieved using the same key.
(key, metadata, record) = client.get(key)
Print the record that was just retrieved. We are also printing:
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 cluster
client.close()