I'm trying to insert JSON files to influxDB database with a python script.
All my scripts are containerized via Docker.
Each time I'm trying to open the connection to insert json logs tell me that 'InfluxDBClient' object has no attribute 'api_client'
I tried a lot of different configuration, here is the actual one:
from influxdb_client import InfluxDBClient, Point, WriteOptions
from dotenv import load_dotenv
import json
import os
class DB():
# Method to open connection
@classmethod
def open_con(cls):
load_dotenv()
cls.token = os.getenv('INFLUXDB_V2_TOKEN')
cls.org = os.getenv('INFLUXDB_V2_ORG')
cls.bucket = os.getenv('INFLUXDB_V2_BUCKET')
cls.url = "http://influxdb:8086"
cls.client = InfluxDBClient(url=cls.url, token=cls.token, org=cls.org)
print('Connected')
cls.write_api = cls.client.write_api(write_options=WriteOptions(batch_size=500, flush_interval=10_000, jitter_interval=2_000, retry_interval=5_000, max_retries=5, max_retry_delay=30_000, exponential_base=2))
# Method to close connection
@classmethod
def close_con(cls):
cls.client.close()
# Method to insert JSON
@classmethod
def insert_json(cls, file):
cls.open_con()
with open(file) as f:
file_data = json.load(f)
cls.write_api.write(bucket=cls.bucket, org=cls.org, record=file_data)
cls.close_con()
print(f'Json inséré')
This is my Docker compose file:
version: "3.8"
services:
buffer:
image: buffer:1.0
build:
context: ./insert
dockerfile: Dockerfile
container_name: buffer
restart: unless-stopped
volumes:
- ./json_files:/app/json_files
depends_on:
- influxdb
influxdb:
image: influxdb
container_name: influxdb
restart: always
ports:
- "8086:8086"
volumes:
- influxdbv2:/.influxdbv2
influxdb_cli:
links:
- influxdb
image: influxdb:1.7
container_name: influxdb_cli
entrypoint: influx setup --bucket ${INFLUXDB_V2_BUCKET} -t ${INFLUXDB_V2_TOKEN} -o ${INFLUXDB_V2_ORG} --username=${INFLUXDB_USERNAME} --password=${INFLUXDB_PASSWORD} --host=http://influxdb:8086 -f
restart: on-failure:10
depends_on:
- influxdb
volumes:
influxdbv2:
And finally this is the json format i'm trying to insert:
[
{
"measurement": "BTC @ 14/03/2021 16:52:33",
"tags": {
"crypto_name": "Bitcoin",
"crypto_symbol": "BTC"
},
"time": "14/03/2021 16:52:33",
"fields": {
"name": "Bitcoin",
"symbol": "BTC",
"currency": "$",
"value": 59623.19,
"market_cap": 1110000000000,
"volume": 17680000000}
}, ...
]
here is the message given by Docker logs buffer (buffer is the script that try to insert json to influxdb):
Traceback (most recent call last):
File "./app/buffer.py", line 5, in <module>
from influxdb_connection import DB
File "/app/influxdb_connection.py", line 36, in <module>
DB.open_con()
File "/app/influxdb_connection.py", line 17, in open_con
cls.client = InfluxDBClient(url=cls.url, token=cls.token, org=cls.org)
File "/usr/local/lib/python3.8/site-packages/influxdb_client/client/influxdb_client.py", line 62, in __init__
auth_header_value = "Token " + auth_token
TypeError: can only concatenate str (not "NoneType") to str
Exception ignored in: <function InfluxDBClient.__del__ at 0x7f064f97b040>
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/influxdb_client/client/influxdb_client.py", line 171, in __del__
if self.api_client:
AttributeError: 'InfluxDBClient' object has no attribute 'api_client'
EDIT:
I added this line to my docker compose.
...
env_file:
- .env
...
.env is the name of my environment file.
INFLUXDB_V2_TOKENenvironment variable is spelled correctly etc.. Since you're using docker-compose, you can try the "environment" config..envfile but It's the same token that init theinfluxdb_cliin my Docker compose so I don't know why it would be a problem.env_file: - .envto my script in my docker compose and it worked very well !! Thanks a lot for your help and your time. @Hitobat