How to get records set with ttl -1 in aerosics?

I have so many records in aerospace, I want to get records whose ttl is -1, please provide a solution

+2


source to share


1 answer


To clarify, setting the TTL to -1 on the client means it does not expire (equivalent to default-ttl

0 on the server aerospike.conf file , setting the TTL to 0 on the client means inheriting the default -ttl for that namespace.

With predicate filtering:

If you're using Java , C , C #, and Go, the easiest way to identify records with void time 0 is to use a predicate filter .

In a Java application:

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

      

Without filtering predicates :

With other clients that don't have predicate filtering yet (Python, PHP, etc.), you would do it all via stream UDF . The filtering logic would have to be inside the 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

      

In 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

      

Alternatively, you can start a UDF stream from the client. The following example is for a Python client:

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

      

+2


source







All Articles