influxDB 2.0安装及使用说明

原文地址: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

原文地址:https://www.cnblogs.com/eyesfree/p/15411977.html