简体   繁体   English

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

[英]Filter items newer than 1 hour with RethinkDB and Python

I have a Python-script gathering some metrics and saving them to RethinkDB. 我有一个Python脚本收集一些指标并将它们保存到RethinkDB。 I have also written a small Flask-application to display the data on a dashboard. 我还写了一个小的Flask应用程序来在仪表板上显示数据。

Now I need to run a query to find all rows in a table newer than 1 hour. 现在我需要运行一个查询来查找表格中超过1小时的所有行。 This is what I got so far: 这是我到目前为止所得到的:

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)

When I try to visit the page I get this error message: 当我尝试访问该页面时,我收到以下错误消息:

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

I googled a bit and found this thread which seems to be a similar problem: https://github.com/rethinkdb/rethinkdb/issues/4827 , so I revisited how I add new rows to the database as well to see if that was the issue: 我google了一下,发现这个线程似乎是一个类似的问题: 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' retrieved by my data collection-script contains some garbage I remove, and then create a datetime-object. 我的数据集合脚本检索的'_time'包含一些我删除的垃圾,然后创建一个datetime-object。 As far as I can understand from the RethinkDB documentation I should be able to insert these directly, and if I use "data explorer" in RethinkDB's Admin Panel my rows look like this: 据我所知,从RethinkDB文档我可以直接插入这些文件,如果我在RethinkDB的管理面板中使用“数据资源管理器”,我的行看起来像这样:

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

Update: I did another test and created a small script to insert data and then retrieve it 更新:我做了另一个测试并创建了一个小脚本来插入数据然后检索它

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)

I still get the same error message: 我仍然得到相同的错误消息:

$ 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()))

I finally figured it out. 我终于弄明白了。 The error is in the lambda-expression. 错误在lambda表达式中。 You need to use .during() on a specific field. 您需要在特定字段上使用.during()。 If not the query will try to wrestle the whole row/document into a timestamp 如果不是,查询将尝试将整个行/文档搏入时间戳

This code works: 此代码有效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM