1

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)
LeanMan
  • 474
  • 1
  • 4
  • 18

1 Answers1

1

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.

Guy Royse
  • 2,739
  • 12
  • 9
  • I think I figured it out. I was on database 2 and redis-om defaults to database 0. How does one change the redis-om's connection to the redis server? – LeanMan Aug 24 '22 at 13:15
  • Using databases other than 0 will cause all sorts of problems as RediSearch, which Redis OM depends extensively upon, will only work on database 0. – Guy Royse Aug 24 '22 at 20:43
  • I'm very much surprised to hear this. Not only is this saying that a database can't be changed but a facility to change `user` and `password` could not be supported. `redis_om` must have something that errors out and tells the user that only `0` is a valid database which is not what I see in the code itself. I took a closer look in redis_om (python) at `redis_om/connections.py` and it seems it can take in a `url` parameter where a `user`, `password` and `database number` can be set in the `url`. – LeanMan Aug 24 '22 at 23:52
  • Redis is simply a key/value database, why wouldn't things like RedisSearch not work with just this base primitives in mind. What dependencies/implementations would break it if changing the pointer to another database?? – LeanMan Aug 24 '22 at 23:56
  • When I add `os.environ["REDIS_OM_URL"]="redis://localhost:6379/2"` the JSON.GET command returns the JSON object at database 2. I'm still confused though. For this to work, I have to put it before the import from redis_om and JsonModel. I still don't know how to set it where I want to initialize my settings. – LeanMan Aug 25 '22 at 00:19
  • I see in the redis-om for python documentation says `Note: Indexing only works for data stored in Redis logical database 0. If you are using a different database number when connecting to Redis, you can expect the code to raise a MigrationError when you run the migrator.` ... Sigh, I guess that is why. I'm still a bit surprised by this limitation. – LeanMan Aug 25 '22 at 06:24
  • I just recognized your name. I watched one of your WASM videos on youtube. Cool stuff! Thanks for chiming in on my SO question. Honored. – LeanMan Aug 25 '22 at 07:53
  • 1
    Thanks! WASM is super cool. Just waiting for network methods so I can write a WASM Redis client! – Guy Royse Aug 26 '22 at 16:04