Update
Jump to the Code block for a combined complete example.
Setup
The following examples will use the setup and record below to illustrate single record updates in an Aerospike database.
import aerospike
# Define host configurationconfig = { 'hosts': [ ('127.0.0.1', 3000) ]}# Establishes a connection to the serverclient = aerospike.client(config).connect()
# Creates a key with the namespace "sandbox", set "ufodata", and user key 5001key = ('sandbox', 'ufodata', 5001)
The record structure:
+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+| PK | occurred | reported | posted | report | location |+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+| 5001 | 20220531 | 20220601 | 20220601 | MAP('{"shape":["circle", "flash", "disc"], "summary":"Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!", "city":"Ann Arbor", "state":"Michigan", "duration":"5 minutes"}') | GeoJSON('{"type":"Point","coordinates":[42.2808,83.743]}') |+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+
Policies
Create and Update both utilize write policies that can be defined per command. Each client employs an exists
action, specified within the write policy, to define the operation semantics when a record already exists. The default is POLICY_EXISTS_IGNORE
.
The following variants are available:
Action Type | Description |
---|---|
POLICY_EXISTS_IGNORE | Create if record doesn’t exist, update otherwise. |
POLICY_EXISTS_CREATE | Create if record doesn’t exist already. |
POLICY_EXISTS_UPDATE | Update only, if record exists. |
POLICY_EXISTS_CREATE_OR_REPLACE | Create if record doesn’t exist, replace otherwise. |
POLICY_EXISTS_REPLACE | Replace only, if record exists. |
The following example sets the policy to update the record, only if it already exists.
# Can use a previously defined write policy or create a new oneupdate_policy = {'exists': aerospike.POLICY_EXISTS_UPDATE}
Update a record
Update
With the write policy exists
action set to POLICY_EXISTS_IGNORE
or POLICY_EXISTS_CREATE
, new bins can be added to a record and existing bin values can be updated.
The following example updates the posted
bin value to 20220602
.
# Create bin with new valuenewPosted = {"posted": 20220602}
# Update record with new bin valueclient.put(key, newPosted, policy=update_policy)
# Close the connection to the serverclient.close()
Type specific
Type specific updates can add
integers or append
/prepend
strings to existing integer and string bin values, respectively.
The following example decrements the posted
bin value by one.
# Add value to existing binclient.increment(key, 'posted', -1, policy=update_policy)
# Close the connection to the serverclient.close()
Replace
To replace a record, set the exists
action in the write policy to either POLICY_EXISTS_CREATE_OR_REPLACE
or POLICY_EXISTS_REPLACE
. Refer to the policies section above for more information on setting the policy and the create page for creating a record to replace the existing one.
The following example sets the existing write policy to replace ony if the record already exists.
# Updating the previously defined write policyupdate_policy['exists'] = aerospike.POLICY_EXISTS_REPLACE
Delete a bin
A bin is deleted by setting it to null
. Delete the posted
bin by setting its value to null
.
# Set bin value to nullremoveBin = {'posted': aerospike.null()}
# Update record with null binclient.put(key, removeBin, policy=update_policy)
# Close the connection to the serverclient.close()
Code block
Expand this section for a single code block to read a record
import aerospike
# Define host configurationconfig = { 'hosts': [ ('127.0.0.1', 3000) ]}# Establishes a connection to the serverclient = aerospike.client(config).connect()
# Creates a key with the namespace "sandbox", set "ufodata", and user key 5001key = ('sandbox', 'ufodata', 5001)
# Can use a previously defined write policy or create a new oneupdate_policy = {'exists': aerospike.POLICY_EXISTS_UPDATE}
# Create bin with new valuenewPosted = {"posted": 20220602}
# Update record with new bin valueclient.put(key, newPosted, policy=update_policy)
# Close the connection to the serverclient.close()