AerospikeC client manual --- user-defined functions-Register User-Defined Functions
Registering a user-defined function Aerospike C client provides the ability to register, update, or remove a user-defined function (UDF) module in a database. Currently, user-defined functions only support LUA.
Aerospike_udf_put ()-register or update the UDF module.
Aerospike_udf_remove ()-remove the UDF module.
The following code references [examples/basic_examples/udf] in the sample Directory, which is provided by the Aerospike C Client installation package.
Read the [create a connection] section to learn how to establish a connection with a cluster.
Read UDF from File
It is very likely that the module to be registered is saved in a file. So first read this file:
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);
Register UDF with the Aerospike Server
Once the UDF content is converted to the as_bytes object format, you can register the function.
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);
This call sends the UDF module to a node in the cluster. This node will spread the UDF to other nodes in the cluster.
If you need to update the UDF function at any time, simply register a new copy with the same module name.
Generally, UDF registration takes only a few seconds to register all nodes in the cluster.
Check whether the UDF module is correctly registered
The best way to check whether the UDF module is correctly registered is by using the aql tool. See [aql manual ].
Remove UDF from server
If the server no longer needs a UDF module at any time, you can remove it from the cluster.
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;}