Aerospike set TTL for all filtered records with Python

Question:

I want to set the TTL to -1 (no ttl) for all records that match some filter, using Python.

I successfully set the TTL for all the records with the following:

import time

from aerospike_helpers import expressions as exp
from aerospike_helpers.operations import operations

import aerospike

if __name__ == '__main__':
    config = {
        'hosts': [('127.0.0.1', 3000)]
    }

    client = aerospike.client(config).connect()

    ops = [operations.touch(-1)]

    query = client.query('ns123', 'foo')
    query.add_ops(ops)
    expr = exp.GE(exp.LastUpdateTime(), 0).compile()
    policy = {}
    job_id = query.execute_background(policy)
    while True:
        response = client.job_info(job_id, aerospike.JOB_SCAN)
        print(response)
        if response["status"] != aerospike.JOB_STATUS_INPROGRESS:
            break
        time.sleep(0.25)

    client.close()

It works, but it produces a deprecationWarning:
TTL should be specified in the meta dictionary for operate. the documentation states:

ttl (int): Deprecated. The ttl that should be set for the record.
    This should be set in the metadata passed to the operate or
    operate_ordered methods.

But how I can do it with operate or operate_oredered methods? seems like both require keys, and I’m doing this query on all records.

Asked By: sheldonzy

||

Answers:

This deprecation requires update to Python client which should be forthcoming.

Answered By: pgupta

As @pgupta said, the touch TTL is deprecated, but for now I ended up using it anyway. Seems to work fine.
Sharing the script I’m using. You can add filter expressions in the policy of the query.

import time

from aerospike_helpers.operations import operations

import aerospike

if __name__ == '__main__':
    config = {
        'hosts': [('127.0.0.1', 3000)]
    }

    client = aerospike.client(config).connect()

    zero_ttl_operation = [operations.touch(-1)]
    query = client.query(namespace, set_name)
    query.add_ops(zero_ttl_operation)
    policy = {}
    job = query.execute_background(policy)
    while True:
        response = client.job_info(job, aerospike.JOB_SCAN)
        if response['status'] != aerospike.JOB_STATUS_INPROGRESS:
            break
        time.sleep(0.5)
Answered By: sheldonzy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.