Aerospike C client manual --- aggregation-record aggregation, aerospike Client

Source: Internet
Author: User
Tags define stream how to use sql aerospike

Aerospike C client manual --- aggregation-record aggregation, aerospike Client
Record Aggregation

There are various forms to process the secondary index query results. A common form of aggregation is to apply a function to the entire query result set.

Many developers are familiar with how to use SQL to define aggregate queries in databases.

For example, the following is an SQL statement that counts the number of records in a database:

SELECT count(*)FROM test.demo WHERE d = 50

This statement counts the number of records where the value of "d" in the "test. demo" table is 50.

In Aerospike, query and user-defined functions are used to complete this task.

Read:

  • Create a connection to a cluster.
  • The secondary index management section describes how to create a new secondary index.
  • [Query] to understand how to use the C client for query.
Define Query

Using the API of the Aerospike C client to implement the same functions of SQL statements, you can build the following query:

as_query query;as_query_init(&query, "test", "demoset");as_query_where_inita(&query, 1);as_query_where(&query, "d", integer_equals(50));as_query_apply(&query, "mymodule", "mycount", NULL);

For the above query object, the namespace where the queried data is located is named "test" and the set name is "demoset ", search for records whose bin name is "d" and whose integer value is 50. A streamUDF named "mycount ()" will be applied to the query result set. This streamUDF can be found in the module mymodule.

Note: queries without the "where" statement will scan the entire database.

Define Stream UDF

The Stream UDF mycount () is defined in the module mymodule, which can process data streams. UDF mycont () is defined:

local function one(rec)    return 1endlocal function add(a, b)    return a + bendfunction mycount(stream)    return stream : map(one) : reduce(add);end

The mycount () function is a Stream UDF applied to a Stream. In this case, the query result stream is used. We add operations to be performed on the query results in the stream. The operations performed on the stream are:

  • map-Map a value from the stream to another value. In this example, the ing is defined as the one () function, and the ing record is to the value 1.
  • reduce-Multiple values are grouped into one value from the stream. In this example, sum up two values in the stream. These values are a bunch of values from the ing function 1.

The final result is a stream containing a single value: count, or more technically, which is the sum of "1" corresponding to each record in the result set.

Register UDF

Before a query can use streamUDF, the UDF must be registered with the Aerospike server.

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

Use aerospike_query_foreach () to execute the query:

if (aerospike_query_foreach(&as, &err, NULL, &query, each_value, NULL) != AEROSPIKE_OK) {    fprintf(stderr, "err(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line);}

This will execute this query in the database.

Processing result

Call the each_value () function one by one for the values returned by the query (). Function implementation may be similar:

bool each_value(const as_val *val, void *udata) {    if (val == NULL) {        // query is complete        return true;    }    as_integer *ival = as_integer_fromval(val);    if (ival == NULL) {        // abort the query        return false;    }    // process the value    return true;}

In the preceding example, an integer is returned, indicating the number of records that meet the query conditions .?????? This is the case in the original article.

If you want to pass a global object during each callback, provide a userdata-type parameter when calling aerospike_query_foreach.

Clear Resources

Once the query ends, the query object and its member objects can be safely released using the as_query_destroy () function. Note that in our example, you do not need to explicitly call as_query_destroy () because it is an as_query object allocated from the stack and as_query_where_inita () is used to avoid internal use of the heap.


Connection: http://www.aerospike.com/docs/client/c/usage/query/aggregate.htmlTranslated by: Q  

Related Article

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.