我正在尝试运行 this RedisLabs page 中的示例程序。 我选择了选项 A - 设置免费的 Redis 云服务器。 (似乎如果您手动安装,则必须将 JSON 添加为插件。) 我可以连接并使用其他“set”命令,但在 JSON 上出现错误:
File "C:\Users\nwalt\.virtualenvs\TDAmeritradeGetQuotes\lib\site-packages\redis\client.py", line 901, in execute_command
return self.parse_response(conn, command_name, **options)
File "C:\Users\nwalt\.virtualenvs\TDAmeritradeGetQuotes\lib\site-packages\redis\client.py", line 915, in parse_response
response = connection.read_response()
File "C:\Users\nwalt\.virtualenvs\TDAmeritradeGetQuotes\lib\site-packages\redis\connection.py", line 756, in read_response
raise response
redis.exceptions.ResponseError: unknown command 'JSON.SET'
我的Python测试程序(除了在发布之前放入示例端点):
import redis
import json
import pprint
host_info = "redis.us-east-1-1.ec2.cloud.redislabs.com"
redisObj = redis.Redis(host=host_info, port=18274, password='xxx')
print ("Normal call to Redis")
redisObj.set('foo', 'bar')
value = redisObj.get('foo')
print(value)
capitals = {
"Lebanon": "Beirut",
"Norway": "Oslo",
"France": "Paris"
}
print ("capitals - before call to Redis")
pprint.pprint(capitals)
print("JSON call to Redis")
redisObj.execute_command('JSON.SET', 'doc', '.', json.dumps(capitals))
print("Data Saved, now fetch data back from redis")
reply = json.loads(redisObj.execute_command('JSON.GET', 'doc'))
print("reply from Redis get")
pprint.pprint(reply)
这是我在他们的网站上创建数据库的屏幕截图。我没有看到任何启用 JSON 或添加任何模块的选项。
最佳答案
不确定当我创建 REDIS 数据库时这是否可用,但现在可以了。当您在 redislabs.com 上创建它时,您可以打开模块,然后从列表中选择一个。
然后使用这个库:来自 https://pypi.org/project/rejson/ 的“rejson”要获取方法“jsonset”方法,请使用如下代码:
rj = Client(host=config_dict['REDIS_CONFIG_HOST'], port=config_dict['REDIS_CONFIG_PORT'], password=config_dict['REDIS_CONFIG_PASSWORD'], decode_responses=True)
out_doc = {}
out_doc['firstname'] = "John"
out_doc['lastname'] = "Doe"
rj.jsonset('config', Path.rootPath(), out_doc)
get_doc = rj.jsonget('config', Path.rootPath())
pprint.pprint(get_doc)
关于redis - Python3/Redis : redis. 异常。响应错误:未知命令 'JSON.SET',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64067197/