Storing postgresql timestamptz as score to redis sorted set via redis-py : DataError

霸气de小男生 提交于 2020-01-25 05:10:05

问题


I am storing postgresql timestamptz to redis sorted set using redis-py.

timestamptz is used as score and data is used as value.

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})

Error is below

.....\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.

How to reslove it?


回答1:


In Psycopg2, timestamptz is returned as a tuple of datetime and FixedOffsetTimezone.

You need to convert the tuple to float so it can be used as a score.

See Converting datetime.date to UTC timestamp in Python



来源:https://stackoverflow.com/questions/59706438/storing-postgresql-timestamptz-as-score-to-redis-sorted-set-via-redis-py-datae

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!