通过redis-py将PostgreSQL时间戳存储为redis排序的分数集:DataError [英] Storing postgresql timestamptz as score to redis sorted set via redis-py : DataError
问题描述
我正在使用redis-py将postgresql时间戳存储到redis排序集中。
I am storing postgresql timestamptz to redis sorted set using redis-py.
timestamptz用作得分,数据用作值。
timestamptz is used as score and data is used as value.
我需要按降序对集合进行排序,但是我无法将数据插入redis。我不知道如何转换为redis支持的格式。
这是代码:
I need the set to be sorted in descending order.But I can't insert the data into redis.I don't know how to convert into redis-supported format. Here's the code:
cursor.execute("select current_timestamp;");
timestamp_raw=cursor.fetchone()
redis_client.zadd("stream",{data:timestamp_raw})
错误低于
.....\AppData\Local\Programs\Python\Python38-32\lib\site-packages\redis\connection.py", line 117, in encode
raise DataError("Invalid input of type: '%s'. Convert to a "redis.exceptions.DataError: Invalid input of type: 'tuple'. Convert to a byte, string or number first.
如何重新爱它?
推荐答案
在Psycopg2中,timestamptz作为日期时间和FixedOffsetTimezone的元组返回。
In Psycopg2, timestamptz is returned as a tuple of datetime and FixedOffsetTimezone.
您需要将元组转换为float以便可以用作分数。
You need to convert the tuple to float so it can be used as a score.
请参见在Python中将datetime.date转换为UTC时间戳
这篇关于通过redis-py将PostgreSQL时间戳存储为redis排序的分数集:DataError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!