如何在 Aerospike 中获取 TTL 为 -1 的记录集?

我在 Aerospike 中有很多记录,我想获取 TTL 为 -1 的记录,请提供解决方案。

点赞
用户582436
用户582436

通过谓词过滤器:

如果您正在使用 JavaCC#Go 客户端,识别具有 虚空时间 为 0 的记录最简单的方法是使用 谓词过滤器

在 Java 应用程序中:

Statement stmt = new Statement();
stmt.setNamespace(params.namespace);
stmt.setSetName(params.set);
stmt.setPredExp(
  PredExp.recVoidTime(),
  PredExp.integerValue(0),
  PredExp.integerEqual()
  );

RecordSet rs = client.query(null, stmt);

没有谓词过滤器:

对于其他尚未具备谓词过滤功能(Python、PHP等)的客户端,您可以通过 流 UDF 来执行所有操作。过滤逻辑必须在 UDF 中运行。

ttl.lua

local function filter_ttl_zero(rec)
  local rec_ttl = record.ttl(rec)
  if rec_ttl == 0 then
    return true
  end
  return false
end

local function map_record(rec)
  local ret = map()
  for i, bin_name in ipairs(record.bin_names(rec)) do
    ret[bin_name] = rec[bin_name]
  end
  return ret
end

function get_zero_ttl_recs(stream)
  return stream : filter(filter_ttl_zero) : map(map_record)
end

AQL 中:

$ aql
Aerospike Query Client
Version 3.12.0
C Client Version 4.1.4
Copyright 2012-2017 Aerospike. All rights reserved.
aql> register module './ttl.lua'
OK, 1 module added.

aql> AGGREGATE ttl.get_zero_ttl_recs() on test.foo

或者,您可以从客户端运行流 UDF。以下示例适用于 Python 客户端:

import aerospike
import pprint

config = {'hosts': [('127.0.0.1', 3000)],
          'lua': {'system_path':'/usr/local/aerospike/lua/',
                  'user_path':'/usr/local/aerospike/usr-lua/'}}
client = aerospike.client(config).connect()

pp = pprint.PrettyPrinter(indent=2)
query = client.query('test', 'foo')
query.apply('ttl', 'get_zero_ttl_recs')
records = query.results()
# we expect a dict (map) whose keys are bin names
# each with the associated bin value
pp.pprint(records)
client.close()
2017-07-17 15:43:10