Aerospike Python Client

19.2.0 · active · verified Wed Apr 15

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to an Aerospike cluster, write a record with a key and bins, read the record, update a bin, and finally close the client connection. It assumes an Aerospike database is running on localhost:3000.

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.")

view raw JSON →