![]() |
The Aerospike Query Operations provide the ability to query data in the Aerospike database. The queries can optionally be performed on secondary indexes, which have been created in the database.
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. 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.
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.
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:
When you are finished with the query, you should destroy the resources allocated to it.
Data Structures | |
struct | as_query |
Typedefs | |
typedef bool(* | aerospike_query_foreach_callback) (const as_val *val, void *udata) |
typedef bool(* | as_async_query_record_listener) (as_error *err, as_record *record, void *udata, as_event_loop *event_loop) |
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. Multiple threads will likely be calling this callback in parallel. Therefore, your callback implementation should be thread safe.
A regular foreground query always returns as_record instances:
An aggregation query using a UDF returns as_val instances. The as_val type depends on what the UDF returns:
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 150 of file aerospike_query.h.
typedef bool(* as_async_query_record_listener) (as_error *err, as_record *record, void *udata, as_event_loop *event_loop) |
Asynchronous query user callback. This function is called for each record returned. This function is also called once when the query completes or an error has occurred.
err | This error structure is only populated on command failure. NULL on success. |
record | Returned record. The record will be NULL on final query completion or query error. |
udata | User data that is forwarded from asynchronous command function. |
event_loop | Event loop that this command was executed on. Use this event loop when running nested asynchronous commands when single threaded behavior is desired for the group of commands. |
true
to continue to the next value. Otherwise, the query will end. Definition at line 167 of file aerospike_query.h.
AS_EXTERN as_status aerospike_query_async | ( | aerospike * | as, |
as_error * | err, | ||
const as_policy_query * | policy, | ||
as_query * | query, | ||
as_async_query_record_listener | listener, | ||
void * | udata, | ||
as_event_loop * | event_loop ) |
Asynchronously execute a query and call the listener function for each result item. Standard queries are supported, but aggregation queries are not supported in async mode.
as | Aerospike cluster instance. |
err | Error detail structure that is populated if an error occurs. |
policy | Query policy configuration parameters, pass in NULL for default. |
query | Query definition. |
listener | The function to be called for each returned value. |
udata | User-data to be passed to the callback. |
event_loop | Event loop assigned to run this command. If NULL, an event loop will be chosen by round-robin. |
AS_EXTERN as_status aerospike_query_background | ( | aerospike * | as, |
as_error * | err, | ||
const as_policy_write * | policy, | ||
const as_query * | query, | ||
uint64_t * | query_id ) |
Apply user defined function on records that match the query filter. Records are not returned to the client. This asynchronous server call will return before the command is complete. The user can optionally wait for command completion.
as | Aerospike cluster instance. |
err | Error detail structure that is populated if an error occurs. |
policy | Write configuration parameters, pass in NULL for default. |
query | The query to execute against the cluster. |
query_id | The id for the query job, which can be used for obtaining query status. |
AS_EXTERN as_status aerospike_query_foreach | ( | aerospike * | as, |
as_error * | err, | ||
const as_policy_query * | policy, | ||
as_query * | query, | ||
aerospike_query_foreach_callback | callback, | ||
void * | udata ) |
Execute a query and call the callback function for each result item. Multiple threads will likely be calling the callback in parallel. Therefore, your callback implementation should be thread safe.
as | Aerospike cluster instance. |
err | Error detail structure that is populated if an error occurs. |
policy | Query policy configuration parameters, pass in NULL for default. |
query | Query definition. |
callback | Query callback function called for each result value. |
udata | User-data to be passed to the callback. |
|
inlinestatic |
Check the progress of a background query running on the database.
as | Aerospike cluster instance. |
err | Error detail structure that is populated if an error occurs. |
policy | Info configuration parameters, pass in NULL for default. |
query | The query that was executed against the cluster. |
query_id | The id for the query job, which can be used for obtaining query status. |
info | Information about this background query, to be populated by this operation. |
Definition at line 452 of file aerospike_query.h.
References aerospike_job_info().
AS_EXTERN as_status aerospike_query_partitions | ( | aerospike * | as, |
as_error * | err, | ||
const as_policy_query * | policy, | ||
as_query * | query, | ||
as_partition_filter * | pf, | ||
aerospike_query_foreach_callback | callback, | ||
void * | udata ) |
Query records with a partition filter. Multiple threads will likely be calling the callback in parallel. Therefore, your callback implementation should be thread safe. Requires server version 6.0+.
as | Aerospike cluster instance. |
err | Error detail structure that is populated if an error occurs. |
policy | Query policy configuration parameters, pass in NULL for default. |
query | Query definition. |
pf | Partition filter. |
callback | Query callback function called for each result value. |
udata | User-data to be passed to the callback. |
AS_EXTERN as_status aerospike_query_partitions_async | ( | aerospike * | as, |
as_error * | err, | ||
const as_policy_query * | policy, | ||
as_query * | query, | ||
as_partition_filter * | pf, | ||
as_async_query_record_listener | listener, | ||
void * | udata, | ||
as_event_loop * | event_loop ) |
Asynchronously query records with a partition filter. Standard queries are supported, but aggregation queries are not supported in async mode. Requires server version 6.0+.
as | Aerospike cluster instance. |
err | Error detail structure that is populated if an error occurs. |
policy | Query policy configuration parameters, pass in NULL for default. |
query | Query definition. |
pf | Partition filter. |
listener | The function to be called for each returned value. |
udata | User-data to be passed to the callback. |
event_loop | Event loop assigned to run this command. If NULL, an event loop will be chosen by round-robin. |
|
inlinestatic |
Wait for a background query to be completed by servers.
as | Aerospike cluster instance. |
err | Error detail structure that is populated if an error occurs. |
policy | Info configuration parameters, pass in NULL for default. |
query | The query that was executed against the cluster. |
query_id | The id for the query job, which can be used for obtaining query status. |
interval_ms | Polling interval in milliseconds. If zero, 1000 ms is used. |
Definition at line 429 of file aerospike_query.h.
References aerospike_job_wait().
|
related |
Apply a function to the results of the query.
query | The query to apply the function to. |
module | The module containing the function to invoke. |
function | The function in the module to invoke. |
arglist | The arguments to use when calling the function. |
Destroy the query and associated resources.
|
related |
Deserialize bytes to query definition. Query definition is assumed to be on the stack. as_query_destroy() should be called when done with the query definition.
Create query definition on the heap and deserialize bytes to that query definition. as_query_destroy() should be called when done with the query definition.
|
related |
If using query pagination, did the previous paginated query with this query instance return all records?
Definition at line 899 of file as_query.h.
References as_partitions_status::done, and as_query::parts_all.
Create and initialize a new heap allocated as_query.
ns | The namespace to query. |
set | The set to query. |
Select bins to be projected from matching records.
You have to ensure as_query.select has sufficient capacity, prior to adding a bin. If capacity is sufficient then false is returned.
query | The query to modify. |
bin | The name of the bin to select. |
Initializes as_query.select
with a capacity of n
using malloc()
.
For stack allocation, use as_query_select_inita()
.
query | The query to initialize. |
n | The number of bins to allocate. |
|
related |
Set if records should be read in pages in conjunction with max_records policy. If true, the client will save the status of all partitions after the query completes. The partition status can be used to resume the query if terminated early by error, user callback, or max_records being reached. Use as_query_set_partitions() or as_partition_filter_set_partitions() to resume a query.
The partition status will be destroyed when as_query_destroy() is called.
Definition at line 873 of file as_query.h.
References as_query::paginate.
|
related |
Set completion status of all partitions from a previous query that ended early. The query will resume from this point.
Definition at line 886 of file as_query.h.
References as_partitions_status_reserve(), and as_query::parts_all.
|
related |
Serialize query definition to bytes.
|
related |
Add a predicate to the query.
You have to ensure as_query.where has sufficient capacity, prior to adding a predicate. If capacity is insufficient then false is returned.
String predicates are not owned by as_query. If the string is allocated on the heap, the caller is responsible for freeing the string after the query has been executed. as_query_destroy() will not free this string predicate.
query | The query add the predicate to. |
bin | The name of the bin the predicate will apply to. |
type | The type of predicate. |
itype | The type of index. |
dtype | The underlying data type that the index is based on. |
... | The values for the predicate. |
Initializes as_query.where
with a capacity of n
using malloc()
.
For stack allocation, use as_query_where_inita()
.
query | The query to initialize. |
n | The number of as_predicate to allocate. |
|
related |
Add a predicate and context to the query.
You have to ensure as_query.where has sufficient capacity, prior to adding a predicate. If capacity is insufficient then false is returned.
String predicates are not owned by as_query. If the string is allocated on the heap, the caller is responsible for freeing the string after the query has been executed. as_query_destroy() will not free this string predicate.
query | The query add the predicate to. |
bin | The name of the bin the predicate will apply to. |
ctx | The CDT context describing the path to locate the data to be indexed. |
type | The type of predicate. |
itype | The type of index. |
dtype | The underlying data type that the index is based on. |
... | The values for the predicate. |