Saturday, October 28, 2023

Redis JSON, with Docker

 JSON | Redis

The JSON capability of Redis Stack provides JavaScript Object Notation (JSON) support for Redis. It lets you store, update, and retrieve JSON values in a Redis database, similar to any other Redis data type. Redis JSON also works seamlessly with Search and Query to let you index and query JSON documents.

not "free", much bigger size than standard Redis

https://hub.docker.com/r/redis/redis-stack/tags

redis-stack: 337 MB compressed, linux/amd64

https://hub.docker.com/_/redis/tags

redis: 49 MB compressed, linux/amd64

docker run -d --name redis-stack -p 6380:6379 redis/redis-stack
docker exec -it redis-stack sh

# redis-cli
ping
PONG

JSON.SET enterprise . '{"captain": "kirk"}'
JSON.GET enterprise
{"captain": "kirk"}
JSON.SET enterprise .designation '{"NCC-1701"}'
JSON.GET enterprise
{"captain": "kirk", "designation": "NCC-1701"}

exit
exit

https://redis.io/docs/data-types/json/#run-with-docker

python example

> pip install redis > python redis-json.py

import redis
data = {
    'dog': {
        'scientific-name' : 'Canis familiaris'
    }
}
r = redis.Redis(host='localhost', port=6380, decode_responses=True)
r.json().set('doc', '$', data)
doc = r.json().get('doc', '$')
dog = r.json().get('doc', '$.dog')
scientific_name = r.json().get('doc', '$..scientific-name')
print(scientific_name) # ['Canis familiaris']



No comments: