譯者| 布加迪
#審查學校| 孫淑娟
本教學將介紹如何使用Python從OpenWeatherMap API取得時間序列數據,並將其轉換成Pandas DataFrame。接下來,我們將使用InfluxDB Python Client,將資料寫入到時間序列資料平台InfluxDB。
我們會將來自API呼叫的JSON回應轉換成Pandas DataFrame,因為這是將資料寫入到InfluxDB的最簡單方法。由於InfluxDB是一個專門建構的資料庫,我們寫入到InfluxDB旨在滿足時間序列資料在攝取方面的高需求。
本教學在透過Homebrew已安裝Python 3的macOS系統上完成。建議安裝額外的工具,例如virtualenv、pyenv或conda-env,以簡化Python和Client的安裝。完整的要求在這裡:
txt influxdb-client=1.30.0 pandas=1.4.3 requests>=2.27.1
本教學也假設您已經建立Free Tier InfluxDB雲端帳戶或正在使用InfluxDB OSS,您也已經:
# Get time series data from OpenWeatherMap API params = {'lat':openWeatherMap_lat, 'lon':openWeatherMap_lon, 'exclude': "minutely,daily", 'appid':openWeatherMap_token} r = requests.get(openWeather_url, params = params).json() hourly = r['hourly']
python # Convert data to Pandas DataFrame and convert timestamp to datetime object df = pd.json_normalize(hourly) df = df.drop(columns=['weather', 'pop']) df['dt'] = pd.to_datetime(df['dt'], unit='s') print(df.head)
on # Write data to InfluxDB with InfluxDBClient(url=url, token=token, org=org) as client: df = df client.write_api(write_options=SYNCHRONOUS).write(bucket=bucket,record=df, data_frame_measurement_name="weather", data_frame_timestamp_column="dt")
python import requests import influxdb_client import pandas as pd from influxdb_client import InfluxDBClient from influxdb_client.client.write_api import SYNCHRONOUS bucket = "OpenWeather" org = "" # or email you used to create your Free Tier InfluxDB Cloud account token = " url = "" # for example, https://us-west-2-1.aws.cloud2.influxdata.com/ openWeatherMap_token = "" openWeatherMap_lat = "33.44" openWeatherMap_lon = "-94.04" openWeather_url = "https://api.openweathermap.org/data/2.5/onecall" # Get time series data from OpenWeatherMap API params = {'lat':openWeatherMap_lat, 'lon':openWeatherMap_lon, 'exclude': "minutely,daily", 'appid':openWeatherMap_token} r = requests.get(openWeather_url, params = params).json() hourly = r['hourly'] # Convert data to Pandas DataFrame and convert timestamp to datetime object df = pd.json_normalize(hourly) df = df.drop(columns=['weather', 'pop']) df['dt'] = pd.to_datetime(df['dt'], unit='s') print(df.head) # Write data to InfluxDB with InfluxDBClient(url=url, token=token, org=org) as client: df = df client.write_api(write_options=SYNCHRONOUS).write(bucket=bucket,record=df, data_frame_measurement_name="weather", data_frame_timestamp_column="dt")
圖2. 導覽至腳本編輯器,並取消註解或刪除aggregateWindow()函數,以查看原始天氣資料
但願本文能幫助您充分利用InfluxDB Python Client庫,取得時間序列資料並儲存於InfluxDB中。如果您想進一步了解使用Python Client函式庫從InfluxDB查詢數據,建議您看看這篇文章(https://thenewstack.io/getting-started-with-python-and-influxdb/)。另外值得一提的是,您可以使用Flux從OpenWeatherMap API取得數據,並將其儲存到InfluxDB。如果您使用InfluxDB Cloud,這表示該Flux腳本將被託管並定期執行,因此您可以獲得可靠的天氣資料流,並饋入到執行個體中。想進一步了解如何使用Flux依照使用者定義的時間表來取得天氣數據,請閱讀這篇文章(https://www.influxdata.com/blog/tldr-influxdb-tech-tips-handling-json-objects-mapping- arrays/?utm_source=vendor&utm_medium=referral&utm_campaign=2022-07_spnsr-ctn_obtaining-storing-ts-pything_tns)。
以上是用Python獲取和儲存時間序列數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!