Aerospike C用戶端手冊———使用者定義函數—註冊使用者定義函數

來源:互聯網
上載者:User

標籤:分布式   aerospike   shard-nothing   nosql   記憶體資料庫   

註冊使用者定義函數

Aerospike C 用戶端提供在資料庫中註冊、更新或移除一個使用者定義函數(UDF)模組的能力。目前,使用者定義函數僅支援LUA語言。

  • aerospike_udf_put() — 註冊或更新UDF模組。
  • aerospike_udf_remove() — 移除UDF模組。

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

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

從檔案讀取UDF

很可能,試圖註冊的模組儲存在一個檔案中。所以首先讀入這個檔案:

FILE* file = fopen("myudf.lua", "r");if (! file) {    LOG("cannot open script file %s : %s", udf_file_path, strerror(errno));    return false;}// Read the file‘s content into a local buffer.uint8_t* content = (uint8_t*)malloc(1024 * 1024);if (! content) {    LOG("script content allocation failed");    return false;}uint8_t* p_write = content;int read = (int)fread(p_write, 1, 512, file);int size = 0;while (read) {    size += read;    p_write += read;    read = (int)fread(p_write, 1, 512, file);}fclose(file);// Wrap the local buffer as an as_bytes object.as_bytes udf_content;as_bytes_init_wrap(&udf_content, content, size, true);
向Aerospike伺服器註冊UDF

一旦UDF內容轉換到as_bytes對象格式,就可以註冊函數。

as_error err;// Register the UDF file in the database cluster.if (aerospike_udf_put(&as, &err, NULL, "myudf", AS_UDF_TYPE_LUA,        &udf_content) != AEROSPIKE_OK) {    LOG("aerospike_udf_put() returned %d - %s", err.code, err.message);}// This frees the local buffer.as_bytes_destroy(&udf_content);

此調用將發送UDF模組到叢集中某一節點。這個節點會將UDF傳播到叢集中其它節點。

若在任何時候,需要更新UDF功能,簡單地以相同模組名稱重新註冊新的拷貝即可。

通常UDF註冊只需要幾秒就可以註冊到叢集中所有節點。

檢查UDF模組是否正確註冊

檢查UDF模組是否正確註冊的最佳方法是通過使用aql工具。請參見【aql手冊】。

從伺服器移除UDF

若在任何時候伺服器不再需要某UDF模組,可從叢集中移除它。

as_error err;if (aerospike_udf_remove(&as, &err, NULL, "myudf") != AEROSPIKE_OK) {    LOG("aerospike_udf_remove() returned %d - %s", err.code, err.message);    return false;}

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

Aerospike C用戶端手冊———使用者定義函數—註冊使用者定義函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.