成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

用Python獲取和存儲時間序列數(shù)據(jù)

譯文
開發(fā) 后端
我們會將來自API調(diào)用的JSON響應轉換成Pandas DataFrame,因為這是將數(shù)據(jù)寫入到InfluxDB的最簡單方法。由于InfluxDB是一個專門構建的數(shù)據(jù)庫,我們寫入到InfluxDB旨在滿足時間序列數(shù)據(jù)在攝取方面的高要求。

譯者 | 布加迪

審校 | 孫淑娟

本教程將介紹如何使用Python從OpenWeatherMap API獲取時間序列數(shù)據(jù),并將其轉換成Pandas DataFrame。接下來,我們將使用InfluxDB Python Client,將該數(shù)據(jù)寫入到時間序列數(shù)據(jù)平臺InfluxDB。

我們會將來自API調(diào)用的JSON響應轉換成Pandas DataFrame,因為這是將數(shù)據(jù)寫入到InfluxDB的最簡單方法。由于InfluxDB是一個專門構建的數(shù)據(jù)庫,我們寫入到InfluxDB旨在滿足時間序列數(shù)據(jù)在攝取方面的高要求。

要求

本教程在通過Homebrew已安裝Python 3的macOS系統(tǒng)上完成。建議安裝額外的工具,比如virtualenv、pyenv或conda-env,以簡化Python和Client的安裝。完整的要求在這里:

txt
influxdb-client=1.30.0
pandas=1.4.3
requests>=2.27.1

本教程還假設您已經(jīng)創(chuàng)建Free Tier InfluxDB云帳戶或正在使用InfluxDB OSS,您也已經(jīng):

  • 創(chuàng)建了存儲桶。您可以將存儲桶視為數(shù)據(jù)庫或InfluxDB中最高層次的數(shù)據(jù)組織。
  • 創(chuàng)建了令牌。

最后,該教程要求您已經(jīng)使用OpenWeatherMap創(chuàng)建了一個帳戶,并已創(chuàng)建了令牌。

請求天氣數(shù)據(jù)

首先,我們需要請求數(shù)據(jù)。我們將使用請求庫,通過OpenWeatherMap API從指定的經(jīng)度和緯度返回每小時的天氣數(shù)據(jù)。

# 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']

將數(shù)據(jù)轉換成Pandas DataFrame

接下來,將JSON數(shù)據(jù)轉換成Pandas DataFrame。我們還將時間戳從秒精度的Unix時間戳轉換成日期時間對象。之所以進行這種轉換,是由于InfluxDB寫入方法要求時間戳為日期時間對象格式。接下來,我們將使用這種方法,將數(shù)據(jù)寫入到InfluxDB。我們還刪除了不想寫入到InfluxDB的列。

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)

將Pandas DataFrame寫入到InfluxDB

現(xiàn)在為InfluxDB Python客戶端庫創(chuàng)建實例,并將DataFrame寫入到InfluxDB。我們指定了測量名稱。測量含有存儲桶中的數(shù)據(jù)。您可以將其視為InfluxDB的數(shù)據(jù)組織中僅次于存儲桶的第二高層次結構。

您還可以使用data_frame__tag_columns參數(shù)指定將哪些列轉換成標簽。

