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

Update

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"
as_set = "table1"
key_id = 6
# define Aerospike key
key = (namespace, as_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}")

Update

Update the newly created document.

# Create the update request. Update the employee at index[2]'s
# department from "Engineering" to "Support".
ctx = [cdt_ctx.cdt_ctx_list_index(2)]
ops = [
map_operations.map_put(bin_name="employees", key="department", value="Support", ctx=ctx)
]
try:
ret_key, ret_meta, ret_bins = client.operate(key=key, list=ops)
except aerospike.exception.AerospikeError as e:
print(f"Update failed\nError:{e.msg},{e.code}")
else:
print("Updated record successfully:")
pprint.pprint(ret_bins)

Read

Read the newly updated document.

# read the 'department' value for from the employees map list's last map
ctx = [cdt_ctx.cdt_ctx_list_index(2)]
ops = [
map_operations.map_get_by_key(bin_name="employees", key="department", return_type=aerospike.MAP_RETURN_VALUE,
ctx=ctx)
]
ret_key, ret_meta, ret_bins = client.operate(key=key, list=ops)
print("element 2's value in their 'department' field: ")
pprint.pprint(ret_bins)

Code block

Expand this section for a single code block to update 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"
as_set = "table1"
key_id = 6
# define Aerospike key
key = (namespace, as_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}")
# Create the update request. Update the employee at index[2]'s
# department from "Engineering" to "Support".
ctx = [cdt_ctx.cdt_ctx_list_index(2)]
ops = [
map_operations.map_put(bin_name="employees", key="department", value="Support", ctx=ctx)
]
try:
ret_key, ret_meta, ret_bins = client.operate(key=key, list=ops)
except aerospike.exception.AerospikeError as e:
print(f"Update failed\nError:{e.msg},{e.code}")
else:
print("Updated record successfully:")
pprint.pprint(ret_bins)
# read the 'department' value for from the employees map list's last map
ctx = [cdt_ctx.cdt_ctx_list_index(2)]
ops = [
map_operations.map_get_by_key(bin_name="employees", key="department", return_type=aerospike.MAP_RETURN_VALUE,
ctx=ctx)
]
ret_key, ret_meta, ret_bins = client.operate(key=key, list=ops)
print("element 2's value in their 'department' field: ")
pprint.pprint(ret_bins)
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?