了解如何使用Python将数据写入InfluxDB

时间:2019-11-14 10:27:22

标签: influxdb-python

我正在使用(使用DataFrameClient)以及两个标签将值(浮点数)写入Influx数据库中:ID和时间戳记

def write_to_influx(ID, value):
    df = pandas.DataFrame(data=[value],index=pandas.date_range(start=datetime.utcnow(), periods=1, freq='S'), columns=['value'])
    client.write_points(df, dataPointID, database="mydb")

在我的循环中,碰巧我连续多次写入同一数据库,如下所示:

while True:

    value_1,value_2,value_3,value_4,value_5 = some_calculation(x,y,z)

    write_to_influx(value_1, ID_1)
    write_to_influx(value_2, ID_2)
    write_to_influx(value_3, ID_3)
    write_to_influx(value_4, ID_4)
    write_to_influx(value_5, ID_5)

有人告诉我,可以将这些写请求批量处理成一个,这样可以减少Influx的负担。

实际上在遇到问题时,我从另一个脚本中偷了write_to_influx方法。在文档中说

write_points(points, time_precision=None, database=None, retention_policy=None, tags=None, batch_size=None, protocol=u'json', consistency=None)

参数: 点(字典列表,每个词典代表一个点)–要写入数据库的点列表

但是使用write_to_influx(value_1, ID_1),不仅有字典,而且传递了数据点ID。

模拟已应用的方法和文档说明...如果我想将多个值写入多个数据点ID,是否应该传递字典列表和数据点ID列表?因为这似乎不起作用:

def write_values_to_influx(ID_list, value_list):

    indexes=pandas.date_range(start=datetime.utcnow().replace(tzinfo=pytz.UTC), periods=1, freq='S')

    dict_list=[]
    for value in value_list:
        df_new= pandas.DataFrame(data=[value],index=indexes)
        dict_list.append(df_new)

    client.write_points(dict_list, ID_list, database="mydb")

为True时:

value_1,value_2,value_3,value_4,value_5 = some_calculation(x,y,z)

ID_list=[ID_1,ID_2,ID_3,ID_4,ID_5]
value_list=[value_1,value_2,value_3,value_4,value_5]

write_values_to_influx(ID_list, value_list)

返回错误:

File "/vdp/base_functions.py", line 249, in write_values_to_influx
xyz@crVB    |     client.write_points(dict_list, ID_list, database="mydb")
xyz@crVB    |   File "/usr/lib/python3.6/site-packages/influxdb/_dataframe_client.py", line 123, in write_points
xyz@crVB    |     numeric_precision=numeric_precision)
xyz@crVB    |   File "/usr/lib/python3.6/site-packages/influxdb/_dataframe_client.py", line 282, in _convert_dataframe_to_lines
xyz@crVB    |     dataframe = dataframe.dropna(how='all').copy()

我的问题: 如何一次写入多个条目(到同一数据库)? write_points是write方法吗?还有DataFrameClient?

0 个答案:

没有答案