Post by devops01 » Wed May 14, 2014 9:59 am
Aggregation UDFs can accept arguments. See the examples at the end of our dev guide.
https://docs.aerospike.com/display/V3/Aggregation+Guide
The args can be passed from our java/c client when invoking the UDF.
https://docs.aerospike.com/display/V3/J … d+Function
One can write an Aggregation UDF where the UDF will accept 4 arguments.
When defining the stream, and can dynamically define the filter/map/reduce functions using the arguments (the example shows only for filter function).
One of our engineer provided the following example:
The instructions to run are as follows
#create secondary index on bin0
$ aql -c “create index myind1 on test.demo (bin0) numeric”
#Inserting sample data
$ for i in {1…1000}; do aql -c “insert into test.demo(PK, bin0, bin1, bin2, bin3, bin4) values (‘key$i’, $i,expr $i / 2
,expr $i / 3
,expr $i / 4
,expr $i / 5
)”; done
#The data looks something like this
$ aql -c ‘select * from test.demo where bin0 between 1 and 100’
±-----±-----±-----±-----±-----+
| bin0 | bin1 | bin2 | bin3 | bin4 |
±-----±-----±-----±-----±-----+
| 1 | 0 | 0 | 0 | 0 |
| 2 | 1 | 0 | 0 | 0 |
| 3 | 1 | 1 | 0 | 0 |
| 4 | 2 | 1 | 1 | 0 |
| 5 | 2 | 1 | 1 | 1 |
| 6 | 3 | 2 | 1 | 1 |
| 7 | 3 | 2 | 1 | 1 |
| 8 | 4 | 2 | 2 | 1 |
| 9 | 4 | 3 | 2 | 1 |
| 10 | 5 | 3 | 2 | 2 |
#running the aggregation from sql
#trying the same aggregation udf with different params
$ aql -o json
Aerospike Query
Copyright 2013 Aerospike. All rights reserved.
Connected to 127.0.0.1:3000
aql> set LUA_USERPATH ‘/home/sunil’
aql> register module ‘/home/sunil/distinct_with_args.lua’
OK, 1 module added.
aql> aggregate distinct_with_args.distinct(‘bin1’, ‘bin2’, ‘bin3’, ‘bin4’, 0) on test.demo where bin0 between 0 and 1000
[
{
“distinct”: Map(“count1”->501, “count2”->334, “count3”->251),
}
]
aql> aggregate distinct_with_args.distinct(‘bin1’, ‘bin2’, ‘bin3’, ‘bin4’, 199) on test.demo where bin0 between 0 and 1000
[
{
“distinct”: Map(“count1”->4, “count2”->3, “count3”->3),
}
]
<>
aql> aggregate distinct_with_args.distinct(‘bin2’, ‘bin3’, ‘bin4’, ‘bin1’, 0) on test.demo where bin0 between 0 and 1000
[
{
“distinct”: Map(“count1”->334, “count2”->251, “count3”->201),
}
]
aql> aggregate distinct_with_args.distinct(‘bin2’, ‘bin3’, ‘bin4’, ‘bin1’, 499) on test.demo where bin0 between 0 and 1000
[
{
“distinct”: Map(“count1”->2, “count2”->2, “count3”->2),
}
]
aql> aggregate distinct_with_args.distinct(‘bin2’, ‘bin3’, ‘bin4’, ‘bin1’, 500) on test.demo where bin0 between 0 and 1000
[
{
“distinct”: Map(“count1”->1, “count2”->1, “count3”->1),
}
]