-2
redis = {}
        redis['A'] = {}
        redis['A']['1'] = value1
        redis['A']['2'] = value2
        redis['A']['3'] = value3

I need to make above structure in redis using python. I have established connection using redis-py client in python like this:

_connection = redis.StrictRedis(host='localhost', port=6379, db=0)

please let me know how to create , store and get values in redis using python?

tushar patil
  • 359
  • 2
  • 4
  • 13

1 Answers1

0

It's important to know that Redis does not support arbitrarily nested data structures natively. Instead, it has a few (maps, lists, queues). Anything more sophisticated, you need to decompose into these structures.

Now if you only care about two levels of nesting, then you can use HMAP support. See HSET and HGET

In your example, the HMAP's name would be 'A', and 1, 2, and 3 would be keys.

Also note that Redis only supports binary and string key/values. So you would need to decide whether you are going to store keys in str(x) form, or encode in binary (x.to_bytes(4, byteorder = 'big')

I leave it to you to figure out how to map this to the python client's APIs -- they tend to match the logical operations faithfully. For example,

_connection.hget('A', '1')
Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52