How to switch from hmset() to hset() in Redis?
Asked Answered
P

4

14

I get the deprication warning, that Redis.hmset() is deprecated. Use Redis.hset() instead.

However hset() takes a third parameter and I can't figure out what name is supposed to be.

info = {'users': 10, "timestamp": datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')}
r.hmset("myKey", info)

The above works, but this requires a first parameter called name.

r.hset(name, "myKey", info)

Comparing the hset vs hmset in docs isn't clear to me.

Planetary answered 15/5, 2020 at 18:47 Comment(6)
You can use hset("myKey", mapping=info).Iliad
Sorry doesn't seem to accept it.Planetary
It appears the mapping kwarg was added in 3.5.0 (Apr 29, 2020). If your version is older, you will need to do as @Ersoy suggests.Iliad
@hmm it may be related to library's min-redis version. HSET with multiple field/value pairs is available since Redis v4.0.0.Whiggism
Yes, sorry, updating the lib to redis-3.5.2 makes the warning in PyCharm go away. Now I don't know which answer to accept. Both are correct. I think for clarity I go with Ersoy. Thanks both.Planetary
i will upvote @hmm then for more updated one :) - you are welcome Houman.Whiggism
W
1

You may execute multiple hset for each field/value pair in hmset.

r.hset('myKey', 'users', 10)
r.hset('myKey', 'timestamp', datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'))
r.hset('myKey', 'yet-another-field', 'yet-another-value')
  • first parameter is the key name
  • second parameter is the field name
  • third parameter is the value of the field.
Whiggism answered 15/5, 2020 at 18:57 Comment(1)
This works but is not a good switch from hset to hmset using the same intended action - which is one call to set multiple fields - so this should not be the correct answer.Laticialaticiferous
I
15

The problem is that you must specify within hset() that you are giving it the mapping. In your case:

r.hset("myKey", mapping=info)

instead of

r.hset("myKey", info)
Ikkela answered 8/3, 2021 at 9:34 Comment(1)
This should be the correct answer - the goal of the question is to set multiple fields in one call - not how to set each field individually.Laticialaticiferous
I
11

hmset(name, mapping): given a hash name ("myKey") and a dictionary (info) set all key/value pairs.

hset(name, key=None, value=None, mapping=None): given a hash name ("myKey") a key and a value, set the key/value. Alternatively, given a dictionary (mapping=info) set all key/value pairs in mapping.

Source: https://redis-py.readthedocs.io/en/stable/

If this does not work, perhaps you need to update the library?

Iliad answered 15/5, 2020 at 18:59 Comment(1)
This is technically right but is less clear than the answer by David Cano.Laticialaticiferous
A
4

I was using Redis.hmset() as following:

redis.hmset('myKey', info)

If you use Redis.hset() the following you will not get warning.

redis.hset('myKey', key=None, value=None, mapping=info)

With this usage, we will skip the single key & value adding step and redis.hset() will set all of the key and value mappings in info to myKey.

Asthenic answered 22/2, 2022 at 23:41 Comment(0)
W
1

You may execute multiple hset for each field/value pair in hmset.

r.hset('myKey', 'users', 10)
r.hset('myKey', 'timestamp', datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'))
r.hset('myKey', 'yet-another-field', 'yet-another-value')
  • first parameter is the key name
  • second parameter is the field name
  • third parameter is the value of the field.
Whiggism answered 15/5, 2020 at 18:57 Comment(1)
This works but is not a good switch from hset to hmset using the same intended action - which is one call to set multiple fields - so this should not be the correct answer.Laticialaticiferous

© 2022 - 2024 — McMap. All rights reserved.