標籤:分布式 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用戶端手冊———使用者定義函數—註冊使用者定義函數