Batched commands
Jump to the Code block for a combined complete example.
Batched commands execute against multiple records issued as a single request.
Batch reads support get, exists, getHeader, and operate requests.
Batch writes, introduced in Aerospike 6.0.0, allow write requests against any keys, including updates, deletes, UDFs, and multi-operation operate commands.
Setup
The following examples will use the setup and record structure below to illustrate batch operations in an Aerospike database.
using Aerospike.Client;using System;using System.Collections;
// Define host configurationHost config = new Host("127.0.0.1", 3000);// Establishes a connection to the serverAerospikeClient client = new AerospikeClient(null, config);The record structure:
Occurred: IntegerReported: IntegerPosted: IntegerReport: Map{ shape: List, summary: String, city: String, state: String, duration: String}Location: GeoJSONPolicies
Policies are defined for the batch parent policy as well as batch read, batch write, batch delete, and batch UDF operations. Filter Expressions can be defined within each type of batch operation policy and the batch parent policy, along with other operation specific policies.
// Create a new batch policyBatchPolicy batchPolicy = new BatchPolicy();batchPolicy.filterExp = Exp.Build( // An example that will always return true Exp.GT(Exp.Val(2), Exp.Val(1)));
// Create the batch write policyBatchWritePolicy batchWritePolicy = new BatchWritePolicy();batchWritePolicy.filterExp = Exp.Build( // An example that will always return true Exp.GT(Exp.Val(2), Exp.Val(1)));Requests
Exists
The following example creates an array of ten keys and checks for their existence in the database.
// Create batch of keysKey[] keys = new Key[10];for (int i = 0; i < 10; i++){ keys[i] = new Key("sandbox", "ufodata", (i + 4995));}
// Check if records existbool[] exists = client.Exists(batchPolicy, keys);
for (int i = 0; i < exists.Length; i++){ if(!exists[i]) { // Do something Console.WriteLine("Key: {0} does not exist", keys[i].userKey); }}
// Close the connection to the serverclient.Close();Read records
The following example creates an array of ten keys and reads the records from the database; returning either the whole record or the specified report and location bins.
// Create batch of keysKey[] keys = new Key[10];for (int i = 0; i < 10; i++){ keys[i] = new Key("sandbox", "ufodata", (i + 1));}
// Read each whole recordRecord[] records = client.Get(batchPolicy, keys);
// Or specifiy bins// Record[] records = client.Get(batchPolicy, keys, "report", "location");
// Access the recordsforeach (Record record in records){ if(record != null) { // Do something Console.WriteLine("Record: {0}\\n", record.ToString().Split("bins:")[1]); }}
// Close the connection to the serverclient.Close();Read commands
The following example creates an array of ten keys and accesses the city and state map keys to return their respective values from the report bin, for each record.
// Create batch of keysKey[] keys = new Key[10];for (int i = 0; i < 10; i++){ keys[i] = new Key("sandbox", "ufodata", (i + 1));}
// Create map key liststring[] mapKeys = {"city", "state"};
// Get 'city' and 'state' from report map for each recordBatchResults batchResult = client.Operate(batchPolicy, batchWritePolicy, keys, MapOperation.GetByKeyList("report", mapKeys, MapReturnType.VALUE));
// Access the recordsforeach (BatchRecord batchRecord in batchResult.records){ Record record = batchRecord.record; if(record != null) { // Do something Console.WriteLine("Record: {0}\\n", record.ToString().Split("bins:")[1]); }}
// Close the connection to the serverclient.Close();Read/write commands
The following example creates an array of ten keys and
- Defines an Operation Expression that compares the
occurredbin value against the provided value,20211231, and verifies thepostedbin exists to determine the boolean value of the newrecentkey being added to thereportmap. - Returns the
reportbin.
// Create batch of keysKey[] keys = new Key[10];for (int i = 0; i < 10; i++){ keys[i] = new Key("sandbox", "ufodata", (i + 1));}
// Define Operation ExpressionsExpression exp = Exp.Build( MapExp.Put(MapPolicy.Default, Exp.Val("recent"), Exp.And( Exp.GT(Exp.IntBin("occurred"), Exp.Val(20211231)), Exp.BinExists("posted") ), Exp.MapBin("report") ));
// Execute the write operation and return the report binBatchResults batchResult = client.Operate(batchPolicy, batchWritePolicy, keys, ExpOperation.Write("report", exp, ExpWriteFlags.DEFAULT), Operation.Get("report"));
// Access the recordsforeach (BatchRecord batchRecord in batchResult.records){ Record record = batchRecord.record; if(record != null) { // Do something Console.WriteLine("Record: {0}\\n", record.ToString().Split("bins:")[1]); }}
// Close the connection to the serverclient.Close();Deletes
The following example deletes the records from the database.
// Create batch of keysKey[] keys = new Key[10];for (int i = 0; i < 10; i++){ keys[i] = new Key("sandbox", "ufodata", (i + 1));}
// Delete records passing null to use the default BatchDeletePolicyBatchResults batchResults = client.Delete(batchPolicy, null, keys);
// Close the connection to the serverclient.Close();Complex batched commands
The following example creates a list of four batch records that each use a differing set of operations.
The record with user defined key 4000
- uses the
ops1array that combines the Operation Expression. - uses
exp1which compares theoccurredbin value against the provided value,20211231and verifies thepostedbin exists to determine the boolean value of the newrecentkey being added to thereportmap. - returns the
reportbin.
The record with user defined key 4001
- uses the
ops2array which contains a read Operation Expression that gets the length of theshapelist from thereportmap and returns the value in a synthetic bin namednumShapes.
The record with user defined key 4002
- uses the
ops3array which combines a write operation that updates thepostedbin value, with a map operation that updates thecityvalue in thereportmap. - returns both the
postedandreportbins.
The record with user defined key 4003 is deleted from the database.
// Define Operation ExpressionsExpression exp1 = Exp.Build( MapExp.Put(MapPolicy.Default, Exp.Val("recent"), Exp.And( Exp.GT(Exp.IntBin("occurred"), Exp.Val(20211231)), Exp.BinExists("posted") ), Exp.MapBin("report") ));Expression exp2 = Exp.Build( ListExp.Size( MapExp.GetByKey(MapReturnType.VALUE, Exp.Type.LIST, Exp.Val("shape"), Exp.MapBin("report")) ));
// Define operationsOperation[] ops1 = Operation.Array( ExpOperation.Write("report", exp1, ExpWriteFlags.DEFAULT), Operation.Get("report"));Operation[] ops2 = Operation.Array(ExpOperation.Read("numShapes", exp2, ExpReadFlags.DEFAULT));Operation[] ops3 = Operation.Array( Operation.Put(new Bin("posted", 20201108)), MapOperation.Put(MapPolicy.Default, "report", Value.Get("city"), Value.Get("Cedarville")), Operation.Get("posted"), Operation.Get("report"));
// Create list of batch records to processList<BatchRecord> batchRecordList = new List<BatchRecord>();batchRecordList.Add(new BatchWrite(new Key("sandbox", "ufodata", 4000), ops1));batchRecordList.Add(new BatchRead(new Key("sandbox", "ufodata", 4001), ops2));batchRecordList.Add(new BatchWrite(new Key("sandbox", "ufodata", 4002), ops3));batchRecordList.Add(new BatchDelete(new Key("sandbox", "ufodata", 4003)));
// Proccess the batchclient.Operate(batchPolicy, batchRecordList);
// Access the resultsforeach (BatchRecord batchRecord in batchRecordList){ Record record = batchRecord.record; if (record != null) { // Do something Console.WriteLine("Record: {0}\\n", record.ToString().Split("bins:")[1]); }}
// Close the connection to the serverclient.Close();Code block
Expand this section for a single code block to execute a batch read/write operation
using Aerospike.Client;using System;using System.Collections;
// Define host configurationHost config = new Host("127.0.0.1", 3000);// Establishes a connection to the serverAerospikeClient client = new AerospikeClient(null, config);
// Create a new batch policyBatchPolicy batchPolicy = new BatchPolicy();batchPolicy.filterExp = Exp.Build( // An example that will always return true Exp.GT(Exp.Val(2), Exp.Val(1)));
// Create the batch write policyBatchWritePolicy batchWritePolicy = new BatchWritePolicy();batchWritePolicy.filterExp = Exp.Build( // An example that will always return true Exp.GT(Exp.Val(2), Exp.Val(1)));
// Create batch of keysKey[] keys = new Key[10];for (int i = 0; i < 10; i++){ keys[i] = new Key("sandbox", "ufodata", (i + 1));}
// Define Operation ExpressionsExpression exp = Exp.Build( MapExp.Put(MapPolicy.Default, Exp.Val("recent"), Exp.And( Exp.GT(Exp.IntBin("occurred"), Exp.Val(20211231)), Exp.BinExists("posted") ), Exp.MapBin("report") ));
// Execute the write operation and return the report binBatchResults batchResult = client.Operate(batchPolicy, batchWritePolicy, keys, ExpOperation.Write("report", exp, ExpWriteFlags.DEFAULT), Operation.Get("report"));
// Access the recordsforeach (BatchRecord batchRecord in batchResult.records){ Record record = batchRecord.record; if(record != null) { // Do something Console.WriteLine("Record: {0}\\n", record.ToString().Split("bins:")[1]); }}
// Close the connection to the serverclient.Close();