【问题标题】:How can I handle with duplicate items in Aerospike script如何处理 Aerospike 脚本中的重复项
【发布时间】:2018-10-20 11:36:41
【问题描述】:

我有脚本,它工作正常,但我必须更新它。脚本现在添加项目而不检查是否存在。

    function put_page(rec, id, val)
        local l = rec['h']
        if l==nil  then l = list() rec['id'] = id  end
        list.append(l, val)
        rec['h'] = l
        if aerospike:exists(rec) then aerospike:update(rec) else aerospike:create(rec) end 
    end

我尝试在 list.iterator(l) 中使用 for value 迭代列表,并在 value~=val 时附加项目,但它没有用。 函数中的 ID 是 solr document_id,val 是 users_id。我从 aerospike 得到示例对象: (('contextChannel', 'ContextChannel', None, bytearray(b'E\xfb\xa3\xd0\r\xd6\r\J@f\xa8\xf6>y!\xd18= \x9b')), {'ttl': 2592000, 'gen': 8}, {'id': 'ALKSD4EW', 'h': []})

更新 我尝试了不同的变体,这很有效:

    function put_page(rec, id, val)
        local l = rec['h']
        local count = 0
        if l==nil  then l = list() rec['id'] = id  end
        for value in list.iterator(l) do
            if (value ~= val) then count = count + 1 end
        end
        if (list.size(l) == count) then list.append(l, val) end
        rec['h'] = l
        if aerospike:exists(rec) then aerospike:update(rec) else aerospike:create(rec) end
    end

【问题讨论】:

  • 您的列表看起来如何?您能否向我们展示您的列表结构示例以及如何在列表中添加新项目?
  • 用python脚本添加:aero_client_insert.apply(key, "channels_utils", "put_page", [page_id, Audience_id])
  • lua 列表是您的示例中带有键、值对 Table = {key1 = val1, key2 = val2 ... etc } 的表。目前还不清楚您的表格是什么样的,以及您是如何读取值的。
  • 我有 python 脚本将 [page_id, audince_id] 放入 lua 函数和 lua 中的脚本,它的代码有问题。就这样。 rec['h'] 列表,我希望其中没有重复项。

标签: lua aerospike


【解决方案1】:

不要为作为List API 操作存在的东西创建UDF。 UDF 的性能和规模都不会那么好。

您可以在没有 UDF 的情况下执行此操作。这是一个使用Python client 做同样事情的例子。

from aerospike_helpers.operations import list_operations as lh
from aerospike_helpers.operations import operations as oh

list_policy = {
    "list_order": aerospike.LIST_UNORDERED,
    "write_flags": (aerospike.LIST_WRITE_ADD_UNIQUE |
                    aerospike.LIST_WRITE_NO_FAIL)
}
ops = [
    oh.write('id', id),
    lh.list_append('h', val, list_policy)
]
client.operate(key, ops)

我在rbotzer/aerospike-cdt-examples 有一个类似的例子。

【讨论】:

    猜你喜欢
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 2013-08-30
    • 2014-06-03
    • 2014-06-08
    • 2017-02-18
    相关资源
    最近更新 更多