Aerospike C client manual --- user-defined functions-apply UDF to records, aerospikeudf
Apply UDF to record
The API of the Aerospike C client provides aerospike_key_apply () to apply a user-defined function to a record in the database.
Before using the aerospike_key_apply () operation, the UDF module that contains the function to be applied must first register with the Aerospike server. See [Register User-Defined Functions] To learn how to register using c api, or read the [aql manual] To learn how to register using external tools.
The following code references [examples/basic_examples/get] 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.
Define UDF
The bin_transform function is defined in the module named "basice_udf.
function bin_transform(record, bin_name, x, y) record[bin_name] = (record[bin_name] * x) + y aerospike:update(record) return record[bin_name]end
This function is a simple algorithm with three parameters: "bin_name", "x", and "y". It executes operations on the bin specified by "bin_name", updates the record data, and returns the operation result value of this bin.
KEY)
When applying a user-defined function to a record, you must use the key to identify the record in the database. Next we will create a key for the sample code. The key is a string "test-key". The namespace where the data is stored is named "test" and the set name is "test-set ". Other data types can also be used as keys, such as integer or Binary Large Object blocks (blob ).
as_key key;as_key_init_str(&key, "test", "test-set", "test-key");
PASS Parameters to UDF
The User-Defined Function "bin_transform" requires a string parameter and two integer Parameters. Therefore, you need to fill in a parameter list.
as_arraylist args;as_arraylist_inita(&args, 3);as_arraylist_append_str(&args, "test-bin-2");as_arraylist_append_int64(&args, 4);as_arraylist_append_int64(&args, 400);
Apply UDF to records
With the record key, you can now call the User-Defined Function on the specified record of the database:
as_val * result = NULL;if (aerospike_key_apply(&as, &err, NULL, &key, "mymodule", "add", (as_list *) &args, &result) != AEROSPIKE_OK) { fprintf(stderr, "err(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);}
The Return Value of the UDF bin_trransform () function is returned in the result of the parameter object.
If the record corresponding to the record key does not exist, AEROSPIKE_ERR_RECORD_NOT_FOUND is returned when the UDF function is found. Otherwise, an error is returned when the UDF is running. The error details are returned in the as_error member domain.
Clear Resources
When the returned results are no longer needed, release them and their related resources:
as_val_destroy(&result);
The parameter list is also cleared:
as_arraylist_destroy(&args);
The record key does not need to be explicitly destroyed because it and its members are created on the stack.
Link: http://www.aerospike.com/docs/client/c/usage/udf/apply.htmlTranslated by: Q