Aerospike Python Client
aerospike is a package which provides a Python client for Aerospike database clusters. The client enables applications to interact with Aerospike, managing connections and handling database operations like CRUD, queries, and scans. It is a CPython module built on the Aerospike C client. The current version is 19.2.0, and the library maintains an active release cycle with frequent updates and major version increments.
Common errors
-
ModuleNotFoundError: No module named 'aerospike'
cause The Aerospike Python client is not installed in the current Python environment.fixInstall the Aerospike client using pip: `pip install aerospike`. -
AttributeError: 'module' object has no attribute 'client'
cause The script is named 'aerospike.py', causing a conflict with the Aerospike module import.fixRename the script to avoid naming conflicts, e.g., 'aerospike_test.py'. -
AttributeError: 'aerospike.Client' object has no attribute 'info_all'
cause The method 'info_all' does not exist in the Aerospike Python client.fixUse the correct method 'info' instead: `client.info('sets')`. -
aerospike.exception.ClientError: (-10, 'Failed to connect')
cause The Python client failed to establish a connection to the Aerospike cluster. Common causes include incorrect host/port configuration, the Aerospike server not running, network connectivity issues (e.g., firewall), or an incompatibility between client and server versions (e.g., Python client 5.0.0+ requires Aerospike server 4.9+).fix1. Verify the `hosts` (IP address and port, typically 3000) in your client configuration. 2. Ensure the Aerospike server is running and accessible from the client machine. 3. Check firewall rules blocking the connection. 4. If using Python client version 5.0.0 or newer, ensure your Aerospike server is version 4.9 or later.
Warnings
- breaking Aerospike Python client 5.0.0 and up requires Aerospike server 4.9 or later. Attempting to connect to older server versions will result in a '-10, Failed to connect' error.
- breaking Support for older Python versions is periodically removed. Python 3.5 support was removed in client 6.0.0. Python 3.8 and 3.9 support was removed in recent major versions (e.g., Python 3.9 in 19.0.0).
- deprecated Direct instantiation of `aerospike.Scan()` and `aerospike.Query()` objects is deprecated. These methods now issue a warning and will raise an exception in future major releases.
- deprecated The `info_node()` method was deprecated and will no longer work when security is enabled due to client authentication changes.
- gotcha Passing a non-integer value as a port number in a host tuple to `aerospike.client()` currently issues a warning but will raise a `ParamError` exception in the next major client release.
Install
-
pip install aerospike
Imports
- aerospike
import aerospike
- Client
client = aerospike.client(config)
Quickstart
import aerospike
import sys
# Configure the client to connect to a local Aerospike cluster
config = {
'hosts': [
('127.0.0.1', 3000)
]
}
try:
# Create a client object and connect to the cluster
client = aerospike.client(config).connect()
except aerospike.exception.ClientError as e:
print(f"Error: Failed to connect to Aerospike cluster: {e}", file=sys.stderr)
sys.exit(1)
# Define a key: (namespace, set, userkey)
key = ('test', 'demo', 'my_key_1')
# Define record bins (data)
bins = {
'name': 'John Doe',
'age': 32
}
try:
# Write a record
client.put(key, bins)
print(f"Record written: {key}")
# Read the record back
(key_read, metadata_read, record_read) = client.get(key)
print(f"Record read: {record_read}")
# Update a bin
client.put(key, {'age': 33})
print(f"Record updated: {key}")
(key_updated, metadata_updated, record_updated) = client.get(key)
print(f"Record after update: {record_updated}")
except aerospike.exception.AerospikeError as e:
print(f"Aerospike Error: {e}", file=sys.stderr)
sys.exit(1)
finally:
# Close the connection to the Aerospike cluster
if client:
client.close()
print("Client connection closed.")