Record operations
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 full-record operations, no matter which language it's written in:
Operation | Description |
---|---|
put | Write a record. Performs the equivalent of create , update or replace , depending on the write policy given to the put operation. |
delete | Remove a record from the database. Generates a tombstone when using the durable-delete policy. |
get | Read the record. Specific bins can be selected. |
add | Add (or subtract) a value in an integer bin. Used to implement counters. Also called increment . |
append prepend | Modify a string or byte array bin. |
touch | Increment the generation counter. |
operate | Apply multiple bin operations as an atomic transaction. |
Best practice is to apply add
and append
/prepend
operations as part of a transaction, and not to use the full-record operation variant described above.
Filtering Operationsโ
An operation can be conditionally applied, based on the result of a filter expression. If the filter expression evaluates to true, the operation will execute. Older versions starting with Aerospike 4.7 can apply a predicate filter to the operation.
Support for predicate filters is removed in Aerospike 6.0, so we recommend that filter expressions added in version 5.2 be used in their place.
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:
- Python
- Java
Create a recordโ
key = (namespace_name, set_name, user_key)
bins = {
'l': [ "qwertyuiop", 1, bytearray("asd;as[d'as;d", "utf-8") ],
'm': { "key": "asd';q;'1';" },
'i': 1234,
'f': 3.14159265359,
's': '!@#@#$QSDAsd;as'
}
client.put(key, bins)
Arguments:
key
: The primary key of the record to create.bins
: Bin name and value pairs to insert into the bins upon creation.
Read a recordโ
client.get(key)
Arguments:
key
: The primary key of the record to read.
Update a recordโ
update_bins = {
'b'=: u"\ud83d\ude04"
'i': aerospike.null()
}
client.put(key, update_bins)
Arguments:
key
: The primary key of the record to update.update_bins
: Bin name and value pairs to upsert into the bins.
Adds a blob bin. Removes the integer bin by overwriting with null.
Delete a recordโ
client.remove(key)
Arguments:
key
: The primary key of the record to delete.
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 operations 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: