How to get key in query

key
python
query

#1

Hi I’m trying to get records with key in a query. By doing the following I did not get the keys, they are coming as None(python client is used).

q = c.query(namespace, set)
q.where(aerospike.predicates.between('bin', 1, 1000))
q.results({'key': aerospike.POLICY_KEY_SEND})

On the page https://pythonhosted.org/aerospike/query.html#query-policies there is only one “timeout” policy mentioned, does this mean the key policy aerospike.POLICY_KEY_SEND is not supported in query? Is there other way to get keys in queries?

Thanks


#2

aerospike.POLICY_KEY_SEND is part of the write policy you can apply to the aerospike.Client.put method. If the record had a key saved with it you’ll get it back on a query or scan, if it did not then you get None.

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

def show_key((k,m,b)):
    print(k)

client = aerospike.client({ 'hosts': [('192.168.119.3', 3000)]}).connect()
#client.index_integer_create('test', 'demo', 'i', 'test_demo_i_idx')

for i in xrange(100):
    client.put(('test', 'demo', i), {'i': i, 's': 's' + str(i)},
               policy={'key': aerospike.POLICY_KEY_SEND})

query = client.query('test','demo')
query.where(p.between('i', 10, 20))
query.foreach(show_key)
client.close()

Will output:

(('test''test', , 'demo', 11'd, emo'bytearray(b'\xa6\xddU\xc8\x85\x9eT\x01\xd2\x88\x0by\xfb\x9f\t\x1c\xc6\xda\xc6\xfb'))
, 10, bytearray(b'/\xc0\x95\x166\xffR\x1e\x81\xb8g/\xcb[\x7f\xb55\rO\xaf'))
('test', 'demo', ('test', 'demo', 14, bytearray(b'\xdc\xef\x91\xefF\x98e\n\x1d\x07\xdb?\xbc\xbb\x87\x01\x17\x861B'))
12, bytearray(b'"\x80\xd0)Y\t\x07\x99y\xc8\xecF\xa7\'\xfd+\xc2R\xcc\x9b'))
('test', 'demo', 15, bytearray(b'\xc7\t\xac\xef\xba\xb7\xf8\xd9QWD&\xe4\x0c\xf9\x04\x1a4?\xc0'))
('test', 'demo', 17, ('test', 'demo'bytearray(b'C\x0f_t\x9e\x13(\x88\xf2\x0e\x7fB\x08\x1ch\x99\x10\xd6\xeb|'))
('test', 'demo', 20, 13, , bytearray(b'\xd4N\xd4\x98\xbb8\x8c\xc7\xca\x8aSP\xeb\xf5\xd2\xa2~\xf2\xfa\x15'))
bytearray(b'"\x1aa\xee\xc7?\x14\x12\xb00\xe3\xb3\xdf\x11\xb5\xccdJ\xe8s'))
('test', 'demo', 16, bytearray(b'W7l\xe4\xdaJYz\x9c$\x0b$\x83\xf4,\x0f\x9d\x9c\x93\xee'))
('test', 'demo', 18, bytearray(b'\x99`qM\xd0\xfc\x1c\x99\xdb\xfb\x08Q\xde\xc8\xc4\x90\xa7\x1d\x7f\xad'))
('test', 'demo', 19, bytearray(b"\xb1[/\xd2%8\x99$\nd\x93\xed\xb1\r\x9e\x89\xe0\'i\xf8"))