View JSONModel in redis-cli after using python redis-om to save it to the database

Viewed 42

Given the following code

from redis_om import HashModel, JsonModel

import redis

r = redis.Redis(host='localhost', port=6379, db=2)
kwargs = {'key1': 'value1', 'keyn': 'value2'}
jsonModel = JsonModel(**kwargs)
jsonModel.save()
print(jsonModel.key()) # example: :redis_om.model.model.JsonModel:01GB7B0V33424C9SDK6FM8496Q

How do I go into redis-cli and view the JSON Model in redis? (Assuming I have correctly loaded the RedisJSON module into redis server).

I'm really new to redis so I tried things like the following but I really have no idea and still finding my way around the documentation.

127.0.0.1:6379[2]> get :redis_om.model.model.JsonModel:01GB7B0V33424C9SDK6FM8496Q
(nil)
127.0.0.1:6379[2]> json.get :redis_om.model.model.JsonModel:01GB7B0V33424C9SDK6FM8496Q
(nil)
1 Answers

I think your keyname is incorrect. You can confirm this by calling:

127.0.0.1:6379> EXISTS :redis_om.model.model.JsonModel:01GB7B0V33424C9SDK6FM8496Q

If you get a 0, that key doesn't exists. If you get a 1, it does. I suspect—although I don't know Redis OM for Python very well—that the leading colon is not required.

Try this:

127.0.0.1:6379> JSON.GET redis_om.model.model.JsonModel:01GB7B0V33424C9SDK6FM8496Q

Or maybe it's less than that. Like this:

127.0.0.1:6379> JSON.GET JsonModel:01GB7B0V33424C9SDK6FM8496Q

Also, assuming this isn't a production database and it doesn't have much in it, you can use KEYS to get a list of all the keys in Redis. That would tell you the structure of the keynames:

127.0.0.1:6379> KEYS *

Do be careful with KEYS as it locks the main thread on Redis when it runs. Not a big deal if you have scores of keys, but problematic if you have thousands or millions.

Related