Read records using "aerospike_batch_read"

Hello Experts,

I went through “aerospike_batch_get” function to read records in batch, where we can define call_back function to read each record. Like wise what is the way to read record using “aerospike_batch_read”, Can someone please advice.

I have referred below sample code from Aerospike.

     as_batch_read_records records;
     as_batch_read_inita(&records, 10);
     char* bin_names[] = {"bin1", "bin2"};
     char* ns = "ns";
     char* set = "set";
     as_batch_read_record* record = as_batch_read_reserve(&records);
     as_key_init(&record->key, ns, set, "key1");
     record->bin_names = bin_names;
     record->n_bin_names = 2;
     record = as_batch_read_reserve(&records);
     as_key_init(&record->key, ns, set, "key2");
     record->read_all_bins = true;
     if (aerospike_batch_read(&as, &err, NULL, &records) != AEROSPIKE_OK) {
        fprintf(stderr, "error(%d) %s at [%s:%d]", err.code, err.message, err.file, err.line);
      }
     as_batch_read_destroy(&records);

here, how we can retrieve data from “records”.

I would try the following after the _read() and before _destroy().

 if (err) {
            fprintf(stderr, "Command failed: %d %s\n", err->code, err->message);
  }
  else {
            as_vector* list = &records->list;
            for (uint32_t i = 0; i < list->size; i++) {
                  as_batch_read_record* record = as_vector_get(list, i);
                  // Process record
            }
   }

Thanks Piyush, I tried with your code, but getting below error while compiling,

i have included, aerospike_batch.h, as_vector.h header files. Please suggest what could be missing. ^

Looks like dereferencing error. In your case records is not a pointer. did u try &records.list? i did not compile