0%

influxDB 介绍和使用

influxDB 介绍和使用

1 简介

  • InfluxDB 是一个开源的分布式时序、事件和指标数据库,使用go语言编写,无需外部依赖,其设计目标是实现分布式和水平伸缩拓展。

2 安装

2.1 下载压缩包、解压、移动

  • 下载压缩包,
1
$ wget https://dl.influxdata.com/influxdb/releases/influxdb-1.8.9_linux_amd64.tar.gz
  • 解压,
1
$ tar xvfz influxdb-1.8.9_linux_amd64.tar.gz
  • 移动文件夹,
1
$ mv influxdb-1.8.9-1/ /opt/influxdb-1.8.9

2.2 设置环境变量

  • 在 ~/.bashrc 文件末尾添加以下内容,
1
2
export INFLUXDB=/opt/influxdb-1.8.9
export PATH=$PATH:$INFLUXDB/usr/bin
  • 使环境变量生效,
1
$ source ~/.bashrc

3 概念说明

3.1 数据库对象

influxDB 名词 传统数据库概念
database 数据库
measurement 数据表
point 数据行

3.2 point

  • influxDB 中的 point 相当于传统数据库里的一行数据,由时间戳(time)、数据(field)、标签(tag)组成。
Point 属性 传统数据库概念
time 每个数据记录时间,是数据库中的主索引
field 各种记录值(没有索引的属性),例如温度、湿度
tags 各种有索引的属性,例如地区、海拔

3.3 Series

  • Series 相当于是 InfluxDB 中一些数据的集合,在同一个 database 中,retention policy、measurement、tag sets 完全相同的数据同属于一个 series,同一个 series 的数据在物理上会按照时间顺序排列存储在一起。

4 go语言操作influxDB

4.1 安装 client

1
go get github.com/influxdata/influxdb-client-go/v2

4.2 连接数据库

  • 使用 influxDB shell 客户端创建数据库 test,
1
2
3
4
5
$ influx                                                               
Connected to http://localhost:8086 version 1.8.9
InfluxDB shell version: 1.8.9
> create database test;
>
  • client 连接数据库
1
2
3
4
5
6
7
userName := "admin"
password := ""
// Create a new client using an InfluxDB server base URL and an authentication token
// For authentication token supply a string in the form: "username:password" as a token. Set empty value for an unauthenticated server
client := influxdb2.NewClient("http://localhost:8086", fmt.Sprintf("%s:%s", userName, password))
// 不要忘记关闭连接
defer client.Close()

4.3 写入一条 point 数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Get the blocking write client
// Supply a string in the form database/retention-policy as a bucket. Skip retention policy for the default one, use just a database name (without the slash character)
// Org name is not used
writeAPI := client.WriteAPIBlocking("", "test/autogen")
// create point using full params constructor
p := influxdb2.NewPoint("stat",
map[string]string{"unit": "temperature"},
map[string]interface{}{"avg": 24.5, "max": 45},
time.Now())
// Write data
err := writeAPI.WritePoint(context.Background(), p)
if err != nil {
fmt.Printf("Write error: %s\n", err.Error())
}

4.4 查询刚被写入的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Get query client. Org name is not used
queryAPI := client.QueryAPI("")
// Supply string in a form database/retention-policy as a bucket. Skip retention policy for the default one, use just a database name (without the slash character)
result, err := queryAPI.Query(context.Background(), `from(bucket:"test")|> range(start: -1h) |> filter(fn: (r) => r._measurement == "stat")`)
if err == nil {
for result.Next() {
if result.TableChanged() {
fmt.Printf("table: %s\n", result.TableMetadata().String())
}
fmt.Printf("row: %s\n", result.Record().String())
}
if result.Err() != nil {
fmt.Printf("Query error: %s\n", result.Err().Error())
}
} else {
fmt.Printf("Query error: %s\n", err.Error())
}

4.5 influxDB shell 验证是否写入成功

1
2
3
4
5
6
7
8
9
10
$ influx                                                               
Connected to http://localhost:8086 version 1.8.9
InfluxDB shell version: 1.8.9
> use test;
Using database test
> select * from stat;
time avg max unit
---- --- --- ----
1633274191505313000 24.5 45 temperature
>
  • 1.x 版本的 influxDB shell 兼容 sql 语句,而 2.x 版本后默认使用 js 操作数据库