What is the detail api for java client

While reading the official document in https://aerospike.com/docs/client/java/usage/udf/register.html like this

-- Validate value before writing.
function writeWithValidation(r,name,value)
   if (value >= 1 and value <= 10) then
     if not aerospike:exists(r) then 
       aerospike:create(r)
     end
     r[name] = value
     aerospike:update(r)
   else
       error("1000:Invalid value") 
   end
end

-- Set a particular bin only if record does not already exist.
function writeUnique(r,name,value)
   if not aerospike:exists(r) then 
       aerospike:create(r) 
       r[name] = value
       aerospike:update(r)
   end
end

I found that the example has a variable aerospike (or maybe some else) and the function create() ,exists() in the example that I did not know. I tried to search some information on the Java client, but I did not find any useful infomation.Where is the detail description for it.I wonder to know the available function in it so that I can use it.If you know how or where to get it and reply to me,I really appreciate it.

This isn’t Java, it is Lua. The example is demonstrating uploading Lua scripts as a User Defined Function (UDF) on the server. UDFs are an atomic set of operations to be applied to a single record.

See UDF Guide.

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)),
        }

‘aerospike’ is a module that is available to UDFs.

See the documentation for how to use it and other modules that are available: https://www.aerospike.com/docs/udf/api_reference.html

Thank so much.I already find the answer from the official document.The variable aerospike is the inbuilt module,which is available for lua in aerospike.Same is for logging.aerospike link I correct my lua and share here,maybe helpful for others

local function groupByUserId(users, record)
    local userId = record['userId'];
    local user = users[userId] or map { userId = userId, count = 0, amount = 0, average = 0 }
    local count = user.count
    count = count + 1;
    user.count = count
    local recordAmount = record.amount
    local flag = not (recordAmount == nil);
    local amount = user.amount
    if flag then
        user.amount = amount + recordAmount
    end
    users[userId] = user
    return users
end

local function reduceByUserId(current, another)
    local function mergeByUserId(firstMap, secondMap)
        local count = firstMap['count'] + secondMap['count'];
        local amount = firstMap['amount'] + secondMap['amount'];
        local average = 0;
        local flag = not (count == 0)
        if flag then
            average = amount / count
        end
        return map { count = count, amount = amount, average = average, userId = firstMap['userId'] }
    end

    return map.merge(current, another, mergeByUserId)
end

function calAverage(stream)
    return stream:aggregate(map(), groupByUserId):reduce(reduceByUserId)
end

Again thank you sincerely

© 2015 Copyright Aerospike, Inc. | All rights reserved. Creators of the Aerospike Database.