Aerospike C client manual --- query-query records, aerospike Client

Source: Internet
Author: User
Tags aerospike

Aerospike C client manual --- query-query records, aerospike Client
Query records

In addition to the primary index, the Aerospike C client also provides APIs to query databases through secondary indexes.

To use the secondary index to query data, first initialize and fill in an as_query object. Then, use aerospike_query_foreach () to execute the query for the initialized as_query object. A query can be:

  • Call the callback function one by one for the returned matching records.
  • Apply streaming user-defined functions (StreamUDF) to the returned set of qualified records, and call back the function for the StreamUDF returned results.

This chapter focuses on the first method. For more information about the second method, see [aggregation.

The following code is referenced in the example directory [examples/query_examples/simple], which is provided by the Aerospike C Client installation package.

Read:

  • Create a connection to a cluster.
  • Subindex management: describes how to create a secondary index.
Define Query

First, initialize and construct a query object. The following code snippet initializes a query object. The namespace of the data to be searched is named "test" and the set name is "demoset ", query records whose bin name is "test-bin" and whose integer value is 7. Equivalent to an SQL statement: "select * from test. demoset where test-bin equal 7 ".

as_query query;as_query_init(&query, "test", "demoset");as_query_where_inita(&query, 1);as_query_where(&query, "test-bin", integer_equals(7));

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

Call Query

Use the aerospike_query_foreach () function to call the query.

This call initializes a query task for each node in the cluster. Within the C client, there are five (NUM_QUERY_THREADS) query worker threads to process query tasks for all nodes. If the cluster has more than five nodes, the default number of processing threads is increased ideally to allow all nodes to process in parallel.

if (aerospike_query_foreach(&as, &err, NULL, &query, query_cb, NULL) !=        AEROSPIKE_OK) {    LOG("aerospike_query_foreach() returned %d - %s", err.code, err.message);    as_query_destroy(&query);    cleanup(&as);    exit(-1);}

If no secondary index is created on the specified query target, the "AEROSPIKE_ERR_INDEX_NOT_FOUND" error is returned. If an index is being created, the AEROSPIKE_ERR_INDEX_NOT_READABLE error is returned.

Processing result

In the preceding example, the aerospike_query_foreach () function uses the callback function query_cd as the parameter. The callback function has the following structure:

typedef bool (*aerospike_query_foreach_callback)(const as_val *value, void *udata);

For each record returned by any node, the callback function is called to process the record. There is no specific order for record processing. You may notice that the parameter value is of the const as_val * type, meaning that the callback function is not responsible for destroying it, and it is visible within the scope of the callback function. The callback function should not pass it out of scope.

When there are no more returned results to be processed, the return function is called using NULL as the value of the parameter value.

When the parameter value is expected to be a record, you can simply convert it to the record type using as_record_formval. If the parameter value is a record, this function returns a record object; otherwise, a null value is returned. You can also use as_val_type () to check the value type.

bool callback(const as_val *value, void *udata) {    if (value == NULL) {        // query is complete        return true;    }    as_record *rec = as_record_fromval(value);    if (rec != NULL) {        // process record    }    return true;}

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.


Link: http://www.aerospike.com/docs/client/c/usage/query/query.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.