如何在aerospike中获取ttl为-1的记录集?

时间:2017-07-17 07:30:29

标签: lua user-defined-functions aerospike ttl aql

我在airospike中有这么多记录,我想获取ttl为-1的记录请提供解决方案

1 个答案:

答案 0 :(得分:2)

只是澄清一点,在客户端设置TTL of -1意味着永不过期(相当于服务器default-ttl文件中的aerospike.conf为0),而在客户端中将TTL设置为0意味着继承此命名空间的default-ttl

使用谓词过滤:

如果您正在使用JavaCC#Go客户端,则最简单的方法是识别void time为0的记录使用predicate filter

在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等),您可以通过stream 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()