将数据框摄取到 influxdb 2.x 时出现奇怪的结果

问题描述 投票:0回答:0

我正在使用 python 将数据提取到 influxdb,

但是很多例子都不起作用(空表,但没有报告错误),还有一个例子结果很奇怪。

设置客户端后,我通过以下方式创建数据框:

from datetime import datetime
from datetime import timedelta

_now = datetime.utcnow()
_data_frame = pd.DataFrame(data=[["coyote_creek", 1.0], ["coyote_creek", 2.0]],
                           index=[_now, _now + timedelta(hours=1)],
                           columns=["location", "water_level"])

然后我得到的数据是:

_data_frame
Out[24]: 
                                location  water_level
2023-03-07 02:04:11.642867  coyote_creek          1.0
2023-03-07 03:04:11.642867  coyote_creek          2.0

因此,我在摄取后应该有两个数据,但是,当我通过以下方式摄取数据时:

write_api.write("testing_for_dataframe", "org", record=_data_frame, data_frame_measurement_name='h2o_feet',
                            data_frame_tag_columns=['location'])

我只查询了一行数据:

query_api.query_data_frame('from(bucket:"testing_for_dataframe")|> range(start: -10m)')

我得到:

Out[31]: 
    result  table  ... _measurement      location
0  _result      0  ...     h2o_feet  coyote_creek
[1 rows x 9 columns]

尝试示例时完全可以工作:

from influxdb_client import InfluxDBClient, Point, Dialect
from influxdb_client.client.write_api import SYNCHRONOUS

client = InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org")

write_api = client.write_api(write_options=SYNCHRONOUS)
query_api = client.query_api()

"""
Prepare data
"""

_point1 = Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
_point2 = Point("my_measurement").tag("location", "New York").field("temperature", 24.3)

write_api.write(bucket="my-bucket", record=[_point1, _point2])

"""
Query: using Pandas DataFrame
"""
data_frame = query_api.query_data_frame('from(bucket:"my-bucket") '
                                        '|> range(start: -10m) '
                                        '|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value") '
                                        '|> keep(columns: ["location", "temperature"])')
print(data_frame.to_string())

"""
Close client
"""
client.close()

我不熟悉influxdb查询语法,但这不是重点,因为我检查了仪表板,它也显示它只有一行数据。

enter image description here

我的问题是:“有时,它无法摄取任何数据,只能创建具有空结果的命名表;同时,它会摄取部分数据并丢失一些数据而不会出错;此外,还有正确工作的示例,所以它是我猜不是因为其他设定问题。”

我想摄取一个数据框,并希望在仪表板中看到它(意味着它可以在 python 中查询)

python influxdb influxdb-python
© www.soinside.com 2019 - 2024. All rights reserved.