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 aerospikefrom aerospike_helpers.operations import map_operationsfrom aerospike_helpers import cdt_ctx
import pprintimport json
# set aerospike host configconfig = {"hosts": [("localhost", 3000)]}
# create the aerospike client and connectclient = aerospike.client(config).connect()
# aerospike namespace, set, and key_id to be used for the aerospike keynamespace = "test"set = "table1"key_id = 5
# define aerospike keykey = (namespace, set, key_id)
Prepare the JSON document to be sent to Aerospike.
# example JSON stringemployees = """[{"id": "09", "name": "Nitin", "department": "Finance"}, {"id": "10", "name": "Jonathan", "department": "Human Resources"}, {"id": "11", "name": "Caitlin", "department": "Engineering"}]"""
# Convert string to Python dictemployee_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 policywrite_policy = {"key": aerospike.POLICY_KEY_SEND}
# Write the record to Aerospiketry: 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 mapctx = [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 betweenops = [ 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 aerospikefrom aerospike_helpers.operations import map_operationsfrom aerospike_helpers import cdt_ctx
import pprintimport json
# set aerospike host configconfig = {"hosts": [("localhost", 3000)]}
# create the aerospike client and connectclient = aerospike.client(config).connect()
# aerospike namespace, set, and key_id to be used for the aerospike keynamespace = "test"set = "table1"key_id = 5
# define aerospike keykey = (namespace, set, key_id)
# example JSON stringemployees = """[{"id": "09", "name": "Nitin", "department": "Finance"}, {"id": "10", "name": "Jonathan", "department": "Human Resources"}, {"id": "11", "name": "Caitlin", "department": "Engineering"}]"""
# Convert string to Python dictemployee_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 policywrite_policy = {"key": aerospike.POLICY_KEY_SEND}
# Write the record to Aerospiketry: 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 mapctx = [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 betweenops = [ 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)