Hi,
I am looking for an python API to store JSON in Redis Cluster.
I tried the following approach which did not work:
from rejson import Client, Path
startup_nodes = [{"host": "1.2.3.1", "port": 6379}, {"host": "1.2.3.2", "port": 6380}, {"host": "1.2.3.3", "port": 6381}]
rj = Client(host=startup_nodes, decode_responses=True)
jsonDoc = {'answer': 42, 'arr': [None, True, 3.14], 'truth': {'coord': 'out there'}}
print("Writing json doc - jsonDoc...")
rj.jsonset('testJsonDoc', Path.rootPath(), jsonDoc)
testJsonDoc = rj.jsonget('testJsonDoc', Path.rootPath())
print("testJsonDoc = " + str(nwTranDoc))
I get the following error:
Traceback (most recent call last):
File "testJson.py", line 9, in <module>
rj.jsonset('testJsonDoc', Path.rootPath(), jsonDoc)
File "/usr/local/lib/python3.4/dist-packages/rejson/client.py", line 151, in jsonset
return self.execute_command('JSON.SET', *pieces)
File "/usr/local/lib/python3.4/dist-packages/redis/client.py", line 754, in execute_command
connection.send_command(*args)
File "/usr/local/lib/python3.4/dist-packages/redis/connection.py", line 619, in send_command
self.send_packed_command(self.pack_command(*args))
File "/usr/local/lib/python3.4/dist-packages/redis/connection.py", line 594, in send_packed_command
self.connect()
File "/usr/local/lib/python3.4/dist-packages/redis/connection.py", line 493, in connect
sock = self._connect()
File "/usr/local/lib/python3.4/dist-packages/redis/connection.py", line 520, in _connect
socket.SOCK_STREAM):
File "/usr/lib/python3.4/socket.py", line 533, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
TypeError: getaddrinfo() argument 1 must be string or None
The above error tells me that, I cannot give an array of cluster node IP's, but I am using Redis Cluster, this has to be an array.
I even tried the above code with the following one change (as per above error) :
rj = Client(host="1.2.3.4", decode_responses=True, port=6379)
Nothing happens (I have to do a CTRL + Z to stop execution) on executing the code with above change from command prompt.
I could not find any other API for Redis cluster to store JSON as REJSON in Redis
The following approach worked by storing JSON as a string, but I have to then convert it to a JSON before using.
import json
import rediscluster
startup_nodes = [{"host": "1.2.3.1", "port": 6379}, {"host": "1.2.3.2", "port": 6380}, {"host": "1.2.3.3", "port": 6381}]
redis_conn = rediscluster.RedisCluster(startup_nodes=startup_nodes, decode_responses=True)
jsonDoc = {'answer': 42, 'arr': [None, True, 3.14], 'truth': {'coord': 'out there'}}
redis_conn.set("jsonDoc", json.dumps(jsonDoc))
jsonDocFromDb = json.loads(redis_conn.get("jsonDoc"))
Is there a Python API for Redis Cluster to store JSON as REJSON in Redis Cluster ?
Thanks,
Sachin Vyas