InfluxDB DataFrameClient KeyError:83

时间:2017-03-29 01:03:45

标签: python pandas dataframe influxdb influxdb-python

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

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

2 个答案:

答案 0 :(得分:1)

我可以在类似的上下文中确认您的发现:使用str()包中的future和标准Python 2.7字符串连接字符串。可以将以下函数作为epoch浮点数作为定义时间窗口的参数。使用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()会返回<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'

答案 1 :(得分:0)

Influx非常挑剔unicode,单引号,双引号等。为ts_str交换str for unicode的工作原理。在查看之前和之后打印出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)