原文地址:https://blog.csdn.net/xz_studying/article/details/105176086
目前influxdb2.0還處於beta階段,網上的相關資料較少,根據自己的使用過程,特別整理此說明文檔。
文章目錄
一、安裝
1.下載
2.解壓
3.設置環境變量(可選)
二、啟動
1.啟動命令
2.初次使用設置
(1)使用UI界面設置
(2)使用CLI命令設置
(3)使用CLI命令快速設置
3.默認啟動端口
三、使用
1.influx部分語法說明
(1)from 指定數據源bucket
(2) |> 管道連接符
(3)range 指定起始時間段
(4)filter 過濾
(5)基於以上常用語法示例
(6)yield
2.influx命令
1.authentication token
2.命令
一、安裝
我們根據官方文檔開始:https://v2.docs.influxdata.com/v2.0/get-started/
1.下載
打開官方文檔選則平台,我這里是mac,點擊下載即可。
2.解壓
3.設置環境變量(可選)
sudo cp influxdb_2.0.0-beta.5_darwin_amd64/{influx,influxd} /usr/local/bin/
如果之前設置了1.x的路徑,由於2.0的可執行文件與1.x一致,此時可以通過替換或重命名文件實現。
Both InfluxDB 1.x and 2.x include influx and influxd binaries. If InfluxDB 1.x binaries are already in your $PATH, run the 2.0 binaries in place or rename them before putting them in your $PATH. If you rename the binaries, all references to influx and influxd in this documentation refer to your renamed binaries.
二、啟動
1.啟動命令
influxd
1
注意:macOS Catalina的版本可能需要處理安全問題,運行命令后,系統偏好設置-安全性與隱私-(左下角)允許運行
2.初次使用設置
有以下幾種方式:
(1)使用UI界面設置
訪問http://localhost:9999
點擊Get Started
按要求輸入Username、Password、Confirm Password、Organization Name、Bucket Name
點擊Continue
(2)使用CLI命令設置
influx setup
按要求依次輸入primary username、password、Confirm Password、Organization Name、Bucket Name、retention period
(3)使用CLI命令快速設置
此方法在github的readme中提到。
influx setup --username marty --password F1uxKapacit0r85 --org InfluxData --bucket telegraf --retention 168 --token where-were-going-we-dont-need-roads --force
3.默認啟動端口
influxDB 2.0默認使用9999端口,通過此端口,我們可以使用其http接口服務。influxDB 1.x默認使用8086端口。
三、使用
官網教程並不完善,需要結合其github項目的readme來使用。
1.influx部分語法說明
特別注意:influxDB 2.0版本相對1.x版本改動較大,尤其是語法方面的改動,2.0版本的語法使用的是JavaScript,1.x使用的是sql。
Flux design principles
Flux is designed to be usable, readable, flexible, composable, testable, contributable, and shareable.
Its syntax is largely inspired by 2018’s most popular scripting language, Javascript, and takes a functional approach to data exploration and processing.
示例如下:
from(bucket:"example-bucket")
|> range(start:-1h)
|> filter(fn:(r) =>
r._measurement == "cpu" and
r.cpu == "cpu-total"
)
|> aggregateWindow(every: 1m, fn: mean)
1
2
3
4
5
6
7
(1)from 指定數據源bucket
from(bucket:“example-bucket”)
(2) |> 管道連接符
將數據從數據源管道傳輸到指定地方,如range()
(3)range 指定起始時間段
range有兩個參數start,stop,stop不設置默認為當前。range可以是相對的(使用負持續時間)或絕對(使用時間段)。
// Relative time range with start only. Stop defaults to now.
from(bucket:"example-bucket")
|> range(start: -1h)
// Relative time range with start and stop
from(bucket:"example-bucket")
|> range(start: -1h, stop: -10m)
// Absolute time range with start and stop
from(bucket:"example-bucket")
|> range(start: 2020-03-02T01:00:00Z)
1
2
3
4
5
6
7
8
9
10
11
(4)filter 過濾
對range()中的數據進行過濾,filter()有一個參數fn,是基於列和屬性過濾數據邏輯的匿名函數。
flux的匿名函數語法與JavaScript的語法類似。記錄或行在filter()中作為對象®。多個過濾規則間用and連接。
語法如下:
// Pattern
(r) => (r.objectProperty comparisonOperator comparisonExpression)
1
2
示例如下:
// Example with single filter
(r) => (r._measurement == "cpu")
// Example with multiple filters
(r) => (r._measurement == "cpu") and (r._field != "usage_system" )
1
2
3
4
5
(5)基於以上常用語法示例
一個完整的查詢示例如下:
from(bucket:"example-bucket")
|> range(start: -15m)
|> filter(fn: (r) =>
r._measurement == "cpu" and
r._field == "usage_system" and
r.cpu == "cpu-total"
)
1
2
3
4
5
6
7
表示:查詢example-bucket最近15分鍾cpu相關數據。
(6)yield
flux的yield()函數作為查詢結果輸出過濾的tables。
from(bucket:"example-bucket")
|> range(start: -15m)
|> filter(fn: (r) =>
r._measurement == "cpu" and
r._field == "usage_system" and
r.cpu == "cpu-total"
)
|> yield()
1
2
3
4
5
6
7
8
為了輸出和可視化數據,Flux在每個腳本的末尾自動添加一個yield()函數。只有在同一個流量查詢中包含多個查詢時,才需要顯式調用yield()。每一組返回的數據都需要使用yield()函數來命名。
2.influx命令
1.authentication token
InfluxDB使用authentication tokens來確保用戶和數據間的安全交互。
(1)生成token
在UI界面設置:
登錄UI界面后,點擊側邊欄Load Data
點擊Tokens,點擊Generate,選擇token類型
添加描述,點擊Save即可
CLI命令設置:
# Syntax
influx auth create -o <org-name> [permission-flags]
# Example
influx auth create -o my-org </br>
--read-buckets 03a2bbf46309a000 03ace3a87c269000 \
--read-dashboards \
--read-tasks \
--read-telegrafs \
--read-user
1
2
3
4
5
6
7
8
9
10
(2)查看token
可以在http://localhost:9999/orgs/{org_id}/load-data/tokens界面查看,如下:
點擊root’s token即可看到token。
將token保存至文件 ~/.influxdbv2/credentials中,后續influx的命令默認讀取此token。
當然,也可以通過cli設置token。
2.命令
1.查找organization ID and bucket ID
(1)查找organization ID
influx org find
(2)查找bucket ID
項目readme中為
influx bucket find 此命令其實會報錯Error: Must specify org-id, or org name.
查看help:
influx bucket find -h
實際查詢bucket id的命令為:
influx bucket find -o org_name
3.write
(1)使用命令
influx write --org InfluxData --bucket telegraf --precision s “m v=2 $(date +%s)”
(2)使用http接口
curl --header “Authorization: Token $(cat ~/.influxdbv2/credentials)” --data-raw “m v=2 $(date +%s)” “http://localhost:9999/api/v2/write?org=InfluxData&bucket=telegraf&precision=s”
(3)使用influxDB的client
根據語言選擇對應版本的client
4.read
influx query -o InfluxData ‘from(bucket:“telegraf”) |> range(start:-1h)’
5.使用REPL
連接后不用反復使用influx的命令
(1)連接
influx repl -o InfluxData
(2)read
from(bucket:“telegraf”) |> range(start:-1h)
實際上官網的教程也是從使用REPL開始的,如果沒有連接,后續的所有使用都無法進行。
————————————————
版權聲明:本文為CSDN博主「yuchenfw」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/xz_studying/article/details/105176086