Can you limit the number of returned records?

I am using the python client library. When I have such statement:

query = client.query(‘test’, ‘raw_data’)

How can I put a limit to the number of records returned ? My aim here is to create batches of data (depending on number of records returned) to handle my local threads, which are responsible to further process the data.

Is there such a thing as “.limit(100)” ?

Thank you.

1 Like

A query in the Python client is initiated with either foreach() or results(). You can use foreach to do the limit by having a counter that stops the query when it reaches the desired number.

For example:

from __future__ import print_function
import aerospike
from aerospike import predicates as p

config = { 'hosts': [ ('192.168.119.3',3000)]}
client = aerospike.client(config).connect()

def limit(lim, result):
    c = [0]
    def key_add((key, metadata, bins)):
        if c[0] < lim:
            result.append(key)
            c[0] = c[0] + 1
        else:
            return False
    return key_add

query = client.query('test','aggr')
query.where(p.between('day', 1, 4))
keys = []
query.foreach(limit(100, keys))
print(len(keys))
client.close()
1 Like

Hi casra,

Just to add to Ronen.

“Limit” feature in query is not yet implemented at the server side.

The queries in the example code above will be reported as failure at the server side. All query stats will be updated accordingly. You can think of this as a scenario where client is closing its connection after receiving N number of records. Since server is not aware of that it will treat this as a connection failure.

Thanks

Hey @pratyyy

Do you guys know when the limit feature would be implemented?

Thanks,