使用Aerospike Python Client udf从所有记录的集合中删除多个bins

如何使用Aerospike Python Client udf从集合中的所有记录中删除多个bins?我尝试将一个bin传递给udf并使用扫描来从所有记录中删除bin,但是如预期的那样效率非常低下。我还尝试在python中创建bin列表并将列表传递给UDF。以下是参考代码:

假设我有2000个记录和200个bin,名称为“1”,“2”,“3”等。我想从'1'到'99'中删除bins。使用的命名空间是testns,使用的集合是udfBins。testUdf.lua是包含udf的lua文件,my_udf是lua函数名称。

test.py

    scan = client.scan("testns", "udfBins")
    bins = [str(i) for i in range(1,366)]
    # for i in range(1,100):
    scan.apply("testUdf", "my_udf", [bins])
    job_id = scan.execute_background()
    while True:
        response = client.job_info(job_id, aerospike.JOB_SCAN)
        if response["status"] != aerospike.JOB_STATUS_INPROGRESS:
            break

    print("job done")

testUdf.lua

function my_udf(rec, bins)

    info(bins)
    for bin in python.iter(bins)
    do
        rec[bin] = nil
    end
    aerospike:update(rec)
end

上述代码无法工作,我无法找到原因和解决问题的正确方法。非常感谢任何帮助。

在此先感谢您的帮助。

点赞
用户1917187
用户1917187

这是一个有点棘手的问题。我们需要将一个数组作为参数从python传递给lua函数。以下是我用来使其工作的代码的相关部分:

1-将数组作为字符串传递,如下所示:

bins = '{"1","2"}'
# print(bins)
self.client.scan_apply("test", "users", "testUdf", "my_udf", [bins])

注意:在scan_apply(函数名有下划线,参数作为列表传递,在这里只有一个参数——在lua中将其转换为表类型并进行迭代的字符串bins。

然后,在testUdf.lua中执行以下操作:

function my_udf(rec, bins_list)
    bins_list = load("return "..bins_list)()
    for i,bin in ipairs(bins_list)
    do
        -- debug("bins_list_item: "..bin)
        rec[bin] = nil
    end
    aerospike:update(rec)
end

我使用调试级别的日志记录(您使用了信息)来检查lua代码的运行情况。这对我有效。

我创建了3个记录,其中bins为“1”,“2”和“3”,然后使用上述方式在扫描UDF中删除了bins“1”和“2”。

这是运行扫描后一个记录的示例输出:

{'3': 1, '1': 1, '2': 1}  <-- initial bins, 3 records, same bins, same values
{"1","2"}  <--list that I passed as a string for setting these bins to nil
{'3': 1}  <-- final bins

我使用AQL进行了检查,所有3个记录的bins“1”和“2”都已被删除。

aql> select * from test.users
+---+
| 3 |
+---+
| 1 |
| 1 |
| 1 |
+---+
3 rows in set (0.123 secs)

这是进一步阅读的好链接:https://discuss.aerospike.com/t/what-is-the-syntax-to-pass-2d-array-values-to-the-record-udf-using-aql/4378

2021-01-13 02:27:44