pythoninfluxdb-python

Python Write to Influx V1.8+


I have implemented a python script to import data to some influx databases. The data is written as line protocol. One instance of influx is v2.7, the write of data works, so I guess my line protocol data is O.K.

The other instance of influx is v.1.8.10. I can connect, create a database, switch to that database, but the write always fails with "Bad request"

"POST /write HTTP/1.1 " 400 28 

When I do it by hand using the following curl command

 curl -i -XPOST 'http://127.0.0.1:8086/write?db=testdb' --data-binary 'measurement,tag1=first,tag2=second raw=1i,physical=1.0 1699436519000'

I get as response

"POST /write?db=testdb HTTP/1.1 " 204 0

I have actually no clue, what the problem is.

Here is my implementation of the client for writing to the database

''' Client for influxDB1. '''
import logging
import json

from influxdb import InfluxDBClient

class Influx1Writer:
    ''' Client to access influxDB1. '''

    def __init__(self, dbname) -> None:
        ''' Initializer, will be called from constructor. '''
        self.logger = logging.getLogger(__name__ + '.' + __class__.__name__)
        self.host = ''
        self.port = ''
        self.user = ''
        self.password = ''
        self.usessl = False

        try:
            with open('influx_1_secret.json', 'r', encoding='utf-8') as fin:
                content = json.load(fin)
                self.host = content['host']
                self.port = content['port']
                self.user = content['user']
                self.password = content['password']

            if not self.host:
                self.logger.error('file influx_1_secret.json, must contain an "host" element')
                return

            if not self.port:
                self.logger.error('file influx_1_secret.json must contain a "port" element')
                return

            if not self.user:
                self.logger.error('file influx_1_secret.json must contain an "user" element')
                return

            if not self.password:
                self.logger.error('file influx_1_secret.json must contain an "password" element')
                return

            self.client = InfluxDBClient(
                host=self.host,
                port=self.port,
                username=self.user,
                password=self.password,
                database=dbname,
                ssl=self.usessl)
            # create database in case it does not exists, otherwise influx will do nothing
            self.client.create_database(dbname)
            self.client.switch_database(dbname)
            self.client.switch_user(self.user, self.password)
        except FileNotFoundError:
            self.logger.error('file influx_1_secret.json not found')

    def write(self, records) -> None:
        ''' Write a batch of records to influxDB V1. '''
        if records:
            self.logger.info('.write: Amount of records is %s', len(records))
            response = self.client.write(data=records, protocol='line')
            if not response:
                self.logger.error('.write: Writing of records failed')
            else:
                self.logger.info('.write: Records written sucessfully')

Maybe somebody can tell where the problem is - thanks for any hints in advance


Solution

  • I found the problem. It lies deep in the influxdb_client package V5.3.1 (last version ever).

    In this version the query goes to

    /write
    

    instead of

    /write?db=testdb
    

    After fixing this (which may break things I don't need) in the package it worked.