Aerospike C client manual --- key-value storage-Batch read records, aerospike Client
Batch read records
In addition to reading a single record each time, you can also read multiple records from the cluster in a transaction. The API calls of the relevant Aerospike C client are:
aerospike_batch_get()-Return All bin data to be recorded.
aerospike_batch_exists()-Return the required record metadata (survival time, generation number ).
The following code is referenced in the example directory examples/basic_examples/get, which is provided by the Aerospike C Client installation package.
Read the [create a connection] section to learn how to establish a connection with a cluster.
Initialize batch requests
Before executing a batch call, initialize an as_batch object. The following sample code initializes an as_batch object for reading 1000 records.
as_batch batch;as_batch_inita(&batch, 1000);
Initialize the key for reading records
Once the as_batch object has been initialized, set the keys for reading records:
for (uint32_t i = 0; i < 1000; i++) { as_key_init_int64(as_batch_keyat(&batch, i), "test", "demoset", (int64_t)i);}
The above call sets 1000 keys using the as_batch_keyat () function. Here, the key data type is integer, ranging from 0 to 1000. You can also use keys of different data types, such as string ). These keys are used to store records: namespace is named "test" and set is named "test-set ".
The keys used to read records in batches must belong to the same namespace.
Read records from the database
Now you are ready to execute the call to read records.
if (aerospike_batch_get(&as, &err, NULL, &batch, batch_read_cb, NULL) != AEROSPIKE_OK) { LOG("aerospike_batch_get() returned %d - %s", err.code, err.message); cleanup(&as); exit(-1);}
Within the Aerospike C client, this call groups record keys based on the server nodes that best process requests and starts a batch task for each of these nodes in the cluster. There are six (NUM_BATCH_THREADS) Batch worker threads that process all Batch Tasks on all nodes. If the cluster has more than 6 nodes, it is ideal to add the default number of threads for Parallel Processing on all nodes.
Processing result set
The above aerospike_batch_get () function example uses the callback function batch_read_cb as the parameter. The callback function has the following structure:
bool (* aerospike_batch_read_callback)(const as_batch_read * results, uint32_t n, void * udata);
This callback function will be called after all records are returned by all nodes, in the order that the record key is submitted.
The application can traverse the as_batch_read result list and process each record. The Result Records and values can only be seen in the callback function scope. If you want to transfer them out of the callback function scope, you must explicitly copy them.
boolbatch_read_cb(const as_batch_read* results, uint32_t n, void* udata){ uint32_t n_found = 0; for (uint32_t i = 0; i < n; i++) { LOG("index %u, key %" PRId64 ":", i, as_integer_getorelse((as_integer*)results[i].key->valuep, -1)); if (results[i].result == AEROSPIKE_OK) { LOG(" AEROSPIKE_OK"); n_found++; } else if (results[i].result == AEROSPIKE_ERR_RECORD_NOT_FOUND) { // The transaction succeeded but the record doesn't exist. LOG(" AEROSPIKE_ERR_RECORD_NOT_FOUND"); } else { // The transaction didn't succeed. LOG(" error %d", results[i].result); } } return true;}
If you want to pass a Global Object in each callback, provide a userdata-type parameter when calling aerospike_batch_get.
Read record metadata
As an alternative to reading the entire record data, you can use an equivalent call, but only return the record metadata (such as the survival time and generation number ). When the application only wants to identify whether the record exists and does not want to bear the actual data read cost, this call can be used first.
if (aerospike_batch_exists(&as, &err, NULL, &batch, batch_read_cb, NULL) != AEROSPIKE_OK) { LOG("aerospike_batch_exists() returned %d - %s", err.code, err.message); cleanup(&as); exit(-1);}
Clear Resources
If the record key is allocated from the stack, it should be cleared and released after use.
Link: http://www.aerospike.com/docs/client/c/usage/kvs/batch.html
Translated by: Q