在 aerospike.com 网站上有一个UDF Developer Guide,专门针对这种情况,请查看“Developing Record UDFs”文章。要记录集合(table)中的所有记录,您可以将记录 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
}
}
您现在可以使用 info() 方法的函数创建 Lua 模块,如 Lua UDF - Best Practices 文章中所述。
我创建了一个名为 sample.lua 的模块,它有一个名为 show_set 的记录 UDF:
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
我使用一个简单的 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()
我运行了脚本并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,