由于我們沒有將任何列指定為標簽,我們的所有列都將轉換成InfluxDB中的字段。標簽用于寫入有關您的時間序列數(shù)據(jù)的元數(shù)據(jù),可用于更有效地查詢數(shù)據(jù)子集。字段是您在 InfluxDB中存儲實際時間序列數(shù)據(jù)的位置。該文檔(https://docs.influxdata.com/influxdb/cloud/reference/key-concepts/?utm_source=vendor&utm_medium=referral&utm_campaign=2022-07_spnsr-ctn_obtaining-storing-ts-pything_tns)更詳細地介紹了InfluxDB中的這些數(shù)據(jù)概念。

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")

完整腳本

回顧一下,不妨看看完整的腳本。 我們采取以下步驟:

1. 導入庫。

2. 收集以下內(nèi)容:

  • InfluxDB存儲桶
  • InfluxDB組織
  • InfluxDB令牌
  • InfluxDB URL
  • OpenWeatherMap URL
  • OpenWeatherMap 令牌

3. 創(chuàng)建請求。

4. 將JSON響應轉換成Pandas DataFrame。

5. 刪除您不想寫入到InfluxDB的任何列。

6. 將時間戳列從Unix時間轉換成Pandas日期時間對象。

7. 為InfluxDB Python Client庫創(chuàng)建實例。

8. 編寫DataFrame,并指定測量名稱和時間戳列。

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")

查詢數(shù)據(jù)

現(xiàn)在,我們已經(jīng)將數(shù)據(jù)寫入到InfluxDB,可以使用InfluxDB UI來查詢數(shù)據(jù)了。導航到數(shù)據(jù)資源管理器(從左側導航欄中)。使用Query Builder(查詢構建器),選擇想要可視化的數(shù)據(jù)和想要為之可視化的范圍,然后點擊“提交”。

圖1. 天氣數(shù)據(jù)的默認物化視圖。InfluxDB自動聚合時間序列數(shù)據(jù),這樣新用戶就不會意外查詢太多數(shù)據(jù)而導致超時

專業(yè)提示:當您使用查詢構建器查詢數(shù)據(jù)時,InfluxDB自動對數(shù)據(jù)進行下采樣。要查詢原始數(shù)據(jù),導航到Script Editor(腳本編輯器)以查看底層Flux查詢。Flux是面向InfluxDB的原生查詢和腳本語言,可用于使用您的時間序列數(shù)據(jù)來分析和創(chuàng)建預測。使用aggregateWindow()函數(shù)取消行注釋或刪除行,以查看原始數(shù)據(jù)。

圖2. 導航到腳本編輯器,并取消注釋或刪除aggregateWindow()函數(shù),以查看原始天氣數(shù)據(jù)

結語

但愿本文能幫助您充分利用InfluxDB Python Client庫,獲取時間序列數(shù)據(jù)并存儲到InfluxDB中。如果您想進一步了解使用Python Client庫從InfluxDB查詢數(shù)據(jù),建議您看看這篇文章(https://thenewstack.io/getting-started-with-python-and-influxdb/)。另外值得一提的是,您可以使用Flux從OpenWeatherMap API獲取數(shù)據(jù),并將其存儲到InfluxDB。如果您使用InfluxDB Cloud,這意味著該Flux腳本將被托管和定期執(zhí)行,因此您可以獲得可靠的天氣數(shù)據(jù)流,并饋入到實例中。想進一步了解如何使用Flux按用戶定義的時間表獲取天氣數(shù)據(jù),請閱讀這篇文章(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)。

責任編輯:姜華 來源: 51CTO技術棧
相關推薦

2020-02-18 16:07:17

物聯(lián)網(wǎng)表存儲數(shù)據(jù)庫

2020-10-27 10:13:06

Python時間序列代碼

2021-09-22 14:49:11

時間序列數(shù)據(jù)分析數(shù)據(jù)數(shù)據(jù)庫

2018-12-17 12:12:43

Netflix數(shù)據(jù)庫存儲

2023-01-05 16:36:55

2021-04-07 10:02:00

XGBoostPython代碼

2023-11-20 08:44:18

數(shù)據(jù)序列化反序列化

2017-03-15 15:45:33

MySQL存儲引擎設計與實現(xiàn)

2013-05-03 13:42:20

iOS開發(fā)SQLite3存儲讀取

2021-03-31 11:20:57

PythonADTK異常檢測

2023-03-30 15:12:47

2023-01-28 16:13:08

InfluxData開源

2024-09-09 14:57:31

2023-02-16 17:44:13

2022-08-16 09:00:00

機器學習人工智能數(shù)據(jù)庫

2016-12-05 18:32:08

序列化androidjava

2023-08-14 16:51:51

傅里葉變換時間序列去趨勢化

2021-07-02 10:05:45

PythonHot-winters指數(shù)平滑

2021-07-01 21:46:30

PythonHot-Winters數(shù)據(jù)

2024-05-08 14:05:03

時間序列數(shù)據(jù)
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 无码国模国产在线观看 | 国产国拍亚洲精品av | 欧美一区二区三区视频在线播放 | 中文字幕一区二区在线观看 | 婷婷综合色| 日韩精品一区二区三区四区视频 | 午夜精品网站 | 亚洲影音先锋 | www免费视频 | 日韩精品一区二区在线 | 九九热精品视频 | 久久黄视频 | 久久国产精品精品国产色婷婷 | av一区二区三区四区 | 人人爱干 | 久久蜜桃av一区二区天堂 | 亚洲一区二区三区观看 | 久久视频精品在线 | 一区二区三区免费 | 国产高清在线精品一区二区三区 | 国产一区91精品张津瑜 | 一本久久a久久精品亚洲 | www.久久.com| 亚洲欧美一区二区三区1000 | 国产精品爱久久久久久久 | 久优草| 特级黄一级播放 | 在线观看视频你懂得 | 成年免费大片黄在线观看一级 | 一级毛片大全免费播放 | 久久亚洲欧美日韩精品专区 | 日美女逼逼| 毛片网站在线观看视频 | 综合九九 | 成人国产精品免费观看视频 | 久久影音先锋 | 91福利网址 | 91久久精品一区二区二区 | 亚洲一区 中文字幕 | 久久99精品久久 | 中文字幕在线观看 |