0

我正在为一个需要在一段时间内从涌入中查询数据的过程构建一个有点自动化的查询构建器。为此,我需要按时间过滤,这意味着我的查询中需要纪元毫秒字符串。此代码引发以下错误。

import pandas as pd

ts = pd.Timestamp(pd.Timestamp('now', tz='US/Pacific').date(), tz='US/Pacific')
ts_str = str(int(ts.value / (10 ** 6)))
query_str = 'SELECT * FROM "table"."measurement" WHERE time <= ' + \
            ts_str + 'ms LIMIT 10'
json_payload = client.query(query_str)
print(json_payload)

和堆栈跟踪:

Traceback (most recent call last):
  File "my_code.py", line 31, in query
    return self.client.query(query)
  File "/usr/local/lib/python2.7/site-packages/influxdb/_dataframe_client.py", line 138, in query
    results = super(DataFrameClient, self).query(query, database=database)
  File "/usr/local/lib/python2.7/site-packages/influxdb/client.py", line 339, in query
    expected_response_code=expected_response_code
  File "/usr/local/lib/python2.7/site-packages/influxdb/client.py", line 239, in request
    timeout=self._timeout
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 474, in request
    prep = self.prepare_request(req)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 407, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/usr/local/lib/python2.7/site-packages/requests/models.py", line 302, in prepare
    self.prepare_url(url, params)
  File "/usr/local/lib/python2.7/site-packages/requests/models.py", line 426, in prepare_url
    enc_params = self._encode_params(params)
  File "/usr/local/lib/python2.7/site-packages/requests/models.py", line 104, in _encode_params
    return urlencode(result, doseq=True)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 1353, in urlencode
    v = quote_plus(v)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 1308, in quote_plus
    s = quote(s, safe + ' ')
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 1303, in quote
    return ''.join(map(quoter, s))
KeyError: 83
4

2 回答 2

1

我可以在类似的情况下确认您的发现:使用包中str()的字符串future和标准 Python 2.7 字符串连接字符串。可以给以下函数提供历元浮点数作为定义时间窗口的参数。在查询期间使用str()会导致 a 。KeyError: 83

from builtins import *
# no 'from __future__ import unicode_literals' for some reason
from influxdb import InfluxDBClient

client = InfluxDBClient(host=db_IP, port=db_port, database=db_name)

def get_measurement(start='now() - 1d', stop='now()'):
    # Checks the arguments
    if start != 'now() - 1d':
        start = str(int(start)) + 's'     #Replace str() with unicode()
    if stop != 'now()':
        stop = str(int(stop)) + 's'       #Replace str() with unicode()
    # Format the database query and query
    query = "SELECT * FROM table WHERE (time > " + start + ") AND (time < " + stop + ")"
    response = client.query(query)

使用unicode()而不是str()解决它。
它指示查看startstop解析后的类型。str()返回一个<class 'future.types.newstr.newstr'>类型,而unicode()返回一个<type 'unicode'>。查询字符串似乎最终搞砸了,但我不确定细节。


编辑:Python 3.x 中不存在警告。 unicode()我的解决方案不是面向未来的,只应被视为与 Python 2.x 相关。任何改进的建议表示赞赏。


编辑#2:更新Python 2/3 兼容性。我在两个语句中添加了异常管理if来处理兼容性。任何更“pythonic”或更优雅的解决方案都会受到赞赏。

if start != 'now() - 1d':
    try:
        start = unicode(int(start)) + 's'   # no unicode() in Python 3 !
    except NameError:
        start = str(int(start)) + 's'
if stop != 'now()':
    try:
        stop = unicode(int(stop)) + 's'
    except NameError:
        stop = str(int(stop)) + 's'
于 2018-03-16T18:07:00.077 回答
0

Influx 对 unicode、单引号、双引号等非常挑剔。将 str 换成 unicode 换成 ts_str 是可行的。打印出 query_str 之前和之后看起来相同,但在引擎盖下类型很重要。

import pandas as pd

ts = pd.Timestamp(pd.Timestamp('now', tz='US/Pacific').date(), tz='US/Pacific')
ts_str = unicode(int(ts.value / (10 ** 6)))
query_str = 'SELECT * FROM "table"."measurement" WHERE time <= ' + \
            ts_str + 'ms LIMIT 10'
json_payload = client.query(query_str)
print(json_payload)
于 2017-03-29T01:03:44.623 回答