Skip to content
Visit booth 3171 at Google Cloud Next to see how to unlock real-time decisions at scaleMore info

Read

Jump to the Code block for a combined complete example.

Create a document record

The following example demonstrates reading a document record with a JSON helper library. In Aerospike, JSON documents are handled as a Collection Data Type (CDT). A JSON object is equivalent to a map, and a JSON array is equivalent to a list.

In this example, we create the following JSON document:

employees = [{"id": "09", "name": "Nitin", "department": "Finance"},
{"id": "10", "name": "Jonathan", "department": "Human Resources"},
{"id": "11", "name": "Caitlin", "department": "Engineering"}]

The JSON document is added to a bin called employees which is of data type list. There are some advantages to holding an entire document in a single bin, rather than spreading out the document’s fields over multiple bins. There is less metadata overhead when namespaces contain fewer, larger bins.

Setup

Import the necessary helpers, create a client connection, and create a key.

import aerospike
from aerospike_helpers.operations import map_operations
from aerospike_helpers import cdt_ctx
import pprint
import json
# set aerospike host config
config = {"hosts": [("localhost", 3000)]}
# create the aerospike client and connect
client = aerospike.client(config).connect()
# aerospike namespace, set, and key_id to be used for the aerospike key
namespace = "test"
set = "table1"
key_id = 5
# define aerospike key
key = (namespace, set, key_id)

Prepare the JSON document to be sent to Aerospike.

# example JSON string
employees = """[{"id": "09", "name": "Nitin", "department": "Finance"},
{"id": "10", "name": "Jonathan", "department": "Human Resources"},
{"id": "11", "name": "Caitlin", "department": "Engineering"}]"""
# Convert string to Python dict
employee_dict = json.loads(employees)
pprint.pprint(employee_dict)
# Set an Aerospike bin with the bin name "employees" and put
# the JSON document in as a list.
bins = {"employees": employee_dict}

Write

Write the document to Aerospike.

# Create the write policy
write_policy = {"key": aerospike.POLICY_KEY_SEND}
# Write the record to Aerospike
try:
client.put(key=key, bins=bins, policy=write_policy)
(key_, meta, bins) = client.get(key=key)
print("Create succeeded\nKey: ", key[2], "\nRecord:")
pprint.pprint(bins)
except aerospike.exception.AerospikeError as e:
print(f"Create failed\nError:{e.msg},{e.code}")

Read

Read the name field from the third record in the map list.

# Read the 'name' value for from the employees map list's third map
ctx = [cdt_ctx.cdt_ctx_list_index(2)]
# Create the get request. The arguments supplied should be
# map_operations.map_get_by_key( bin_name, key_name, ctx_path);
# provide the highest level bin_name and lowest level key_name, with the
# ctx_path defining the intermediary access steps between
ops = [
map_operations.map_get_by_key(
"employees", "name", aerospike.MAP_RETURN_VALUE, ctx
)
]
_, _, result = client.operate(key, ops)
print("Element 2's value in the 'name' field: ")
print(result)

Code block

Expand this section for a single code block to read a document record.
import aerospike
from aerospike_helpers.operations import map_operations
from aerospike_helpers import cdt_ctx
import pprint
import json
# set aerospike host config
config = {"hosts": [("localhost", 3000)]}
# create the aerospike client and connect
client = aerospike.client(config).connect()
# aerospike namespace, set, and key_id to be used for the aerospike key
namespace = "test"
set = "table1"
key_id = 5
# define aerospike key
key = (namespace, set, key_id)
# example JSON string
employees = """[{"id": "09", "name": "Nitin", "department": "Finance"},
{"id": "10", "name": "Jonathan", "department": "Human Resources"},
{"id": "11", "name": "Caitlin", "department": "Engineering"}]"""
# Convert string to Python dict
employee_dict = json.loads(employees)
pprint.pprint(employee_dict)
# Set an Aerospike bin with the bin name "employees" and put
# the JSON document in as a list.
bins = {"employees": employee_dict}
# Create the write policy
write_policy = {"key": aerospike.POLICY_KEY_SEND}
# Write the record to Aerospike
try:
client.put(key=key, bins=bins, policy=write_policy)
(key_, meta, bins) = client.get(key=key)
print("Create succeeded\nKey: ", key[2], "\nRecord:")
pprint.pprint(bins)
except aerospike.exception.AerospikeError as e:
print(f"Create failed\nError:{e.msg},{e.code}")
# Read the 'name' value for from the employees map list's third map
ctx = [cdt_ctx.cdt_ctx_list_index(2)]
# Create the get request. The arguments supplied should be
# map_operations.map_get_by_key( bin_name, key_name, ctx_path);
# provide the highest level bin_name and lowest level key_name, with the
# ctx_path defining the intermediary access steps between
ops = [
map_operations.map_get_by_key(
"employees", "name", aerospike.MAP_RETURN_VALUE, ctx
)
]
_, _, result = client.operate(key, ops)
print("Element 2's value in the 'name' field: ")
print(result)
Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?