Aerospike C Client Manual ——— key-value store-bulk Read records

Source: Internet
Author: User
Tags aerospike value store

Bulk Read Records

In addition to reading a single record each time, multiple records can be read from the cluster in one transaction. The associated client Aerospike C Client API calls are:

    • aerospike_batch_get()-Returns all bin data required to record.
    • aerospike_batch_exists()-Returns the required record metadata (time-to-live, generational number).

The following code references the sample directory from the " Examples/basic_examples/get ", with the Aerospike C client installation package.

Please read the section "Creating connections" to understand how to establish a connection to a cluster.

Initializing a bulk request

A As_batch object is initialized first before a bulk call is performed. The following example code initializes a As_batch object to read 1000 records.

as_batch batch;as_batch_inita(&batch, 1000);
Initialize the key to read the record

Once the As_batch object has been initialized, you should set the key that requires reading the 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, using the As_batch_keyat () function, sets 1000 keys. Here, the data type of the key is Integer (integer), ranging from 0 to 1000. You can also use keys of different data types, such as: string. These keys are used to read where records are stored: The namespace name is "test", and the set name is called "Test-set".

The keys for bulk read records must belong to the same namespace.

Reading records from the database

The call to read the record is now ready for execution.

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 );}   

inside the aerospike C client, this call groups the record keys by the server node that best handles the requests, and starts a batch task for each of those nodes in the cluster. There are 6 (num_batch_threads) batch worker threads that process all the bulk tasks on all nodes. If there are more than 6 nodes in the cluster, it is desirable to increase the default number of threads so that all nodes are processed in parallel.

Working with result sets

The Aerospike_batch_get () function example above uses the callback function BATCH_READ_CB as a parameter. The callback function has the following type structure:

bool  (* aerospike_batch_read_callback) (const  As_batch_read * results, uint32_t N, void  * udata); 
       

This callback function will be called after all nodes have returned all records, according to the order in which the record keys are submitted.

Apps can traverse the results list of as_batch_read and process each record. Results records and values are only visible in the scope of the callback function and must be copied explicitly if the callback function scope needs to be passed out.

BOOLBATCH_READ_CB (Constas_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-&GT;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 at each callback, provide a parameter of type UserData when calling Aerospike_batch_get ().

Reading record metadata

As an alternative to reading the entire record data, an equivalent call can be used, but only the metadata of the record is returned (for example: Time to live, generational number). This call is preferred when the app wants to find out if the record exists and does not want to incur the actual read data cost.

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 );}   
Clean up resources

If the record key is allocated from the heap, it should be cleaned and released after use.


original link: http://www.aerospike.com/docs/client/c/usage/kvs/batch.html Translator: Crooked neck belly Q

Aerospike C Client Manual ——— key-value store-bulk Read records

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.