Skip to main content

Bin operations


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 command operations in an Aerospike database.

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.MapOrder;
import com.aerospike.client.cdt.MapWriteFlags;
import com.aerospike.client.cdt.MapPolicy;
import com.aerospike.client.cdt.MapOperation;

// Establishes a connection to the server
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);

// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001
Key key = new Key("sandbox", "ufodata", 5001);

The record structure:

+------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+
| PK | occurred | reported | report | location |
+------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+
| 5001 | 20220531 | 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]}') |
+------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+

Operations

Use the Aerospike Client API to perform separate operations on one or multiple bins in a record within a single transaction. This feature performs an atomic modification then returns the result. The table below shows some of the bin operations available for operate transactions.

Operations for List, Map, Blob/Bytes, Geospatial, and HyperLogLog data types can be found here.

Data TypeOperationDescription
Allop_putUpsert (create or update) a bin. Also called write.
Allop_getRead a bin. Also called read.
Allop_touchIncrease the generation counter for a record.
Allop_deleteRemove a record from the database.
Integer
Float
op_addAdd (or subtract) a value. Used to implement counters. Also called increment.
Stringop_append
op_prepend
Modify a string.
note

Operations are performed in user defined order.

Single operation

All of the operations in the table above, along with CDT, Blob/Bytes, Geospatial, and HyperLogLog operations, can be executed as a single operation with the operate method.

The following example creates a map policy for a KEY_ORDERED map, then applies that policy to the report bin. A KEY_ORDERED map provides faster access than the default UNORDERED map. This example passes null to use the default policy, but a write policy could be passed instead.

// Create KEY_ORDERED map policy
MapPolicy mapPolicy = new MapPolicy(MapOrder.KEY_ORDERED, MapWriteFlags.DEFAULT);

// Update the record
client.operate(null, key, MapOperation.setMapPolicy(mapPolicy, "report"));

// Close the connection to the server
client.close();

Multiple Operations

Multiple operations can be combined in a single transaction when one or more bins need to be updated or read.

The following example inserts the posted bin with value 20220602, updates the city inside the report bin to "Ypsilanti", and reads the report bin in a single transaction.

// Create posted bin to insert
Bin posted = new Bin("posted", 20220602);

// Update the record
Record record = client.operate(null, key,
Operation.put(posted),
MapOperation.put(MapPolicy.Default, "report", Value.get("city"), Value.get("Ypsilanti")),
Operation.get("report")
);

// Do something
System.out.format("Record: %s\\n", record.bins);

// Close the connection to the server
client.close();

Returning from operate()

It's important to understand how the operate method returns data, since operate accepts a variety of operations and deals with both reads and writes in a single call.

Examples of operations and results:

  • A basic put or write operation does not return anything by default, though it can be set to return null through a write policy.
  • A map or list modify operation returns the map or list size for the bin_name.
  • A get or read operation returns the value for the bin_name.

See Bitwise Operations, List Operations, Map Operations, HLL Operations, and Expressions for more information about return values.

The operate method takes the results of all the operations executed within the call and groups them together by bin_name. If a specific bin_name has multiple results, such as an operate call containing multiple operations that return results for a single bin_name, a list is created containing the results of each operation. If a bin_name has a single value, the bin_name value is returned as expected.

When grouped by bin_name, the way results are accessed will vary based on what operations are performed.

note

Some types of write commands do not return results by default, which can make it difficult to determine the correct result offset. To ensure that every write command returns a result, set the WritePolicy.respondAllOps field to true.

Code block

Expand this section for a single code block to create a transaction operation
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.MapOrder;
import com.aerospike.client.cdt.MapWriteFlags;
import com.aerospike.client.cdt.MapPolicy;
import com.aerospike.client.cdt.MapOperation;

// Establishes a connection to the server
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);

// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001
Key key = new Key("sandbox", "ufodata", 5001);

// Create posted bin to insert
Bin posted = new Bin("posted", 20220602);

// Update the record
Record record = client.operate(null, key,
Operation.put(posted),
MapOperation.put(MapPolicy.Default, "report", Value.get("city"), Value.get("Ypsilanti")),
Operation.get("report")
);

// Do something
System.out.format("Record: %s\\n", record.bins);

// Close the connection to the server
client.close();