JSON数据格式写入InfluxDB错误导致204响应

问题描述 投票:0回答:1

我正在从天气API中请求一些数据,并试图将其保存到influxdb数据库中。

当我试图将json数据写入influx数据库时,我得到一个204的响应,并且没有数据被写入数据库。从docker日志中得到的确切错误是:。

[httpd] 172.17.0.1 - root [17/Jun/2020:23:00:35 +0000] "POST /write?db=external_weather HTTP/1.1" 204 0 "-" "python-requests/2.23.0" 5ac727b3-b0ee-11ea-8152-0242ac110002 169

我的代码是:

import requests
from requests.exceptions import HTTPError
from influxdb import InfluxDBClient

url = "http://api.openweathermap.org/data/2.5/weather"
payload = {
    "lat": 51.509865,
    "lon": -0.118092,
    # "exclude": {"minute", "hourly", "daily"},
    "appid": "xxx",
    "units": "metric"}


try:  

    responce = requests.get(url, params=payload)
    responce.raise_for_status()
    json_responce = [responce.json()]
    print(responce.status_code)


except HTTPError as http_err:
    print(f'HTTP error occurred: {http_err}')

except Exception as err:
    print(f'Other error occurred: {err}')


# Send to InfluxDB


'''
'''
client = InfluxDBClient()
client.write_points(json_responce, database='external_weather')
print(json_responce)

json_responce包含以下信息。

[{'coord': {'lon': -0.12, 'lat': 51.51}, 'weather': [{'id': 500, 'main': 'Rain', 'description': 'light rain', 'icon': '10n'}], 'base': 'stations', 'main': {'temp': 16.14, 'feels_like': 15.39, 'temp_min': 15.56, 'temp_max': 16.67, 'pressure': 1012, 'humidity': 71}, 'wind': {'speed': 1.49, 'deg': 143}, 'rain': {'1h': 0.9}, 'clouds': {'all': 100}, 'dt': 1592435463, 'sys': {'type': 3, 'id': 268730, 'country': 'GB', 'sunrise': 1592451762, 'sunset': 1592511641}, 'timezone': 3600, 'id': 2643743, 'name': 'London', 'cod': 200}]

我不明白为什么influx拒绝接受数据。

python influxdb
1个回答
0
投票

根据文档中的 InfluxDBClientజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజ 204 在向influx DB中写入数据时,实际上是需要响应代码的,请参见 https:/influxdb-python.readthedocs.ioenlatestapi-documentation.html#influxdb.InfluxDBClient.write。

请仔细检查你的情况是否真的存在将数据写入influx的问题。

© www.soinside.com 2019 - 2024. All rights reserved.