influxDB 2.0安裝及使用說明

目前influxdb2.0還處於beta階段,網上的相關資料較少,根據自己的使用過程,特別整理此說明文檔。

一、安裝

我們根據官方文檔開始:https://v2.docs.influxdata.com/v2.0/get-started/

1.下載

influxDB下載頁
打開官方文檔選則平臺,我這裏是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

注意: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)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)

(4)filter 過濾

對range()中的數據進行過濾,filter()有一個參數fn,是基於列和屬性過濾數據邏輯的匿名函數。
flux的匿名函數語法與JavaScript的語法類似。記錄或行在filter()中作爲對象®。多個過濾規則間用and連接。
語法如下:

// Pattern
(r) => (r.objectProperty comparisonOperator comparisonExpression)

示例如下:

// Example with single filter
(r) => (r._measurement == "cpu")

// Example with multiple filters
(r) => (r._measurement == "cpu") and (r._field != "usage_system" )

(5)基於以上常用語法示例

一個完整的查詢示例如下:

from(bucket:"example-bucket")
  |> range(start: -15m)
  |> filter(fn: (r) =>
    r._measurement == "cpu" and
    r._field == "usage_system" and
    r.cpu == "cpu-total"
  )

表示:查詢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()

爲了輸出和可視化數據,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

(2)查看token

可以在http://localhost:9999/orgs/{org_id}/load-data/tokens界面查看,如下:
influxDB token
點擊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開始的,如果沒有連接,後續的所有使用都無法進行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章