I am very new to lua and aerospike. I would like to develop a UDF that runs on aerospike but I can't find a way to do this with option to debug.
I tried to install eclipse LDT, but it can't seem to find aerospike requirement.
How can I do this?
I tried something simple: load all records of a table and print.
function test(rec)
print(rec['bin1'])
end
of course I created the table and inserted records.
Thanks
On the aerospike.com website there is a UDF Developer Guide, and specifically for this case look at the "Developing Record UDFs" article. To log all the records in a set (table) you would apply a record UDF to a scan. For an example of doing this with the Python client see the aerospike.Client.scan_apply() method.
You will want to set up a log file for the UDF operations for debugging, and in your example to log the records in the set. In your /etc/aerospike/aerospike.conf add a logging section, the restart the service:
logging {
file /var/log/aerospike/udf.log {
context any warning
context ldt info
context udf debug
context query debug
}
}
You can now create a Lua module with a function that uses the info() method, as described in the Lua UDF - Best Practices article.
I created a module called sample.lua which has a record UDF called show_set:
function show_set(rec, ns, set)
out = ''
bins = record.bin_names(rec)
for i, bin_name in ipairs(bins) do
out = out .. "'" .. tostring(bin_name) .. "'"
out = out .. '=>' .. tostring(rec[bin_name]) .. ","
end
info("show_set(%s.%s, %s): %s", ns, set, tostring(record.key(rec)), out)
end
I loaded it into the server with a simple Python script that also applies the record UDF to the scan:
import aerospike
from aerospike.exception import *
import time
config = { 'hosts': [ ('192.168.119.3', 3000)]}
client = aerospike.client(config).connect()
try:
client.udf_put('sample.lua')
time.sleep(2)
except AerospikeError as e:
print("Error: {0} [{1}]".format(e.msg, e.code))
client.put(('test','demo','key1'), {'id':1,'a':1},
policy={'key':aerospike.POLICY_KEY_SEND})
client.put(('test','demo','key2'), {'id':2,'b':2},
policy={'key':aerospike.POLICY_KEY_SEND})
client.put(('test','demo','key3'), {'id':3,'c':3},
policy={'key':aerospike.POLICY_KEY_SEND})
try:
scan_id = client.scan_apply('test', 'demo', 'sample', 'show_set', ['test',
'demo'])
while True:
response = client.scan_info(scan_id)
if (response['status'] == aerospike.SCAN_STATUS_COMPLETED) or \
response['status'] == aerospike.SCAN_STATUS_ABORTED:
break
if response['status'] == aerospike.SCAN_STATUS_COMPLETED:
print("Background scan successful")
print("Progess percentage : ", response['progress_pct'])
print("Number of scanned records : ", response['records_scanned'])
print("Background scan status : ", "SCAN_STATUS_COMPLETED")
else:
print("Scan_apply failed")
except AerospikeError as e:
print("Error: {0} [{1}]".format(e.msg, e.code))
client.close()
I ran the script and tail -f /var/log/aerospike/udf.log | grep show_set:
May 14 2015 21:01:47 GMT: INFO (udf): ([C]::-1) show_set(test.demo, key1): 'a'=>1,'id'=>1, May 14 2015 21:01:47 GMT: INFO (udf): ([C]::-1) show_set(test.demo, key3): 'c'=>3,'id'=>3, May 14 2015 21:01:47 GMT: INFO (udf): ([C]::-1) show_set(test.demo, key2): 'b'=>2,'id'=>2,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With