Skip to main content

Introduction - Python Client

Use the Aerospike Python client to build applications to store and retrieve data from an Aerospike cluster.

The Python client is a CPython module, built on the Aerospike C client.

The Python client for Aerospike works with Python 3.6, 3.7, 3.8, and 3.9 running on 64-bit OS X 10.9+ and Linux.

Code example - connecting the client with the cluster

The example below demostrates the following:

  • Imports the Aerospike Python module
  • Creates a client
  • Connects the client with the cluster
  • Creates a key for the record ID
  • Writes and reads the record
  • Closes the cluster connection with the client

# import the module
from __future__ import print_function
import aerospike

# Configure the client
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)
key = ('test', 'demo', 'foo')

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)

# Read a record
(key, metadata, record) = client.get(key)
print(record)

# Close the connection to the Aerospike cluster
client.close()

The Data Model

At the top of the data model is the namespace container with one set of policy rules for all its data. This is similar to the database concept in an RDBMS, only the Aerospike database distributes the data across the cluster. Namespaces are subdivided into sets, which are similar to RDBMS tables.

Bins store the record data. The data type of the bin is set by the value of the data it contains. Multiple bins and data types can be stored in a single record. Aerospike is schema-less. There is no need to define bins in advance.

Records are uniquely identified by their key. All records appear in the primary index. For grouping records, use the set construct, for fast queries within a set, use the set index.