Aerospike C用戶端手冊———鍵-值儲存—批量讀取記錄,aerospike用戶端

來源:互聯網
上載者:User

Aerospike C用戶端手冊———鍵-值儲存—批量讀取記錄,aerospike用戶端
批量讀取記錄

除了每次讀取單條記錄外,還可以在一個事務中從叢集中讀取多條記錄。相關的客戶Aerospike C用戶端API調用是:

  • aerospike_batch_get() — 返回所需記錄所有bin資料。
  • aerospike_batch_exists() — 返回所需記錄中繼資料(存留時間, 分代編號)。

後面的代碼引用自樣本目錄【examples/basic_examples/get】,由Aerospike C用戶端安裝包內建。

請先閱讀【建立串連】章節內容,理解如何建立與叢集的串連。

初始化批量請求

在執行批量調用前,首先初始化一個as_batch對象。下面的範例程式碼,初始化了一個要讀取1000條記錄的as_batch對象。

as_batch batch;as_batch_inita(&batch, 1000);
初始化需讀取記錄的鍵

一旦as_batch對象初始化過,應設定需要讀取記錄們的鍵:

for (uint32_t i = 0; i < 1000; i++) {  as_key_init_int64(as_batch_keyat(&batch, i), "test", "demoset", (int64_t)i);}

上面的調用,使用as_batch_keyat()函數設定了1000個鍵。在這裡,鍵的資料類型是整型(integer),範圍從0到1000。也可以使用不同資料類型的鍵,比如:字串(string)。這些鍵用於讀取記錄的儲存位置:namespace名稱為"test"、set名稱為“test-set”。

批量讀取記錄的鍵必須屬於同一個namespace。

從資料庫中讀取記錄

現在已經準備好執行讀取記錄的調用。

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

在Aerospike C用戶端內部,此調用按最佳處理請求的伺服器節點來分組記錄鍵,並為叢集中這些節點每個啟動一個批量任務。有6個(NUM_BATCH_THREADS)批量背景工作執行緒處理所有節點上的全部批量任務。若叢集中有多於6個節點,理想狀態下是增加預設線程數以讓全部節點平行處理。

處理結果集

上面的aerospike_batch_get()函數樣本使用回呼函數batch_read_cb做為參數。回呼函數具有如下型構:

bool (* aerospike_batch_read_callback)(const as_batch_read * results, uint32_t n, void * udata);

此回呼函數將在所有節點返回全部記錄後被調用,按照記錄鍵被遞交的順序。

應用可遍曆as_batch_read的結果清單並處理每一個記錄。結果記錄及數值只可見於回呼函數範圍,若需要傳遞出回呼函數範圍以外,必須顯式地拷貝。

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

若想在每次回調傳遞一個全域對象出來,請在調用aerospike_batch_get()時提供一個userdata類型的參數。

讀取記錄中繼資料

做為讀取整條記錄資料的替代,可以使用一個等價調用,但只返回記錄的中繼資料(比如:存留時間、分代編號)。應用只想找出記錄是否存在而不想承擔實際讀取資料成本時,可優先使用此調用。

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);}
清理資源

若記錄鍵是從堆上分配,應在使用後清理釋放。


原文連結: http://www.aerospike.com/docs/client/c/usage/kvs/batch.html 譯       者: 歪脖大肚子Q 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.