Reid K
Reid K

Reputation: 47

How to structure python code when storing redis hashes and json

I need to read and write csv, rdb and json into redis using python. I'm considering using rejson for json support. The commands for rejson seem to be different when using rejson on json versus redis hashes for csv and rdb data.

Can I have only one set of code or do i need to condition it given the redis commands are different for json and rejson vs. hashes?

This code snippet shows how to use RedisJSON with raw Redis commands from Python with redis-py:

import redis
import json

data = {
    'foo': 'bar'
}

r = redis.StrictRedis()
r.execute_command('JSON.SET', 'doc', '.', json.dumps(data))
reply = json.loads(r.execute_command('JSON.GET', 'doc'))

Upvotes: 1

Views: 164

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 49962

You'll need dedicated code paths for each data structure. Hashes and JSON use a different API (as do the rest of the data structures)

Upvotes: 1

Related Questions