2017-07-17 76 views
2

我在塞這麼多的紀錄,我希望獲取其TTL值爲-1請提供解決方案如何獲取記錄集中的ttl爲-1的aerospike?

+1

好的,但您想對所有這些記錄做什麼?你想閱讀他們的內容?你想刪除它們嗎?你想重置他們的TTL到指定的時間? –

+0

這與您在討論中提出的問題不同:https://discuss.aerospike.com/t/how-to-get-records-whose-ttl-is-1/4400/5 - 您打開重複的問題,但給予不同信息?那是什麼? –

+0

爲了保存我在回答丟失問題時所做的工作,我創建了一個重複問題(https://stackoverflow.com/questions/45193957/how-to-modify-the-ttl-of-all-records-set -with-A-TTL-的-1合塞)。現在我需要編輯這個問題,因爲你反駁了你自己。這是無禮的 - 你要求某人解決你的問題,然後在發表一個寫得不好的問題後,你就改變主意。 –

回答

2

只是爲了澄清記錄,在客戶端設置TTL of -1意味着永不過期(相當於0的default-ttl在服務器的aerospike.conf文件中),同時在客戶端中設置TTL爲0意味着繼承此名稱空間的默認-ttl。

謂語過濾:

如果您使用的JavaCC#Go客戶最簡單的方法,以確定爲0的void time記錄將使用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()