InfluxDB DataFrameClient KeyError: 83

InfluxDB DataFrameClient KeyError: 83

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

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

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)

我可以在类似的上下文中确认您的发现:使用 future 包中的 str() 和标准 Python 2.7 字符串连接字符串。可以给以下函数提供纪元浮点数作为定义时间 window 的参数。在查询期间使用 str() 将导致 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() returns 键入 <class 'future.types.newstr.newstr'>unicode() returns 键入 <type 'unicode'>。似乎查询字符串最终被弄乱了,但我不确定细节。


编辑:警告 unicode() 不存在于 Python 3.x 中。我的解决方案不是 future-proof,只应被视为与 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'