![]() |
The Aerospike Query Operations provide the ability to query data in the Aerospike database. The queries can only be performed on secondary indexes, which have been created in the database. To scan all the records in the database, then you must use the Scan Operations.
Before you can execute a query, you first need to build a query using as_query. See as_query for details on building queries.
Once you have a query defined, then you can execute the query using either:
The two operations are very similar in that they provide the ability to read the records returned from a query. However, they differ in how they manage resources.
When aerospike_query_foreach() is executed, it will process the results and create records on the stack. Because the records are on the stack, they will only be available within the context of the callback function. If you want the record available outside the callback, then you must use aerospike_query_stream().
When aerospike_query_stream() is executed, it will process the results and create records on the heap. These records are sent to the stream's write callback. Because the records are on the heap, the stream is able to pass the record outside the scope of the write callback. This form of processing is more expensive in terms CPU, but provides added flexibility.
First, we define a query using as_query. The query will be for the "test" namespace and "demo" set. We will add a where predicate on "bin2", on which we have already created a secondary index. Also, we will limit the results to 100 records.
Now that we have a query defined, we want to execute it using aerospike_query_foreach().
The callback provided to the function above is implemented as:
An as_query is simply a query definition, so it does not contain any state, allowing it to be reused for multiple query operations.
When you are finished with the query, you should destroy the resources allocated to it:
Typedefs | |
typedef bool(* | aerospike_query_foreach_callback )(const as_val *val, void *udata) |
Functions | |
as_status | aerospike_query_foreach (aerospike *as, as_error *err, const as_policy_query *policy, const as_query *query, aerospike_query_foreach_callback callback, void *udata) |
typedef bool(* aerospike_query_foreach_callback)(const as_val *val, void *udata) |
This callback will be called for each value or record returned from a query.
The aerospike_query_foreach() function accepts this callback.
val | The value received from the query. |
udata | User-data provided to the calling function. |
true
to continue to the next value. Otherwise, iteration will end. Definition at line 142 of file aerospike_query.h.
as_status aerospike_query_foreach | ( | aerospike * | as, |
as_error * | err, | ||
const as_policy_query * | policy, | ||
const as_query * | query, | ||
aerospike_query_foreach_callback | callback, | ||
void * | udata | ||
) |
Execute a query and call the callback function for each result item.
as | The aerospike instance to use for this operation. |
err | The as_error to be populated if an error occurs. |
policy | The policy to use for this operation. If NULL, then the default policy will be used. |
query | The query to execute against the cluster. |
callback | The callback function to call for each result value. |
udata | User-data to be passed to the callback. |