【问题标题】:Deleting multiple bins from all the records of a set in Aerospike using Aerospike Python Client udf使用 Aerospike Python Client udf 从 Aerospike 中的一组记录中删除多个 bin
【发布时间】:2021-04-16 01:44:21
【问题描述】:

如何使用 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 函数名。

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

上面的代码不起作用,我无法弄清楚原因和解决手头问题的正确方法。非常感谢任何帮助。

提前非常感谢

【问题讨论】:

    标签: python lua user-defined-functions aerospike


    【解决方案1】:

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

    1 - 像这样将数组作为字符串传递:

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

    注意:在 scan_apply 中(函数名有下划线,args 作为列表传递,这里只有一个 arg - 我们在 lua 中转换为表类型并迭代的字符串 bin。

    然后在你的 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 条记录,其中包含箱“1”、“2”和“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

    【讨论】:

    • 这太棒了。如果必须删除多个随机箱,那么这可能是我见过的最好的解决方案。但是,如果必须删除特定范围内的所有 bin,则将两个参数(开始和结束索引)传递给 Lua 函数,然后迭代它们以删除 bin 可能会快一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 2016-04-07
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多