44
55```
66aerospike.client.query(namespace[, set])
7+ aerospike.query.select(bins)
78aerospike.predicates.equals(bin, val)
89aerospike.predicates.between(bin, min, max)
910aerospike.query.where(_predicate_)
11+ aerospike.query.apply(module, function[, arguments])
1012res = aerospike.query.results([policy])
1113aerospike.query.foreach(callback[, policy])
1214```
@@ -19,15 +21,21 @@ The Query can be assigned a Predicate object using **aerospike.query.where()**,
1921then invoked using either ** aersopike.query.foreach()** or ** aerospike.query.results()** .
2022** foreach()** invokes a callback function for each of the records streaming back
2123from the query, while ** results** buffers these records and returns them as a
22- list.
24+ list. Without a predicate the query acts similar to a scan.
2325
24- The bins returned can be filtered by using ** aerospike.query.select()** .
26+ The bins returned can be filtered by using ** aerospike.query.select()** , which
27+ takes a list of bin names.
2528
2629Predicates currently come in two flavors, ** aerospike.predicates.between()** and
27- ** aerospike.predicates.equals()** .
30+ ** aerospike.predicates.equals()** . Currently, only one may be used per-query.
31+
32+ Finally, a stream UDF may be applied to the records streaming back from the
33+ query to aggregate the results.
2834
2935## Examples
3036
37+ ## Query with Callback Example
38+
3139``` python
3240# -*- coding: utf-8 -*-
3341import aerospike
@@ -51,8 +59,75 @@ query.foreach(matched_names, {'timeout':2000})
5159pp.pprint(names)
5260```
5361
54- ### See Also
62+ ### Aggregation Example
63+
64+ Assume we registered the following Lua module ** stream_udf.lua** with the
65+ cluster.
66+
67+ ``` lua
68+ local function having_ge_threshold (bin_having , ge_threshold )
69+ return function (rec )
70+ debug (" group_count::thresh_filter: %s > %s ?" , tostring (rec [bin_having ]), tostring (ge_threshold ))
71+ if rec [bin_having ] < ge_threshold then
72+ return false
73+ end
74+ return true
75+ end
76+ end
77+
78+ local function count (group_by_bin )
79+ return function (group , rec )
80+ if rec [group_by_bin ] then
81+ local bin_name = rec [group_by_bin ]
82+ group [bin_name ] = (group [bin_name ] or 0 ) + 1
83+ debug (" group_count::count: bin %s has value %s which has the count of %s" , tostring (bin_name ), tostring (group [bin_name ]))
84+ end
85+ return group
86+ end
87+ end
88+
89+ local function add_values (val1 , val2 )
90+ return val1 + val2
91+ end
92+
93+ local function reduce_groups (a , b )
94+ return map .merge (a , b , add_values )
95+ end
96+
97+ function group_count (stream , group_by_bin , bin_having , ge_threshold )
98+ if bin_having and ge_threshold then
99+ local myfilter = having_ge_threshold (bin_having , ge_threshold )
100+ return stream : filter (myfilter ) : aggregate (map {}, count (group_by_bin )) : reduce (reduce_groups )
101+ else
102+ return stream : aggregate (map {}, count (group_by_bin )) : reduce (reduce_groups )
103+ end
104+ end
105+ ```
106+
107+ Find the first name distribution of users in their twenties using a query
108+ aggregation:
109+
110+ ``` python
111+ # -*- coding: utf-8 -*-
112+ import aerospike
113+ from aerospike import predicates as p
114+ import pprint
115+
116+ config = { ' hosts' : [(' 127.0.0.1' , 3000 )] }
117+ client = aerospike.client(config).connect()
118+
119+ pp = pprint.PrettyPrinter(indent = 2 )
120+ query = self .client.query(' test' , ' users' )
121+ query.where(p.between(' age' , 20 , 29 ))
122+ query.apply(' stream_udf' , ' group_count' , [ ' first_name' ])
123+ names = query.results()
124+ pp.pprint(names)
125+ ```
126+
127+ ## See Also
55128
56129- [ Query] ( http://www.aerospike.com/docs/guide/query.html )
57130- [ Managing Queries] ( http://www.aerospike.com/docs/operations/manage/queries/index.html )
131+ - [ User Defined Functions] ( http://www.aerospike.com/docs/guide/udf.html )
132+ - [ Developing Stream UDFs] ( http://www.aerospike.com/docs/udf/developing_stream_udfs.html )
58133
0 commit comments