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
Boilerplate
The examples below leverage the following boilerplate to initialize the Aerospike module and connect to a server before executing batch operations.
const Aerospike = await import("aerospike");const batchType = Aerospike.batchType;const op = Aerospike.operations;const maps = Aerospike.maps;const list = Aerospike.lists;const exp = Aerospike.exp;
// Set hosts to your server's address and portconst config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Establishes a connection to the serverconst client = await Aerospike.connect(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 policyconst batchPolicy = new Aerospike.BatchPolicy({ // An example that will always return true filterExpression: exp.gt(exp.int(2), exp.int(1)),});
// Create the batch write policyconst batchWritePolicy = new Aerospike.BatchWritePolicy({ // An example that will always return true filterExpression: exp.gt(exp.int(2), exp.int(1)),});Requests
Exists
The following example creates an array of ten keys and checks for their existence in the database.
// Create batch of keysconst keys = Array.from( { length: 10 }, (_, i) => new Aerospike.Key("sandbox", "ufodata", i + 4995),);
// Check if records existconst batchResult = await client.batchExists(keys, batchPolicy);
// Access the recordsfor (const { record } of batchResult) { if (result.status !== 0) { console.info("Key: %o does not exist", record.key.key); }}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 keysconst batchRecords = Array.from({ length: 10 }, (_, i) => ({ type: batchType.BATCH_READ, key: new Aerospike.Key("sandbox", "ufodata", i + 1), readAllBins: true, // Can set 'readAllBins' false and specify bins // bins: ['report', 'location']}));
// Read recordsconst batchResult = await client.batchRead(batchRecords);
// Access the recordsfor (const { record } of batchResult) { console.info("Record bins:", record.bins);}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 expressionsconst city_expr = exp.maps.getByKey( exp.binMap("report"), exp.str("city"), exp.type.STR, maps.returnType.VALUE,);const state_expr = exp.maps.getByKey( exp.binMap("report"), exp.str("state"), exp.type.STR, maps.returnType.VALUE,);
// Create batch of recordsconst batchRecords = Array.from({ length: 10 }, (_, i) => ({ type: batchType.BATCH_READ, key: new Aerospike.Key("sandbox", "ufodata", i + 1), ops: [ exp.operations.read("city", city_expr, 0), exp.operations.read("state", state_expr, 0), ],}));
// Get 'city' and 'state' from report map for each recordconst batchResult = await client.batchRead(batchRecords);
// Access the recordsfor (const { record } of batchResult) { console.info("Record bins:", record.bins);}Read/write operations
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.
// Build the expressionconst expr = exp.maps.put( exp.binMap("report"), exp.and( exp.gt(exp.binInt("occurred"), exp.int(20211231)), exp.binExists("posted"), ), exp.str("recent"),);
// Create batch of keysconst batchRecords = Array.from({ length: 10 }, (_, i) => ({ type: batchType.BATCH_WRITE, key: new Aerospike.Key("sandbox", "ufodata", i + 1), ops: [exp.operations.write("report", expr, 0), op.read("report")],}));
// Execute the write operation and return the report binconst batchResult = await client.batchWrite(batchRecords);
// Access the recordsfor (const { record } of batchResult) { console.info("Record bins:", record.bins);}Deletes
The following example deletes the records from the database.
// Create batch of keysconst keys = Array.from( { length: 10 }, (_, i) => new Aerospike.Key("sandbox", "ufodata", i + 4995),);
// Delete recordsconst batchResults = await client.batchRemove(keys);Complex batched operations
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 Expressionsconst write_expr = exp.maps.put( exp.binMap("report"), exp.and( exp.gt(exp.binInt("occurred"), exp.int(20211231)), exp.binExists("posted"), ), exp.str("recent"),);
const size_expr = exp.lists.size( exp.maps.getByKey( exp.binMap("report"), exp.str("shape"), exp.type.LIST, maps.returnType.VALUE, ),);
// Create batch of recordsconst batchRecords = [ { type: batchType.BATCH_WRITE, key: new Aerospike.Key("sandbox", "ufodata", 4000), ops: [exp.operations.write("report", write_expr, 0), op.read("report")], }, { type: batchType.BATCH_READ, key: new Aerospike.Key("sandbox", "ufodata", 4001), ops: [exp.operations.read("numShapes", size_expr, 0)], }, { type: batchType.BATCH_WRITE, key: new Aerospike.Key("sandbox", "ufodata", 4002), ops: [ op.write("posted", 20220602), maps.put("report", "city", "Cedarville"), op.read("posted"), op.read("report"), ], }, { type: batchType.BATCH_REMOVE, key: new Aerospike.Key("sandbox", "ufodata", 4003), },];
// Execute the batchconst batchResult = await client.batchWrite(batchRecords);
// Access the recordsfor (const { record } of batchResult) { console.info("Record bins:", record.bins);}Code block
Expand this section for a single code block to execute a batch read/write operation
const Aerospike = await import("aerospike");const batchType = Aerospike.batchType;const op = Aerospike.operations;const maps = Aerospike.maps;const list = Aerospike.lists;const exp = Aerospike.exp;
// Set hosts to your server's address and portconst config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };
// Establishes a connection to the serverconst client = await Aerospike.connect(config);
// Define Operation Expressionsconst write_expr = exp.maps.put( exp.binMap("report"), exp.and( exp.gt(exp.binInt("occurred"), exp.int(20211231)), exp.binExists("posted"), ), exp.str("recent"),);
const size_expr = exp.lists.size( exp.maps.getByKey( exp.binMap("report"), exp.str("shape"), exp.type.LIST, maps.returnType.VALUE, ),);
// Create batch of recordsconst batchRecords = [ { type: batchType.BATCH_WRITE, key: new Aerospike.Key("test", "demo", 4000), ops: [exp.operations.write("report", write_expr, 0), op.read("report")], }, { type: batchType.BATCH_READ, key: new Aerospike.Key("test", "demo", 4001), ops: [exp.operations.read("numShapes", size_expr, 0)], }, { type: batchType.BATCH_WRITE, key: new Aerospike.Key("test", "demo", 4002), ops: [ op.write("posted", 20220602), maps.put("report", "city", "Cedarville"), op.read("posted"), op.read("report"), ], }, { type: batchType.BATCH_REMOVE, key: new Aerospike.Key("test", "demo", 4003), },];
// Execute the batchconst batchResult = await client.batchWrite(batchRecords);
// View the resultsfor (const { record } of batchResult) { console.info("Record bins:", record.bins);}
await client.close();