An easy way to do an aggregation, would be to do a batch get of the composite keys. Say you want this key to be “key”. To split the load up evenly, you could split this into key[1-100] - effectively splitting the load across 100 keys. Then, when you go to retrieve the keys, simply construct an array of keys to get - and perform a batch get.
ex…
Key keys = new Key[100];
for (int i = 0; i < 100; i++) {
keys[i] = new Key(“test”, “myset”, “mybin”);
}
Record records = client.Get(policy, keys);
Then you can loop through the records…
int sum=0;
for (int i = 0; i < 100; i++) {
if(records[i] != null){
sum+=records[i].getInt(“mybin”);
}
}
The UDF way may actually be faster though… I’m not sure if you can use batch get with an aggregation, or if you’d have to use a statement. Of course - you’d want to experiment and see what yields best results.
http://www.aerospike.com/docs/client/java/usage/aggregate
It may look something like this (@pgupta will correct me if i am wrong… hopefully.)
local function one(rec)
return rec[mybin]
end
local function add(a, b)
return a + b
end
function sum(stream)
return stream : map(one) : reduce(add);
end
and invocation from the program would look something like this:
Statement stmt = new Statement();
stmt.setnamespace=“foo”;
stmt.setSetname=“bar”;
stmt.setbin=“mybin”;
stmt.setFilters(Filter.range(“mybin”, Long.MIN_VALUE, Long.MAX_VALUE));
ResultSet rs = client.QueryAggregate(null, stmt, “myudf”, “sum”, Value.Get(“mybin”));
if (rs != null && rs.Next()) {
Console.WriteLine("Sum = " + rs.Object);
}