influxdb:多次写入多个点而不是单点

2024-05-19 07:57:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我在我的项目中使用influxdb,当一次写入多个点时,我遇到了一个查询问题

我使用influxdb python为influxdb编写1000个唯一点。在

在influxdb python中有一个名为influxclient.write_points()的函数

我现在有两个选择:

  1. 每次写一次每个点(1000次)或
  2. 巩固1000分,把所有的分数写一次。在

第一个选项代码如下所示(仅限伪代码),并且可以工作:

thousand_points = [0...9999
while i < 1000:
    ...
    ...
    point = [{thousand_points[i]}]  # A point must be converted to dictionary object first
    influxclient.write_points(point, time_precision="ms")
    i += 1

在写下所有的要点之后,当我像这样写一个查询时:

^{pr2}$

我得了1000分。在

为了避免每次迭代中每次写入都会增加开销,我想一次编写多个点。它由write_points函数支持。在

write_points(points, time_precision=None, database=None, retention_policy=None, tags=None, batch_size=None)

Write to multiple time series names.

Parameters: points (list of dictionaries, each dictionary represents a point) – the list of points to be written in the database

所以,我所做的是:

thousand_points = [0...999]
points = []
while i < 1000:
    ...
    ...
    points.append({thousand_points[i]})  # A point must be converted to dictionary object first
    i += 1

influxclient.write_points(points, time_precision="ms")

有了这个变化,当我询问:

^{pr2}$

结果我只得到1分。我不明白为什么。在

任何帮助都将不胜感激。在


Tags: to函数代码nonedictionarytimebepoints
2条回答

你可能有一个很好的例子来证明^{}

本质上,您预先设置了一个SeriesHelper类,并且每次发现要添加的数据点时,都要进行调用。SeriesHelper将为您批量写入,每次写入最多bulk_size

我知道一年前就有人问过这个问题,但是,为了批量发布多个数据点到influxdb,每个数据点似乎都需要有一个唯一的时间戳,否则就会被不断地覆盖。在

我将导入datetime,并将以下内容添加到for loop内的每个数据点:

'time': datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")

所以每个数据点应该看起来像。。。在

{'fields': data, 'measurement': measurement, 'time': datetime....}

希望这对其他遇到这种情况的人有帮助!在

编辑:阅读文档会显示另一个唯一标识符是一个标记,因此如果您希望指定时间,可以改为包含{'tag' : i}(假设每个迭代值都是唯一的)。(但我没试过)

相关问题 更多 >

    热门问题