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

redis-stack: 337 MB compressed, linux/amd64

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

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


python example

> pip install redis > python

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: