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

Create

Jump to the Code block for a combined complete example.

Create a document record

The following command uses the Aerospike Admin (asadm) to create an integer index on the sandbox namespace, ufodata set, and occurred bin. This is the recommended way to create a secondary index.

asadm -e 'enable; manage sindex create numeric occurred_idx ns sandbox set ufodata bin occurred'

Setup

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

import aerospike
from aerospike_helpers.operations import map_operations
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)

Create a JSON document

Prepare the JSON document to be sent to Aerospike.

# example JSON string
employee ='{"id":"09", "name": "Nitin", "department":"Finance"}'
# Convert string to Python dict
employee_dict = json.loads(employee)
pprint.pprint(employee_dict)
# set an aerospike bin with the bin name "employee" and
# put the JSON document in as a map
bins = {
"employee": 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}]")

Code block

Expand this section for a single code block to create a document record.
import aerospike
from aerospike_helpers.operations import map_operations
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
employee ='{"id":"09", "name": "Nitin", "department":"Finance"}'
# Convert string to Python dict
employee_dict = json.loads(employee)
pprint.pprint(employee_dict)
# set an aerospike bin with the bin name "employee" and
# put the JSON document in as a map
bins = {
"employee": 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}]")
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?