sheldonzy
sheldonzy

Reputation: 5941

Aerospike count all filter by TTL

I'm trying to perform the following query:

SELECT COUNT(*) FROM ns123.foo WHERE ttl < 60 * 60 * 24

I found the lua scripts from Aerospike AQL count(*) SQL analogue script to perform the COUNT(*)

Using Python with the above LUA scripts, I tried to apply the UDF with a read policy:

        client.udf_put('aggr_functions.lua')
        query = client.query('ns123', 'foo')
        policy = {
            'expressions':
                exp.LT(exp.TTL(), 60 * 60 * 24).compile()
        }
        query.apply('aggr_functions', 'count_star', [])
        records = query.results(policy)
        print(records)

I'm getting thrown with:

Traceback (most recent call last):
  ...
    records = query.results(policy)
exception.UnsupportedFeature: (16, 'AEROSPIKE_ERR_UNSUPPORTED_FEATURE', 'src/main/aerospike/aerospike_query.c', 348, False)

Using Aerospike 6.1.x for both Python3.8 lib and server.

Upvotes: 3

Views: 192

Answers (2)

sheldonzy
sheldonzy

Reputation: 5941

As stated above, in aggregations, filters should be in the lua script. The script I'm using:

function counts(s)
    function mapper(rec)
        local ttl = record.ttl(rec);
        local ttl_threshold = 86400;
        if ttl <= ttl_threshold and ttl > 0 then
            return 1
        else
            return 0
        end
    end
    local function reducer(v1, v2)
        return v1 + v2
    end
    return s : map(mapper) : reduce(reducer)
end

This corresponding to the query

SELECT COUNT(*) FROM ns123.foo WHERE ttl < 60 * 60 * 24 AND ttl > 0

Upvotes: 0

pgupta
pgupta

Reputation: 5415

Aggregations don't support filter expressions. But you can write the filter code in lua add the filter function in the lua aggregation module itself. (I have a code example for using filters with aggregations in lua posted here: https://discuss.aerospike.com/t/record-manipulation-with-more-than-one-filter-lua/3637 )

Upvotes: 5

Related Questions