Thank you very much for your reply.How the variable aerospike
pass to the function?I didn’t pass any aerospike
to it.Just like the function writeUnique
,I didn’t pass the arg r
to the function,but the result is correct.
for example
Object execute = client.execute(null, key, "record_example", "writeUnique", Value.get(bin.name), bin.value);
so,how it work.
Another question abount the stream.For example,I wonder calculate every user’s the average amount in 5min,I write my lua script like this:
local function mapTrade(record)
local itemMap = {}
itemMap['count'] = 1
itemMap['amount'] = record['amount']
return itemMap
end
local function reduceTrade(current, next)
local targetMap = {}
targetMap['count'] = current['count'] + next['count']
targetMap['amount'] = current['amount'] + next['amount']
return targetMap
end
local function writeAverage(r, name, value)
if not aerospike:exists(r) then
aerospike:create(r)
r[name .. '_average5min'] = value
aerospike:update(r)
end
end
function calAverage(r, stream, name)
local result = stream:map(mapTrade):reduce(reduceTrade)
local average = 0
if not result['count'] == 0 then
average = result['amount'] / result['count']
end
writeAverage(r, name, average)
end
the java
Statement statement = new Statement();
statement.setNamespace("test");
statement.setSetName("trade");
ong millis = System.currentTimeMillis();
statement.setFilter(Filter.range("tradeTime", millis - 5 * 60 * 1000, millis));
LuaConfig.SourceDirectory = "/as/udf";
client.queryAggregate(null,statement,"average5min","calAverage");
I’m not sure whether it’s right,how the pass the arg in java for this case,because I don’t know how the arg like aerospike,stream,record …etc pass to the function.They seem to be inbuilt .If I’m right ,I don’t need to pass any avariable,but how the function know which is for stream and another for record.Forgive my poor English,if you have any question,just let me know.Waiting for your reply,Thanks.
every record struct:
new Bin[]{
new Bin("userId", "userId" + i % num),
new Bin("amount", new BigDecimal("" + random.nextDouble() * 600)
.setScale(2, RoundingMode.UP).doubleValue()),
new Bin("city", "city" + i % 10),
new Bin("tradeTime", getDateTimeIn(Calendar.MINUTE, 5)),
}