Skip to main content
Loading

Single-record commands

Aerospike provides simple interfaces for reading data from and writing data to an Aerospike cluster. They cover the standard CRUD (create, read, update, delete) use cases.

Every Aerospike client can perform the following per-bin operations, no matter which language it's written in:

CommandDescription
putWrite a record. Performs the equivalent of create, update or replace, depending on the write policy given to the write command.
deleteRemove a record from the database. Generates a tombstone when using the durable-delete policy.
getRead the record. Specific bins can be selected.
addAdd (or subtract) a value in an integer bin. Used to implement counters. Also called increment.
append
prepend
Modify a string or byte array bin.
touchIncrement the generation counter.
operateExecutes multiple bin operations atomically and in isolation as a single record command.

Filtering operationsโ€‹

A command can be conditionally applied, based on the result of a filter expression. If the filter expression evaluates to true, the operation executes.

note

Support for predicate filters was removed in Aerospike 6.0. We recommend using filter expressions instead.

Code examples of record operationsโ€‹

The following examples use Python to perform create, read, update, and delete (CRUD) operations on a record. The syntax differs per Aerospike client, based on the needs of the operating system. These examples model common usage:

Create a recordโ€‹

String sighting = "{\"sighting\":{\"occurred\":20200912,\"reported\":20200916,\"posted\":20201105,\"report\":{\"city\":\"Kirkland\",\"duration\":\"~30 minutes\",\"shape\":[\"circle\"],\"state\":\"WA\",\"summary\":\"4 rotating orange lights in the Kingsgate area above the Safeway. Around 9pm the power went out in the Kingsgate area.  Four lights were spotted rotating above the local Safeway and surrounding streets.  They were rotating fast but staying relatively in the same spots.  Also described as orange lights. About thirty minutes later they disappeared.  The second they disappeared the power was restored.  Later a station of police from Woodinville and Kirkland came to guard the street where it happened.  They wouldn't let anyone go past the street, putting out search lights and flare signals so people couldn't drive past Safeway.  The police also would not let people walk past to go home.\"},\"location\":\"\\\"{\\\"type\\\":\\\"Point\\\",\\\"coordinates\\\":[-122.1966441,47.69328259]}\\\"\"}}";

// Convert string to Java Map
Map<String, Map<String, Object>> sightingMap = new Gson().fromJson(sighting, new TypeToken<HashMap<String, HashMap<String, Object>>>(){}.getType());

// Create bins from map
Bin[] bins = {
new Bin("occured", Value.get(sightingMap.get("sighting").get("occurred"))),
new Bin("reported", Value.get(sightingMap.get("sighting").get("reported"))),
new Bin("posted", Value.get(sightingMap.get("sighting").get("posted"))),
new Bin("report", Value.get(sightingMap.get("sighting").get("report"))),
};

// Write recored
WritePolicy policy = client.copyWritePolicyDefault();
client.put(policy, key, bins);

Read a recordโ€‹

// Reading reacord using with key and null policy. If no policy is provided 
// default policy will be used
Record rec = client.get(null, key);
System.out.println(new Gson().toJson(rec.getValue("report")));

Update recordโ€‹

// Update record
Map<String, Object> updateReportMap = Map.of("city","Seattle", "duration", "~1Hour");
Bin[] updateBins = {
new Bin("posted", Value.get(20221105)),
new Bin("report", updateReportMap),
};

// Update policy to record update
policy.recordExistsAction = RecordExistsAction.UPDATE;

// Update record with key
client.put(policy, key, updateBins);

Delete recordโ€‹

// Delete record
client.delete(null, key);

Usage note for all examplesโ€‹

All full record read and write commands accept policy parameters: Max Retries and Total Timeout.

Write operations take an optional time-to-live (TTL) parameter that specifies how long to protect a record from automatic eviction by the system. Set the TTL to -1 to tell Aerospike to never evict that data.

Referencesโ€‹

See these topics for language-specific examples: