redis - Python3/Redis : redis. 异常。响应错误:未知命令 'JSON.SET'

标签 redis python-3.6

我正在尝试运行 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 或添加任何模块的选项。

enter image description here

最佳答案

不确定当我创建 REDIS 数据库时这是否可用,但现在可以了。当您在 redislabs.com 上创建它时,您可以打开模块,然后从列表中选择一个。

enter image description here

然后使用这个库:来自 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/

相关文章:

redis - Lettuce core redis blpop 带来Type mismatch Can't assign non-array value to an array

node.js - 使用 node_redis 在 node.js 应用程序中使用 redis 检查缓存键是否存在

python - RQ - 清空和删除队列

matplotlib - 是否可以使用seaborn绘制超过6列的线图?

python - PyAutoGUI 在屏幕上定位,在 linux 上出现错误

python - 类中派生类中预期方法的断言和文档

ruby-on-rails - 什么是更好的 redis 锁定机制来避免 rails 中的异步验证失败?

multiprocessing - 多处理命令列表

python - 使用 f-string 的查询格式

redis - 从 Kubernetes Pod 中写入数据到主机