使用AerospikePythonClientudf从Aerospike中一个集合的所有记录中删除多个bin

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

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

测试文件

    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")

测试Udf文件

function my_udf(rec, bins)

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

上面的代码不起作用,我无法弄清楚原因和解决手头问题的正确方法。任何帮助都受到高度赞赏。

非常感谢提前

回答

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

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

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

注意:在 scan_apply (函数名有一个下划线,args 作为列表传递,这里只有一个 arg - 在 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 代码在做什么。这对我有用。我用垃圾箱“1”、“2”和“3”创建了 3 个记录,然后使用上面的扫描 udf 删除了垃圾箱“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 条记录都删除了它们的 bin“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

  • This is awesome. If one has to delete multiple random bins, then this is probably the best solution I have seen. However, if all the bins in a specific range have to be deleted, passing two arguments - starting and ending indices - to the Lua function and then iterating over them to delete the bins will probably be a tad faster.

以上是使用AerospikePythonClientudf从Aerospike中一个集合的所有记录中删除多个bin的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>