Python3/Redis: redis.exceptions.ResponseError: unknown command 'JSON.SET'
Python3/Redis: redis.exceptions.ResponseError: unknown command 'JSON.SET'
我正在尝试 运行 来自 this RedisLabs page 的示例程序。
我选择了选项 A - 这是设置免费的 Redis 云服务器。
(好像如果你手动安装,那么你必须添加 JSON 作为插件。)
我能够连接并使用其他“设置”命令,但在 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)
我正在尝试 运行 来自 this RedisLabs page 的示例程序。 我选择了选项 A - 这是设置免费的 Redis 云服务器。 (好像如果你手动安装,那么你必须添加 JSON 作为插件。) 我能够连接并使用其他“设置”命令,但在 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)