Skip to content
Visit booth 3171 at Google Cloud Next to see how to unlock real-time decisions at scaleMore info

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, 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.

const Aerospike = require('aerospike');
const batchType = Aerospike.batchType;
const op = Aerospike.operations;
const map = Aerospike.maps;
const list = Aerospike.lists;
const exp = Aerospike.exp;
// Define host configuration
let config = {hosts: '127.0.0.1:3000'};

The record structure:

Occurred: Integer
Reported: Integer
Posted: Integer
Report: Map
{
shape: List,
summary: String,
city: String,
state: String,
duration: String
}
Location: GeoJSON

Policies

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 policy
let batchPolicy = new Aerospike.BatchPolicy({
// An example that will always return true
filterExpression: exp.gt(exp.int(2), exp.int(1))
});
// Create the batch write policy
let 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 existance in the database.

// Create batch of keys
let keys = [];
for(i = 0; i < 10; i++){
keys.push(new Aerospike.Key('sandbox', 'ufodata', i + 4995));
}
;(async () => {
// Establishes a connection to the server
let client = await Aerospike.connect(config);
// Check if records exist
let exists = await client.batchExists(keys, batchPolicy);
exists.forEach(result => {
if(result.status !== 0){
console.info('Key: %o does not exist', result.record.key.key);
}
});
// Close the connection to the server
client.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 records
let batchRecords = [];
for(i = 0; i < 10; i++){
batchRecords.push({
type: batchType.BATCH_READ,
key: new Aerospike.Key('sandbox', 'ufodata', i + 1),
readAllBins: true
// Can set 'readAllBins' false and specify bins
// bins: ['report', 'location']
});
}
;(async () => {
// Establishes a connection to the server
let client = await Aerospike.connect(config);
// Read records
let records = await client.batchRead(batchRecords);
// Access the records
records.forEach(result => {
let record = result.record;
console.info('Record: %o\\n', record.bins)
});
// Close the connection to the server
client.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 expressions
let expr1 = exp.maps.getByKey(exp.binMap("report"), exp.str("city"), exp.type.STR, map.returnType.VALUE);
let expr2 = exp.maps.getByKey(exp.binMap("report"), exp.str("state"), exp.type.STR, map.returnType.VALUE);
// Create batch of records
let batchRecords = [];
for(i = 0; i < 10; i++){
batchRecords.push({
type: batchType.BATCH_READ,
key: new Aerospike.Key('sandbox', 'ufodata', i + 1),
ops: [
exp.operations.read('city', expr1, 0),
exp.operations.read('state', expr2, 0)
]
});
}
;(async () => {
// Establishes a connection to the server
let client = await Aerospike.connect(config);
// Get 'city' and 'state' from report map for each record
let batchResult = await client.batchRead(batchRecords);
// Access the records
batchResult.forEach(result => {
let record = result.record;
console.info('Record: %o\\n', record.bins)
});
// Close the connection to the server
client.close();
})();

Read/write operations

The following example creates an array of ten keys and

  1. Defines an Operation Expression that compares the occurred bin value against the provided value, 20211231, and verifies the posted bin exists to determine the boolean value of the new recent key being added to the report map.
  2. Returns the report bin.
// Build the expression
let 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 records
let batchRecords = [];
for(i = 0; i < 10; i++){
batchRecords.push({
type: batchType.BATCH_WRITE,
key: new Aerospike.Key('sandbox', 'ufodata', i + 1),
ops: [
exp.operations.write('report', expr, 0),
op.read('report')
]
});
}
;(async () => {
// Establishes a connection to the server
let client = await Aerospike.connect(config);
// Execute the write operation and return the report bin
let batchResult = await client.batchWrite(batchRecords);
// Access the records
batchResult.forEach(result => {
let record = result.record;
console.info('Record: %o\\n', record.bins);
});
// Close the connection to the server
client.close();
})();

Deletes

The following example deletes the records from the database.

// Create batch of keys
let keys = [];
for(i = 0; i < 10; i++){
keys.push(new Aerospike.Key('sandbox', 'ufodata', i + 1))
}
;(async () => {
// Establishes a connection to the server
let client = await Aerospike.connect(config);
// Delete records
let batchResults = await client.batchRemove(keys);
// Close the connection to the server
client.close();
})();

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

  1. uses the ops1 array that combines the Operation Expression.
  2. uses exp1 which compares the occurred bin value against the provided value, 20211231 and verifies the posted bin exists to determine the boolean value of the new recent key being added to the report map.
  3. returns the report bin.

The record with user defined key 4001

  1. uses the ops2 array which contains a read Operation Expression that gets the length of the shape list from the report map and returns the value in a synthetic bin named numShapes.

The record with user defined key 4002

  1. uses the ops3 array which combines a write operation that updates the posted bin value, with a map operation that updates the city value in the report map.
  2. returns both the posted and report bins.

The record with user defined key 4003 is deleted from the database.

// Define Operation Expressions
let expr1 = exp.maps.put(exp.binMap('report'),
exp.and(
exp.gt(exp.binInt('occurred'), exp.int(20211231)),
exp.binExists('posted')
),
exp.str('recent')
);
let expr2 = exp.lists.size(
exp.maps.getByKey(exp.binMap('report'), exp.str('shape'), exp.type.LIST, map.returnType.VALUE)
);
// Create batch of records
let batchRecords = [
{
type: batchType.BATCH_WRITE,
key: new Aerospike.Key('sandbox', 'ufodata', 4000),
ops: [
exp.operations.write('report', expr1, 0),
op.read('report')
]
},
{
type: batchType.BATCH_READ,
key: new Aerospike.Key('sandbox', 'ufodata', 4001),
ops: [
exp.operations.read('numShapes', expr2, 0)
]
},
{
type: batchType.BATCH_WRITE,
key: new Aerospike.Key('sandbox', 'ufodata', 4002),
ops: [
op.write('posted', 20220602),
map.put('report', 'city', 'Cedarville'),
op.read('posted'),
op.read('report')
]
},
{
type: batchType.BATCH_REMOVE,
key: new Aerospike.Key('sandbox', 'ufodata', 4003)
}
];
;(async () => {
// Establishes a connection to the server
let client = await Aerospike.connect(config);
// Execute the batch
let batchResult = await client.batchWrite(batchRecords);
// Access the records
batchResult.forEach(result => {
let record = result.record;
console.info('Record: %o\\n', record.bins);
});
// Close the connection to the server
client.close();
})();

Code block

Expand this section for a single code block to execute a batch read/write operation
const Aerospike = require('aerospike');
const batchType = Aerospike.batchType;
const op = Aerospike.operations;
const map = Aerospike.maps;
const list = Aerospike.lists;
const exp = Aerospike.exp;
// Define host configuration
let config = {hosts: '127.0.0.1:3000'};
// Create a new batch policy
let batchPolicy = new Aerospike.BatchPolicy({
// An example that will always return true
filterExpression: exp.gt(exp.int(2), exp.int(1))
});
// Create the batch write policy
let batchWritePolicy = new Aerospike.BatchWritePolicy({
// An example that will always return true
filterExpression: exp.gt(exp.int(2), exp.int(1))
});
// Build the expression
let 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 records
let batchRecords = [];
for(i = 0; i < 10; i++){
batchRecords.push({
type: batchType.BATCH_WRITE,
key: new Aerospike.Key('sandbox', 'ufodata', i + 1),
ops: [
exp.operations.write('report', expr, 0),
op.read('report')
]
});
}
;(async () => {
// Establishes a connection to the server
let client = await Aerospike.connect(config);
// Execute the write operation and return the report bin
let batchResult = await client.batchWrite(batchRecords);
// Access the records
batchResult.forEach(result => {
let record = result.record;
console.info('Record: %o\\n', record.bins);
});
// Close the connection to the server
client.close();
})();
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?