0

I have a redis instance that stores data as hash. the hash key looks something like "o:t:23:45". I'm using redis-py and I am able to get list of keys.

for key in rc.scan_iter():
    print(key)

b'o:t:49:15' b'o:t:50:156' b'o:t:51:159' b'o:t:52:1593' b'o:t:53:1591'

If I perform rc.hgetall(b'o:t:53:1591'), I am getting proper values.

However, when I do the following:

for key in rc.scan_iter():
    rc.hgetall(key)

I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/local/lib/python3.6/site-packages/redis/client.py", line 2717, in hgetall
    return self.execute_command('HGETALL', name)
  File "/usr/local/lib/python3.6/site-packages/redis/client.py", line 839, in execute_command
    return self.parse_response(conn, command_name, **options)
  File "/usr/local/lib/python3.6/site-packages/redis/client.py", line 853, in parse_response
    response = connection.read_response()
  File "/usr/local/lib/python3.6/site-packages/redis/connection.py", line 705, in read_response
    raise response
redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value

I tried to pass the key as str:

for key in rc.scan_iter():
    rc.hgetall(str(key))

But the results I am getting are empty dicts. What am I missing here? Is it possible to iterate thru a list of keys and call hgetall() to get the values for each individual key?

EDIT: As pointed out by Itamar, the database did indeed have an entry whose key was of string type. Doing a type check before hgetall avoids the issue.

Aakash Howlader
  • 3
  • 1
  • 1
  • 3
  • "> What am I missing here?" - one of the keys in your database isn't a Redis Hash (maybe a String, a List, ...) and your scanning picks it up. Options: for each key check the `TYPE` first, or use new `SCAN`'s type filter or make sure (somehow) that there are only hashes in your database – Itamar Haber Jun 11 '20 at 16:04
  • Yep. There was a key which was a string. – Aakash Howlader Jun 11 '20 at 22:41

1 Answers1

0

I got the same problem, I set up a String Key instead of a Hash key, then when I tried to retrieve the keys of the String key I got the error.

Solution: Ensure the desired key is Hash type:

>> redis.set 1,1
=> "OK"
>> redis.hkeys 1
Operation against a key holding the wrong kind of value: Expected Hash at 1.
Redis::CommandError: WRONGTYPE Operation against a key holding the wrong kind of value
from /onelogin/src/vendor/bundle/gems/fakeredis-0.5.0/lib/redis/connection/memory.rb:991:in `data_type_check'
from /onelogin/src/vendor/bundle/gems/fakeredis-0.5.0/lib/redis/connection/memory.rb:213:in `hkeys'
from /onelogin/src/vendor/bundle/gems/fakeredis-0.5.0/lib/fakeredis/command_executor.rb:10:in `send'
from /onelogin/src/vendor/bundle/gems/fakeredis-0.5.0/lib/fakeredis/command_executor.rb:10:in `write'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:257:in `write'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:236:in `io'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:255:in `write'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:214:in `process'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:208:in `each'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:208:in `process'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:353:in `ensure_connected'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:207:in `process'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:292:in `logging'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:206:in `process'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis/client.rb:112:in `call'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis.rb:1995:in `hkeys'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis.rb:37:in `synchronize'
from /usr/local/lib/ruby/1.8/monitor.rb:242:in `mon_synchronize'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis.rb:37:in `synchronize'
from /onelogin/src/vendor/bundle/gems/redis-3.2.1/lib/redis.rb:1994:in `hkeys'
from (irb):36>> 
G. I. Joe
  • 1,585
  • 17
  • 21