Aerospike C Client Manual ——— query-query record

Source: Internet
Author: User
Tags aerospike

Query records

When you query In addition to using the primary index, the Aerospike C client also provides an API to query the database by retrieving the secondary index.

In order to query data using a secondary index, a As_query object should be initialized and populated first, and then the query is executed using Aerospike_query_foreach () to initialize the completed As_query object. A query can:

    • Call the callback function one by one on the returned satisfied condition record.
    • Applies a streaming user-defined function (STREAMUDF) to the returned set of satisfied condition records, and then invokes the call function back to the streamudf return result.

This chapter focuses on the first way, for the second approach, see the Aggregations section.

The following code references the sample directory from the " examples/query_examples/simple " , comes with the Aerospike C client installation package.

Please read first:

    • "Create a Connection" section to understand how to establish a connection to a cluster.
    • Sub-index management Section to understand how to create a secondary index.
Defining a query

first, initialize and build a query object. The following code fragment initializes a query object that contains the namespace name "Test", a set name called "Demoset", and a record that thebin name is "Test-bin" and the integer value equals 7. Equivalent to the 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 a "where" statement will cause a full-library scan.

Call Query

The query uses the Aerospike_query_foreach () function call.

This call initializes a query task for each node in the cluster. Within the C client, there are 5 (num_query_threads) query worker threads that process query tasks for all nodes. If the cluster has more than 5 nodes, It is desirable to increase the default number of processing threads in order for all nodes to be processed 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, a "Aerospike_err_index_not_found" error is returned. If an index is being created, a aerospike_err_index_not_readable error is returned.

Processing results

In the above example, the Aerospike_query_foreach () function uses a callback function QUERY_CD as a parameter. The callback function has the following type structure:

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

for each record returned by any node, callback functions are invoked to process the records. Record processing does not have a specific order. You may notice that the parameter value is a 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 outside the scope.

When there are no more return results to be processed, a null value (NULL) is called as the value of the parameter value, calling back to the calling function.


bool  callback ( Span class= "Hljs-keyword" style= "Color:rgb (170,13,145)" >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 ;}

To pass a global object at each callback, provide a parameter of type UserData when calling Aerospike_query_foreach ().

Clean up resources

Once the query is finished, the query object and its member object can be safely freed using the As_query_destroy () function. Note In our example, it is not necessary to explicitly call As_query_destroy (), because the As_query object is allocated from the stack, and As_query_where_inita () is used to avoid using the heap internally.


original link: http://www.aerospike.com/docs/client/c/usage/query/query.html Translator: Crooked neck belly Q  

Aerospike C Client Manual ——— query-query record

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.