influxdb:多次写入多个点而不是一次写入单个点 [英] influxdb: Write multiple points vs single point multiple times
问题描述
我在项目中使用influxdb,并且一次写入多个点时遇到查询问题
I'm using influxdb in my project and I'm facing an issue with query when multiple points are written at once
我正在使用influxdb-python向influxdb写入1000个唯一点.
I'm using influxdb-python to write 1000 unique points to influxdb.
In the influxdb-python there is a function called influxclient.write_points()
我现在有两个选择:
- 每次(1000次)写入每个点一次,或
- 合并1000点并一次写入所有点.
第一个选项代码如下所示(仅适用于伪代码)并且有效:
The first option code looks like this(pseudo code only) and it works:
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
写完所有要点后,当我编写如下查询时:
After writing all the points, when I write a query like this:
SELECT * FROM "mydb"
我得到了所有的1000分.
I get all the 1000 points.
为了避免每次迭代中每次写操作都会增加开销,我觉得想要探索一次写多个点. write_points
函数支持.
To avoid the overhead added by every write in every iteration, I felt like exploring writing multiple points at once. Which is supported by the write_points
function.
write_points(点,time_precision =无,数据库=无, tention_policy =无,标签=无,batch_size =无)
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")
有了此更改,当我查询时:
With this change, when I query:
SELECT * FROM "mydb"
我只得到1分.我不明白为什么.
I only get 1 point as the result. I don't understand why.
任何帮助将不胜感激.
推荐答案
对于从本质上讲,您预先设置了一个SeriesHelper
类,并且每次发现要添加的数据点时,都将进行呼叫. SeriesHelper
将为您分批写入,每次写入最多bulk_size
点
In essence, you set up a SeriesHelper
class in advance, and every time you discover a data point to add, you make a call. The SeriesHelper
will batch up the writes for you, up to bulk_size
points per write
这篇关于influxdb:多次写入多个点而不是一次写入单个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!