av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

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

譯者 | 布加迪

創(chuàng)新互聯(lián)專(zhuān)注于雅安網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供雅安營(yíng)銷(xiāo)型網(wǎng)站建設(shè),雅安網(wǎng)站制作、雅安網(wǎng)頁(yè)設(shè)計(jì)、雅安網(wǎng)站官網(wǎng)定制、小程序開(kāi)發(fā)服務(wù),打造雅安網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供雅安網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。

審校 | 孫淑娟

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

我們會(huì)將來(lái)自API調(diào)用的JSON響應(yīng)轉(zhuǎn)換成Pandas DataFrame,因?yàn)檫@是將數(shù)據(jù)寫(xiě)入到InfluxDB的最簡(jiǎn)單方法。由于InfluxDB是一個(gè)專(zhuān)門(mén)構(gòu)建的數(shù)據(jù)庫(kù),我們寫(xiě)入到InfluxDB旨在滿(mǎn)足時(shí)間序列數(shù)據(jù)在攝取方面的高要求。

要求

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

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

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

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

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

請(qǐng)求天氣數(shù)據(jù)

首先,我們需要請(qǐng)求數(shù)據(jù)。我們將使用請(qǐng)求庫(kù),通過(guò)OpenWeatherMap API從指定的經(jīng)度和緯度返回每小時(shí)的天氣數(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ù)轉(zhuǎn)換成Pandas DataFrame

接下來(lái),將JSON數(shù)據(jù)轉(zhuǎn)換成Pandas DataFrame。我們還將時(shí)間戳從秒精度的Unix時(shí)間戳轉(zhuǎn)換成日期時(shí)間對(duì)象。之所以進(jìn)行這種轉(zhuǎn)換,是由于InfluxDB寫(xiě)入方法要求時(shí)間戳為日期時(shí)間對(duì)象格式。接下來(lái),我們將使用這種方法,將數(shù)據(jù)寫(xiě)入到InfluxDB。我們還刪除了不想寫(xiě)入到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寫(xiě)入到InfluxDB

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

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

由于我們沒(méi)有將任何列指定為標(biāo)簽,我們的所有列都將轉(zhuǎn)換成InfluxDB中的字段。標(biāo)簽用于寫(xiě)入有關(guān)您的時(shí)間序列數(shù)據(jù)的元數(shù)據(jù),可用于更有效地查詢(xún)數(shù)據(jù)子集。字段是您在 InfluxDB中存儲(chǔ)實(shí)際時(shí)間序列數(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)更詳細(xì)地介紹了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. 導(dǎo)入庫(kù)。

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

  • InfluxDB存儲(chǔ)桶
  • InfluxDB組織
  • InfluxDB令牌
  • InfluxDB URL
  • OpenWeatherMap URL
  • OpenWeatherMap 令牌

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

4. 將JSON響應(yīng)轉(zhuǎn)換成Pandas DataFrame。

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

6. 將時(shí)間戳列從Unix時(shí)間轉(zhuǎn)換成Pandas日期時(shí)間對(duì)象。

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

8. 編寫(xiě)DataFrame,并指定測(cè)量名稱(chēng)和時(shí)間戳列。

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

查詢(xún)數(shù)據(jù)

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

圖1. 天氣數(shù)據(jù)的默認(rèn)物化視圖。InfluxDB自動(dòng)聚合時(shí)間序列數(shù)據(jù),這樣新用戶(hù)就不會(huì)意外查詢(xún)太多數(shù)據(jù)而導(dǎo)致超時(shí)

專(zhuān)業(yè)提示:當(dāng)您使用查詢(xún)構(gòu)建器查詢(xún)數(shù)據(jù)時(shí),InfluxDB自動(dòng)對(duì)數(shù)據(jù)進(jìn)行下采樣。要查詢(xún)?cè)紨?shù)據(jù),導(dǎo)航到Script Editor(腳本編輯器)以查看底層Flux查詢(xún)。Flux是面向InfluxDB的原生查詢(xún)和腳本語(yǔ)言,可用于使用您的時(shí)間序列數(shù)據(jù)來(lái)分析和創(chuàng)建預(yù)測(cè)。使用aggregateWindow()函數(shù)取消行注釋或刪除行,以查看原始數(shù)據(jù)。

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

結(jié)語(yǔ)

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


當(dāng)前文章:用Python獲取和存儲(chǔ)時(shí)間序列數(shù)據(jù)
網(wǎng)頁(yè)鏈接:http://uogjgqi.cn/article/cojdjdg.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流