13

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.

Houman
  • 64,245
  • 87
  • 278
  • 460
  • 1
    You can use `hset("myKey", mapping=info)`. – chash May 15 '20 at 18:51
  • Sorry doesn't seem to accept it. – Houman May 15 '20 at 18:54
  • 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. – chash May 15 '20 at 19:04
  • @hmm it may be related to library's min-redis version. HSET with multiple field/value pairs is available since Redis v4.0.0. – Ersoy May 15 '20 at 19:09
  • 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. – Houman May 15 '20 at 19:14
  • 1
    i will upvote @hmm then for more updated one :) - you are welcome Houman. – Ersoy May 15 '20 at 19:17

4 Answers4

13

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)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
David Cano
  • 131
  • 1
  • 3
  • 3
    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. – russellthehippo Aug 11 '21 at 19:38
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?

chash
  • 3,975
  • 13
  • 29
3

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.

ramazans
  • 31
  • 1
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.
Ersoy
  • 8,816
  • 6
  • 34
  • 48
  • 3
    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. – russellthehippo Aug 11 '21 at 19:41