sql >> Base de Datos >  >> NoSQL >> Redis

Cómo almacenar un JSON anidado complejo en Redis usando Python

No puede hacer eso directamente, pero afortunadamente hay un nuevo módulo de Redis llamado RedisJSON que hace exactamente lo que necesita, y también tiene un buen enlace de Python. Puede iniciar un contenedor acoplable RedisJSON o usar Redis 4.0+, luego descargar/compilar e instalar RedisJSON y configurar Redis para cargarlo, y agrega comandos nativos para la manipulación de JSON.

Le permite almacenar documentos JSON en Redis y luego buscar o modificar un elemento específico en el árbol del documento, sin recuperar (o incluso analizar internamente) el documento. Su cliente de Python incluso le permite almacenar dictados de Python y convertirlos a JSON automáticamente.

Módulo ReJSON:http://redisjon.io

Cliente Python:https://pypi.python.org/pypi/rejson

Ejemplo:

from rejson import Client, Path

rj = Client(host='localhost', port=6379)

# Set the key `obj` to some object
obj = {
    'answer': 42,
    'arr': [None, True, 3.14],
    'truth': {
        'coord': 'out there'
    }
}
rj.jsonset('obj', Path.rootPath(), obj)

# Get something
print 'Is there anybody... {}?'.format(
    rj.jsonget('obj', Path('.truth.coord'))
)