问题描述
我正在尝试使用 python 脚本将 JSON 文件插入到 influxDB 数据库中。
我所有的脚本都是通过 Docker 容器化的。
每次我尝试打开连接以插入 json 日志时都告诉我 'InfluxDBClient' object has no attribute 'api_client'
我尝试了很多不同的配置,这是实际的配置:
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é')
这是我的 Docker compose
文件:
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:
最后这是我要插入的 json 格式:
[
{
"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}
},...
]
这是Docker logs buffer
给出的消息(缓冲区是尝试将json插入到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,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'
编辑:
我将此行添加到我的 docker compose 中。
...
env_file:
- .env
...
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)