使用带有 Line 协议的 Python 客户端将数据插入 Influxdb

Inserting data into Influxdb using Python client with Line protocol

我正在使用 InfluxDb 并且具有如下所示的简单线路协议行:

cpu,atag=test1 idle=100,usertime=10,system=1

我有 python 客户端使用字典,如下所示

client = InfluxDBClient(host, port, USER, PASSWORD, DBNAME)
client.create_database(DBNAME)

tagdic= {'Name': 'n1', 'data': 7}
fielddic= {'Name': 'field', 'f1': 70}
def main():
    var = 1
    while var == 1 :  
     client.write("cpu,atag=test1 idle=100,usertime=10,system=1")
     #client.write_points([{"measurement": "cpu", "tags": tagdic, "fields": fielddic}])   

只要我使用 write_points 使用 write_points 和字典,上面的程序就可以正常工作,但是当我使用 client.write 时,我会遇到错误。

如何通过使用协议值 = 'line' 而不是默认协议 'json' 来使用 client.write 作为提及 here(行号 -255)?

你遇到了什么错误?这是非常重要的信息。

你得到了吗:

influxdb.exceptions.InfluxDBClientError: 400: {"error":"database is required"}

那么你应该这样写你的调用:

client.write(['cpu,atag=test1 idle=100,usertime=10,system=1'],{'db':DBNAME},204,'line')

我改变的东西:

  • 行协议字符串必须在列表中。
  • 不知何故,您需要将数据库名称添加到额外的参数中。我不确定这是临时 'bug' 还是预期的功能。
  • 您必须将协议设置为 'line'(这也会强制您设置预期的 return 代码,因为这是协议之前的位置参数)

给 ubuntu 用户的注意事项:如果您在撰写本文时使用 ubuntu 包管理器进行安装,您将获得旧版本的 python 客户端,其中写入功能接受其他论点。所以有疑问的时候用pip安装。