Can I execute record UDF using secondary indexes..?

Hi all, I am beginner in aerospike technology. My requirement is to execute Record UDF using secondary index value so is it possible to do this…?

Record UDF executes on a single record. What is your requirement in combination with a Secondary Index?

Requirement is i want to updates all device names where device_id = ‘101’. Here i created secondary on device_id.

Yes, you can apply a record UDF to all the records matched by a scan or query. It doesn’t aggregate results like a stream UDF, and doesn’t return a result, but it would modify the bins.

Typically run through AQL, since you only need to run it once.

$ aql
Aerospike Query Client
Version 3.12.0
C Client Version 4.1.4
Copyright 2012-2017 Aerospike. All rights reserved.

aql> EXECUTE myudfs.some_func() ON test.demo WHERE device_id='101'

This assumes you have a numeric index over the bin device_id.

You can also do this from any language client. Which client are you using?

For Java Client, you can look up this API call:

 ExecuteTask execute(WritePolicy policy,
                      Statement statement,
                      String packageName,
                      String functionName,
                      Value... functionArgs)
                        throws AerospikeException
    Apply user defined function on records that match the statement filter. Records are not returned to the client. This asynchronous server call will return before command is complete. The user can optionally wait for command completion by using the returned ExecuteTask instance.
    Parameters:
    policy - write configuration parameters, pass in null for defaults
    statement - record filter
    packageName - server package where user defined function resides
    functionName - function name
    functionArgs - to pass to function name, if any
    Throws:
    AerospikeException - if command fails

Thanx for the reply, I tried the same query but it doesn’t work.

I am using community edition is this option available only in enterprise edition…?

Apparently it can’t be invoked from AQL.

From the AQL help:

aql> help
Aerospike Query Client
Version 3.12.0
C Client Version 4.1.4
Copyright 2012-2016 Aerospike. All rights reserved.

COMMANDS
:
    INVOKING UDFS
        EXECUTE <module>.<function>(<args>) ON <ns>[.<set>]
        EXECUTE <module>.<function>(<args>) ON <ns>[.<set>] WHERE PK = <key>  

So, you can run this from a Python script, or from another language client.

The Python client, for example has aerospike.Client.query_apply().

from __future__ import print_function
import aerospike
from aerospike import predicates as p
from aerospike.exception import AerospikeError
import time

config = {'hosts': [ ('127.0.0.1', 3000)]}
client = aerospike.client(config).connect()
try:
    pred = p.equals('device_id', '101')
    job_id = client.query_apply('test', 'dev_details', pred, 'myudf', 'lua_fun')
    while True:
        time.sleep(0.25)
        response = client.job_info(job_id, aerospike.JOB_SCAN)
        if response['status'] == aerospike.JOB_STATUS_COMPLETED:
            break
    print("Job ", str(job_id), " completed")
    print("Progress percentage : ", response['progress_pct'])
    print("Number of scanned records : ", response['records_read'])
except AerospikeError as e:
    print("Error: {0} [{1}]".format(e.msg, e.code))
client.close()

AQL is not a full blown utility with every API feature. Its good for basic data exploration and some management. Start testing with a client application - that will be more efficient.

1 Like