我对lua和aerospike很新.我想开发一个在aerospike上运行的UDF,但我无法通过调试选项找到一种方法.
我试图安装eclipse LDT,但它似乎无法找到aerospike要求.
我怎样才能做到这一点?
我尝试了一些简单的事情:加载表格的所有记录并打印.
function test(rec)
print(rec['bin1'])
end
Run Code Online (Sandbox Code Playgroud)
当然我创建了表并插入了记录.
谢谢
在aerospike.com网站上有一个UDF开发人员指南,特别针对本案例,请查看" 开发记录UDF "一文.要记录集(表)中的所有记录,您可以将记录UDF应用于扫描.有关使用Python客户端执行此操作的示例,请参阅aerospike.Client.scan_apply()方法.
您将需要为UDF操作设置日志文件以进行调试,并在您的示例中记录集中的记录.在你的/etc/aerospike/aerospike.conf中添加一个logging部分,重启服务:
logging {
file /var/log/aerospike/udf.log {
context any warning
context ldt info
context udf debug
context query debug
}
}
Run Code Online (Sandbox Code Playgroud)
您现在可以使用Lua UDF - Best Practices文章中info()描述的函数创建一个带有使用该方法的函数的Lua模块.
我创建了一个名为的模块sample.lua,它有一个名为UDF的记录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
Run Code Online (Sandbox Code Playgroud)
我用一个简单的Python脚本将它加载到服务器中,该脚本也将记录UDF应用于扫描:
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()
Run Code Online (Sandbox Code Playgroud)
我运行了脚本并且tail -f /var/log/aerospike/udf.log | grep show_set:
2015年5月14日21:01:47 GMT:INFO(udf):( [C] :: - 1)show_set(test.demo,key1):'a'=> 1,'id'=> 1,2015年5月14日21:01:47 GMT:INFO(udf):( [C] :: - 1)show_set(test.demo,key3):'c'=> 3,'id'=> 3,2015年5月14日21:01 :47 GMT:INFO(udf):( [C] :: - 1)show_set(test.demo,key2):'b'=> 2,'id'=> 2,