Inserting data into Influxdb using Python client with Line protocol

Ammad picture Ammad · Oct 19, 2016 · Viewed 8.5k times · Source

I am using InfluxDb and have simple line protocol line as shown below:

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

I have python client using dictionary as shown below

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}])   

Above program is working fine as long as I am using write_points using write_points and dictionary, but when i am using client.write i am getting errors.

How can i use client.write as mention here (line number -255) by using protocol value = 'line' instead of default protocol 'json'?

Answer

Sebastian picture Sebastian · Feb 17, 2017

What errors are you getting? That is quite essential information.

Are you getting:

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

Then you should write your call like this:

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

Things I changed:

  • The line protocol string(s) must be inside a list.
  • Somehow you need to add the database name to the extra params. I am not sure if this is a temporary 'bug' or intended feature.
  • You must set the protocol to 'line' (which also forces you to set the expected return code since that is the positional argument before the protocol)

A note for ubuntu users: if you install with the ubuntu package manager at the time of writing you will get an older version of the python client where the write function takes other arguments. So install with pip when in doubt.