无法使用google colab的redis-om JsonModel将数据保存到redis云

zmeyuzjn  于 2023-10-15  发布在  Redis
关注(0)|答案(1)|浏览(169)

我正尝试使用google colab的redis-om JsonModel将数据作为持久化JSON文件保存到redis云。
使用下面的脚本,我收到这个错误:

OSError                                   Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/redis/connection.py in connect(self)
    706         try:
--> 707             sock = self.retry.call_with_retry(
    708                 lambda: self._connect(), lambda error: self.disconnect(error)

10 frames
/usr/local/lib/python3.10/dist-packages/redis/retry.py in call_with_retry(self, do, fail)
     45             try:
---> 46                 return do()
     47             except self._supported_errors as error:

/usr/local/lib/python3.10/dist-packages/redis/connection.py in <lambda>()
    707             sock = self.retry.call_with_retry(
--> 708                 lambda: self._connect(), lambda error: self.disconnect(error)
    709             )

/usr/local/lib/python3.10/dist-packages/redis/connection.py in _connect(self)
   1005         if err is not None:
-> 1006             raise err
   1007         raise OSError("socket.getaddrinfo returned an empty list")

/usr/local/lib/python3.10/dist-packages/redis/connection.py in _connect(self)
    993                 # connect
--> 994                 sock.connect(socket_address)
    995 

OSError: [Errno 99] Cannot assign requested address

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
<ipython-input-27-4ee06a3d036c> in <cell line: 24>()
     22 
     23 # Create an instance of the model class with some data
---> 24 person = Person(name="Alice", age=25, email="[email protected]")
     25 
     26 # Save the instance to Redis as a JSON document

/usr/local/lib/python3.10/dist-packages/redis_om/model/model.py in __init__(self, *args, **kwargs)
   1676 
   1677     def __init__(self, *args, **kwargs):
-> 1678         if not has_redis_json(self.db()):
   1679             log.error(
   1680                 "Your Redis instance does not have the RedisJson module "

/usr/local/lib/python3.10/dist-packages/redis_om/checks.py in has_redis_json(conn)
     15     if conn is None:
     16         conn = get_redis_connection()
---> 17     command_exists = check_for_command(conn, "json.set")
     18     return command_exists
     19 

/usr/local/lib/python3.10/dist-packages/redis_om/checks.py in check_for_command(conn, cmd)
      7 @lru_cache(maxsize=None)
      8 def check_for_command(conn, cmd):
----> 9     cmd_info = conn.execute_command("command", "info", cmd)
     10     return None not in cmd_info
     11 

/usr/local/lib/python3.10/dist-packages/redis/client.py in execute_command(self, *args, **options)
   1264         pool = self.connection_pool
   1265         command_name = args[0]
-> 1266         conn = self.connection or pool.get_connection(command_name, **options)
   1267 
   1268         try:

/usr/local/lib/python3.10/dist-packages/redis/connection.py in get_connection(self, command_name, *keys, **options)
   1459         try:
   1460             # ensure this connection is connected to Redis
-> 1461             connection.connect()
   1462             # connections that the pool provides should be ready to send
   1463             # a command. if not, the connection was either returned to the

/usr/local/lib/python3.10/dist-packages/redis/connection.py in connect(self)
    711             raise TimeoutError("Timeout connecting to server")
    712         except OSError as e:
--> 713             raise ConnectionError(self._error_message(e))
    714 
    715         self._sock = sock

ConnectionError: Error 99 connecting to localhost:6379. Cannot assign requested address.

备注:
1.我在Redis云上提供的端口不是6379,我不明白为什么脚本试图使用localhost
1.我在redis云上的模块配置到Redis Stack
1.使用我的连接凭证,我能够读写数据到流/从流读取数据-因此凭证是正确的
1.我在Redis文档中看到可以在redis云上使用JsonModel
使用的脚本:

!pip install redis
!pip install redis-om
import redis
import redis_om
from redis_om import JsonModel, Field

server_ip = "my correct redis cloud server_ip"
port =  "my correct redis cloud port"
password =  "my correct redis cloud password"

# Connect to the Redis instance
client = redis.Redis(host=server_ip, port=port, password=password)

# Define a custom model class using JsonModel
class Person(JsonModel):
    name: str = Field(index=True)
    age: int 
    email: str 

# Create an instance of the model class with some data
person = Person(name="Alice", age=25, email="[email protected]")

# Save the instance to Redis as a JSON document
person.save()

# Print the ID and the JSON representation of the instance
print(person.id)
print(person.to_json())
yduiuuwa

yduiuuwa1#

你正在用你的服务器凭证创建一个Redis客户端client,但没有在Redis OM模型中使用它,所以假设Redis在localhost:6379上,并试图到达那里。
Redis OM Python允许您使用REDIS_OM_URL环境变量指定Redis的位置,该环境变量需要Redis URL格式(https://github.com/redis/redis-om-python/blob/main/docs/connections.md
所以你需要设置一下。示例值为:

redis://default:<password>@<host>:<port>/

<host><port><password>分别替换为Redis Cloud主机、端口和密码。

相关问题