Skip to content

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 single-record commands, 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.
getRead the record. Specific bins can be selected.
operateExecutes multiple bin operations atomically and in isolation as a single record command.
touchIncrement the generation counter and extend its time-to-live (TTL)
deleteRemove a record from the database. Generates a tombstone when using the durable-delete policy.

Filtering operations

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

Code examples of record operations

Every record is identified by a key consisting of a namespace, an optional set, and a user key. In the following examples, the key is ("test", "demo", "user1"), placing the record in the demo set of the test namespace.

A record can also be created without a set by passing null (or None) as the set component. Such records belong to the null set. Records in the null set are fully accessible through single-record commands (put, get, operate, touch, delete) and batch commands, but cannot be targeted by a secondary index query unless the index covers the entire namespace. You can prevent null-set records from being created by setting disallow-null-setname to true.

The following examples demonstrate put (create and merge-style update), get, operate (atomic multi-bin updates in one round trip), touch, and delete on a single record. They use a record with a string bin, an integer bin, and a map bin:

BinTypeValue
namestring"Alice"
visitsinteger1
prefsmap{"theme": "dark", "lang": "en"}

Each section below shows an excerpt. See the Code block at the bottom of the page for the full, continuous program in each language.

Create a record

Use put to write a new record. By default, put creates the record if it does not exist, or merges bins into the existing record if it does. This behavior is controlled by the write policy’s recordExistsAction, which defaults to UPDATE (upsert). Other options include CREATE_ONLY, UPDATE_ONLY, and REPLACE.

Map<String, Object> prefs = new HashMap<>();
prefs.put("theme", "dark");
prefs.put("lang", "en");
// Default upsert behavior (RecordExistsAction.UPDATE)
// WritePolicy wp = new WritePolicy();
// wp.recordExistsAction = RecordExistsAction.UPDATE;
// client.put(wp, key, ...);
client.put(null, key,
new Bin("name", "Alice"),
new Bin("visits", 1),
new Bin("prefs", prefs));

Read a record

Use get to read the full record (metadata and all bins), or select specific bins.

Record record = client.get(null, key);
System.out.format("Record: %s%n", record.bins);

Merge-update bins with put

Use put again to change one or more bins. With default write policies, the client merges new bins into the existing record and leaves other bins unchanged (this is not a full replace of every bin unless you use a replace-style policy).

client.put(null, key, new Bin("name", "Alice K."));

Atomic updates with operate

Use operate to execute multiple bin operations atomically in a single command. The following example increments the visits counter, updates a value inside the prefs map, and reads both bins — all in one round trip.

Record record = client.operate(null, key,
Operation.add(new Bin("visits", 1)),
MapOperation.put(MapPolicy.Default, "prefs",
Value.get("lang"), Value.get("fr")),
Operation.get("visits"),
Operation.get("prefs"));
System.out.format("visits: %s, prefs: %s%n",
record.getValue("visits"), record.getValue("prefs"));

Touch a record

Use touch to increment the record’s generation counter and optionally reset its time-to-live (TTL).

WritePolicy touchPolicy = new WritePolicy();
touchPolicy.expiration = 300; // TTL in seconds
client.touch(touchPolicy, key);

Delete a record

Use delete (or remove) to delete a record. Enable durableDelete in the write policy to write a tombstone that prevents the record from resurrecting on a cold restart. See Durable Deletes for details.

// Durable delete:
// WritePolicy deletePolicy = new WritePolicy();
// deletePolicy.durableDelete = true;
// client.delete(deletePolicy, key);
WritePolicy removePolicy = new WritePolicy(); // empty / default
client.delete(removePolicy, 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.

Code block

Expand for the full single-record operations example
import com.aerospike.client.AerospikeClient;
import com.aerospike.client.Bin;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.Value;
import com.aerospike.client.Operation;
import com.aerospike.client.cdt.MapPolicy;
import com.aerospike.client.cdt.MapOperation;
import com.aerospike.client.policy.WritePolicy;
import java.util.HashMap;
import java.util.Map;
// Connect
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
Key key = new Key("test", "demo", "user1");
// Upsert
Map<String, Object> prefs = new HashMap<>();
prefs.put("theme", "dark");
prefs.put("lang", "en");
client.put(null, key,
new Bin("name", "Alice"),
new Bin("visits", 1),
new Bin("prefs", prefs));
// Read
Record record = client.get(null, key);
System.out.format("Record: %s%n", record.bins);
// Merge-update (put)
client.put(null, key, new Bin("name", "Alice K."));
// Update (operate)
record = client.operate(null, key,
Operation.add(new Bin("visits", 1)),
MapOperation.put(MapPolicy.Default, "prefs",
Value.get("lang"), Value.get("fr")),
Operation.get("visits"),
Operation.get("prefs"));
System.out.format("visits: %s, prefs: %s%n",
record.getValue("visits"), record.getValue("prefs"));
// Touch
WritePolicy touchPolicy = new WritePolicy();
touchPolicy.expiration = 300;
client.touch(touchPolicy, key);
// Durable delete:
// WritePolicy deletePolicy = new WritePolicy();
// deletePolicy.durableDelete = true;
// client.delete(deletePolicy, key);
WritePolicy removePolicy = new WritePolicy(); // empty / default
client.delete(removePolicy, key);
client.close();

References

See these topics for language-specific examples:

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?