如何使用 influxdb-python 将正确的时间戳发送到 influxdb

How send proper timestamp to influxdb with influxdb-python

我有 influxdb 数据库 test 测量:

name: mes1
time          Amount     Buy_order_id Price     
----          ------     ------------ -----     
1529832177822 0.02294    132868375    130117.83 

我想在 Grafana 中制作图表,但所有数据都是 1970 年的。我有其他测量值:

name: cpu_load_short
time                Bool_value Float_value Int_value String_value host     region
----                ---------- ----------- --------- ------------ ----     ------
1257894000000000000 true       0.64        3         Text         server01 us-west

这次工作正常。我发现,测量 cpu_load_short 中的时间存储在 ns 中,但测量中的数据 mes1 存储在 ms.

我从 websocket 收到 mes1 的时间。 cpu_load_short 的时间来自 python:

datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')

所有数据都通过influxdb-python发送到influxdb。我尝试调整 mes1 的时间并在数字末尾添加六个零:

'1529832177822' -> '1529832177822000000'

但我收到了:

OverflowError: signed integer is greater than maximum

我怎样才能将数据发送到 influxdb 并从中制作图表,以便数据采用正确的格式和正确的日期?也许我遗漏了一些东西,但我不明白为什么我不能在 ns 中将数据发送到我的数据库,但我可以用 datetime 发送它。谁能解释一下,问题出在哪里?

我遇到了同样的问题,并且能够解决,让我尝试指导你。

当您使用 influxdb-python 客户端将您的点写入 InfluxDB 时,您可以在 write_points 方法中指定时间精度。 (http://influxdb-python.readthedocs.io/en/latest/api-documentation.html#influxdb.DataFrameClient.write_points)

一个例子:

from influxdb import InfluxDBClient
client = InfluxDBClient(host=host, port=port, database=database)
client.write_points(value, time_precision='ms')

这将为您将 ms 转换为 ns。希望这有帮助。