Update
For the complete documentation index see: llms.txt
All documentation pages available in markdown.
Jump to the Code block for a combined complete example.
Setup
The following examples use the setup and record below to illustrate single record updates in an Aerospike database.
import com.aerospike.client.AerospikeClient;import com.aerospike.client.AerospikeException;import com.aerospike.client.ResultCode;import com.aerospike.client.policy.GenerationPolicy;import com.aerospike.client.policy.WritePolicy;import com.aerospike.client.policy.RecordExistsAction;import com.aerospike.client.Bin;import com.aerospike.client.Key;import com.aerospike.client.Operation;import com.aerospike.client.Record;
// Establishes a connection to the serverAerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001Key key = new Key("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. The client employs a RecordExistsAction, specified within the write policy, to define the operation semantics when a record already exists. The default is UPDATE.
The following variants are available:
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 oneWritePolicy updatePolicy = new WritePolicy();updatePolicy.recordExistsAction = RecordExistsAction.UPDATE_ONLY;Update a record
Update
With the write policy RecordExistsAction set to UPDATE or UPDATE_ONLY, 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 valueBin newPosted = new Bin("posted", 20220602);
// Update record with new bin valueclient.put(updatePolicy, key, newPosted);
// 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.
// Create bin with value to be addedBin newPosted = new Bin("posted", -1);
// Add new bin value to existing binclient.add(updatePolicy, key, newPosted);
// Close the connection to the serverclient.close();Replace
To replace a record, set the RecordExistsAction in the write policy to either REPLACE or REPLACE_ONLY. 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 policyupdatePolicy.recordExistsAction = RecordExistsAction.REPLACE_ONLY;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 nullBin removeBin = Bin.asNull("posted");
// Update record with null binclient.put(null, key, removeBin);
// Close the connection to the serverclient.close();Reset the time to live
Operation.touch() rewrites the record’s metadata without changing its bins.
It sets the record’s time to live (TTL) to the write policy’s expiration and increments its generation.
Combine it with Operation.getHeader() to read back the new metadata in the same command.
// Reset the TTL to two hoursWritePolicy touchPolicy = new WritePolicy();touchPolicy.expiration = 7200;
Record record = client.operate(touchPolicy, key, Operation.touch(), Operation.getHeader());
System.out.format("Generation: %d, TTL: %d%n", record.generation, record.getTimeToLive());
// Close the connection to the serverclient.close();A touch of a record that doesn’t exist throws an AerospikeException with the result code ResultCode.KEY_NOT_FOUND_ERROR.
To keep a record’s current TTL when you update its bins, set the write policy’s expiration to -2.
Write only if the record is unchanged
Every write increments the record’s generation.
To detect changes since your read, set generationPolicy to GenerationPolicy.EXPECT_GEN_EQUAL and set generation to the generation from that read.
If the generations differ, the write fails with an AerospikeException whose result code is ResultCode.GENERATION_ERROR.
// Read the record and note its generationRecord record = client.get(null, key);
WritePolicy genPolicy = new WritePolicy();genPolicy.generationPolicy = GenerationPolicy.EXPECT_GEN_EQUAL;genPolicy.generation = record.generation;
try { client.put(genPolicy, key, new Bin("posted", 20220603));}catch (AerospikeException ae) { // Rethrow any other failure if (ae.getResultCode() != ResultCode.GENERATION_ERROR) { throw ae; } System.out.println("Record changed since it was read");}
// Close the connection to the serverclient.close();Code block
Expand this section for a single code block to read a record
import com.aerospike.client.AerospikeClient;import com.aerospike.client.policy.WritePolicy;import com.aerospike.client.policy.RecordExistsAction;import com.aerospike.client.Bin;import com.aerospike.client.Key;import com.aerospike.client.Record;
// Establishes a connection to the serverAerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001Key key = new Key("sandbox", "ufodata", 5001);
// Can use a previously defined write policy or create a new oneWritePolicy updatePolicy = new WritePolicy();updatePolicy.recordExistsAction = RecordExistsAction.UPDATE_ONLY;
// Create bin with new valueBin newPosted = new Bin("posted", 20220602);
// Update record with new bin valueclient.put(updatePolicy, key, newPosted);
// Close the connection to the serverclient.close();