Aerospike C Client Manual ——— user-defined functions-apply UDFs to records

Source: Internet
Author: User
Tags aerospike

Applying UDFs to records

The Aerospike C Client API 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 containing the function being applied must first be registered with the Aerospike server. See the section " registering user-defined Functions " to learn how to register using C API, or read the AQL Handbook to learn how to register with external tools.

The following code references the sample directory from the " Examples/basic_examples/get " , comes with the Aerospike C client installation package.

Please read the section "Creating connections" to understand how to establish a connection to a cluster.

Defining UDFs

The function bin_transform is defined in a 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, and three parameter names are "Bin_name", "X", "Y", respectively. It performs operations on the bin specified by "Bin_name" and updates the record data, and then returns the result value of the bin's operation.

Initialize record key (key)

Below we create a key for the sample code. The key is the string "Test-key", where the namespace name of the data is called "Test", the set name is called "Test-set"

as_key key;as_key_init_str(&key, "test", "test-set", "test-key");
Passing parameters to the UDF

user-defined function" Bin_transform ", Requires a string argument and two integer (integer) parameters, so a parameter list needs to be populated.

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 a UDF on a record

Using the record key, you can now invoke a user-defined function on the specified record in 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 function Bin_trransform () is returned in the Parameter object result.

If the record key does not exist, the UDF function is returned when it is found Aerospike_err_record_not_found, otherwise a UDF run-time error is returned, and the error details are returned in the member domain of As_error.

Clean up resources

When you no longer need the returned results, you should free it and its associated resources:

as_val_destroy(&result);

Also, clean up the parameter list:

as_arraylist_destroy(&args);

The record key here does not need to be explicitly destroyed because it and its members are created on the stack.


original link: http://www.aerospike.com/docs/client/c/usage/udf/apply.html Translator: crooked Neck belly Q  

Aerospike C Client Manual ——— user-defined functions-apply UDFs to records

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.