gpt4 book ai didi

python - 使用 RethinkDB 和 Python 过滤超过 1 小时的项目

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:04 24 4
gpt4 key购买 nike

我有一个 Python 脚本收集一些指标并将它们保存到 RethinkDB。我还编写了一个小型 Flask 应用程序来在仪表板上显示数据。

现在我需要运行查询来查找表中超过 1 小时的所有行。这是我到目前为止得到的:

tzinfo = pytz.timezone('Europe/Oslo')
start_time = tzinfo.localize(datetime.now() - timedelta(hours=1))
r.table('metrics').filter( lambda m:
m.during(start_time, r.now())
).run(connection)

当我尝试访问该页面时,我收到此错误消息:

ReqlRuntimeError: Not a TIME pseudotype: `{
"listeners": "6469",
"time": {
"$reql_type$": "TIME",
"epoch_time": 1447581600,
"timezone": "+01:00"
}
}` in:
r.table('metrics').filter(lambda var_1:
var_1.during(r.iso8601('2015-11-18T12:06:20.252415+01:00'), r.now()))

我用谷歌搜索了一下,发现这个线程似乎是一个类似的问题:https://github.com/rethinkdb/rethinkdb/issues/4827 ,所以我重新审视了如何向数据库中添加新行,看看是否是问题所在:

def _fix_tz(timestamp):
tzinfo = pytz.timezone('Europe/Oslo')
dt = datetime.strptime(timestamp[:-10], '%Y-%m-%dT%H:%M:%S')
return tzinfo.localize(dt)
...
for row in res:
... remove some data, manipulate some other data ...
r.db('metrics',
{'time': _fix_tz(row['_time']),
...
).run(connection)

我的数据收集脚本检索到的“_time”包含我删除的一些垃圾,然后创建一个日期时间对象。据我从 RethinkDB 文档中了解到,我应该能够直接插入这些,如果我在 RethinkDB 的管理面板中使用“数据浏览器”,我的行如下所示:

{
...
"time": Sun Oct 25 2015 00:00:00 GMT+02:00
}

更新:我做了另一个测试并创建了一个小脚本来插入数据然后检索它

import rethinkdb as r

conn = r.connect(host='localhost', port=28015, db='test')

r.table('timetests').insert({
'time': r.now(),
'message': 'foo!'
}).run(conn)

r.table('timetests').insert({
'time': r.now(),
'message': 'bar!'
}).run(conn)

cursor = r.table('timetests').filter(
lambda t: t.during(r.now() - 3600, r.now())
).run(conn)

我仍然收到相同的错误消息:

$ python timestamps.py 
Traceback (most recent call last):
File "timestamps.py", line 21, in <module>
).run(conn)
File "/Users/tsg/.virtualenv/p4-datacollector/lib/python2.7/site-packages/rethinkdb/ast.py", line 118, in run
return c._start(self, **global_optargs)
File "/Users/tsg/.virtualenv/p4-datacollector/lib/python2.7/site-packages/rethinkdb/net.py", line 595, in _start
return self._instance.run_query(q, global_optargs.get('noreply', False))
File "/Users/tsg/.virtualenv/p4-datacollector/lib/python2.7/site-packages/rethinkdb/net.py", line 457, in run_query
raise res.make_error(query)
rethinkdb.errors.ReqlQueryLogicError: Not a TIME pseudotype: `{
"id": "5440a912-c80a-42dd-9d27-7ecd6f7187ad",
"message": "bar!",
"time": {
"$reql_type$": "TIME",
"epoch_time": 1447929586.899,
"timezone": "+00:00"
}
}` in:
r.table('timetests').filter(lambda var_1: var_1.during((r.now() - r.expr(3600)), r.now()))

最佳答案

我终于明白了。错误在 lambda 表达式中。您需要在特定字段上使用 .during()。如果不是,查询将尝试将整行/文档转换为时间戳

此代码有效:

cursor = r.table('timetests').filter(
lambda t: t['time'].during(r.now() - 3600, r.now())
).run(conn)

关于python - 使用 RethinkDB 和 Python 过滤超过 1 小时的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33779732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com