安装:
//拉取镜像docker pull influxdb:1.8.0//运行docker run -dp 0.0.0.0:8083->8083/tcp, 0.0.0.0:8086->8086/tcp --name influxdb influxdb:1.8.0
8083端口用于客户端的连接端口 8086端口用于rpc服务执行备份和恢复操作。
基本使用
#使用docker安装,进入容器内部docker exec -it 容器Id bash#控制台输入命令,进入influx制台命令行模式influx -precision rfc3339#上面的命令解释 -precision代表使用的时间格式是rfc3339 一种标准的时间格式 YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ#不指定默认使用纳秒时间戳,其他可用的格式是h, m, s, ms, u or ns#使用帮助命令查看其他可选项root@823b110209fa:/# influx --helpUsage of influx:-version当前influxdb的版本-path-prefix 'url path'Path that follows the host and port-host 'host name'需要连接的主机名,默认是使用本机-port 'port #'端口号 默认8086-socket 'unix domain socket'Unix socket to connect to.-database 'database name'使用的数据库名-password 'password'连接的密码,如果指定,默认密码为空字符串''-username 'username'如连接设置了账号密码,此选项输入账号-sslUse https for requests.-unsafeSslSet this when connecting to the cluster using https and not use SSL verification.-execute 'command'指定要执行的命令-type 'influxql|flux'Type specifies the query language for executing commands or when invoking the REPL.-format 'json|csv|column'指定查询出来的数据的格式json格式 csv格式和列表格式-precision 'rfc3339|h|m|s|ms|u|ns'指定时间显示格式-consistency 'any|one|quorum|all'Set write consistency level: any, one, quorum, or all-pretty当使用json格式的时候,是否开始格式化-importImport a previous database export from file-ppsHow many points per second the import will allow. By default it is zero and will not throttle importing.-pathPath to file to import-compressedSet to true if the import file is compressedExamples:# Use influx in a non-interactive mode to query the database "metrics" and pretty print json:$ influx -database 'metrics' -execute 'select * from cpu' -format 'json' -pretty# Connect to a specific database on startup and set database context:$ influx -database 'metrics' -host 'localhost' -port '8086'
#创建数据库 没有提示任何错误消息就是代表正常执行了CREATE DATABASE mydb#查看数据库SHOW DATABASES#使用数据库Using database mydb#插入数据的格式insert <表名>[,<tag-key>=<tag-value>...] 空格隔开 <field-key>=<field-value>[,<field2-key>=<field2-value>...] 空格隔开 [unix-nano-timestamp]#例如 在插入数据的时候可以不提供时间戳,会自动使用系统的当前时间INSERT cpu,host=serverA,region=us_west value=0.64INSERT temperature,machine=unit42,type=assembly external=25,internal=37#查询SELECT "host", "region", "value" FROM "cpu"#在大型数据库上使用不带 LIMIT 子句的 * 可能会导致性能问题。您可以使用 Ctrl+C 取消响应时间过长的查询SELECT * FROM "temperature"#表名使用正则进行查询SELECT * FROM /.*/ LIMIT 1
关键概念
#模拟数据#使用mydb数据库use mydb#插入数据insert census,location=1,scientist=langstroth butterflies=12,honeybees=23insert census,location=1,scientist=perpetua butterflies=1,honeybees=30insert census,location=1,scientist=langstroth butterflies=11,honeybees=28insert census,location=1,scientist=perpetua butterflies=3,honeybees=28insert census,location=2,scientist=langstroth butterflies=2,honeybees=11insert census,location=2,scientist=langstroth butterflies=1,honeybees=10insert census,location=2,scientist=perpetua butterflies=8,honeybees=23insert census,location=2,scientist=perpetua butterflies=7,honeybees=22#查询数据>select * from censusname: censustime butterflies honeybees location scientist---- ----------- --------- -------- ---------1649484087733993996 12 23 1 langstroth1649484087744790339 1 30 1 perpetua1649484087749043086 11 28 1 langstroth1649484087764539831 3 28 1 perpetua1649484087795830840 2 11 2 langstroth1649484087827259231 1 10 2 langstroth1649484087831655393 8 23 2 perpetua1649484089338011595 7 22 2 perpetua
- database:用户、保留策略、连续查询和时间序列数据的逻辑容器 数据库 mydb
- measurement:InfluxDB 数据结构中描述存储在相关字段中的数据的部分。字符串格式,类似于关系数据库中的table census
series:由共享measurement:、tag set 和 field key 定义的数据逻辑分组。
census,location=1,scientist=langstrothcensus,location=1,scientist=perpetuacensus,location=2,scientist=langstrothcensus,location=2,scientist=perpetua
point:代表一条数据记录,类似于 SQL 数据库表中的一行
每个point :
- 有一个 measurement 一个tag set,一个field key,一个field value和一个timestamp
- 由 series 和 timestamp 唯一标识
- field key:构成字段的键值对的键部分。字段键是字符串,它们存储元数据。 ```bash name: census fieldKey fieldType
butterflies float honeybees float
- [field value](https://docs.influxdata.com/influxdb/v1.8/concepts/glossary/#field-value):构成字段的键值对的值部分。field value是实际数据;它们可以是字符串、浮点数、整数或布尔值。字段值始终与时间戳相关联。字段值未编入索引 - 对字段值的查询会扫描与指定时间范围匹配的所有点,因此不够高效```bash12 231 3011 283 282 111 108 237 22
field set :一个点上的字段键和字段值的集合。
butterflies = 12 honeybees = 23butterflies = 1 honeybees = 30butterflies = 11 honeybees = 28butterflies = 3 honeybees = 28butterflies = 2 honeybees = 11butterflies = 1 honeybees = 10butterflies = 8 honeybees = 23butterflies = 7 honeybees = 22
retention policy(RP):描述 InfluxDB 保存数据的时间(持续时间 duration),集群中存储数据的多少副本(复制因子 replication factor),以及分片组覆盖的时间范围(分片组持续时间 shard group duration)。每个数据库的 RP 都是唯一的,并且与测量和标签集一起定义了一个系列。当您创建数据库时,InfluxDB 会创建一个名为 autogen 的保留策略,其持续时间为无限,复制因子设置为 1,分片组持续时间设置为 7 天 ```bash name duration shardGroupDuration replicaN default
autogen 0s 168h0m0s 1 true
- [tag key](https://docs.influxdata.com/influxdb/v1.8/concepts/glossary/#tag-key):构成标签的键值对的关键部分。标签key是字符串,它们存储元数据。tag key已编入索引,因此对tag key的查询是高效的。```bashname: censustagKey------locationscientist
- tag value:构成标签的键值对的值部分。标签value是字符串,它们存储元数据。tag value已编入索引,因此对tag value的查询是高效的
```bash
show tag values on mydb with key=location name: census key value
location 1 location 2
show tag values on mydb with key=scientist name: census key value
scientist langstroth scientist perpetua ```
tag set: 一个point上的tag keys和tag values的集合
location = 1, scientist = langstrothlocation = 2, scientist = langstrothlocation = 1, scientist = perpetualocation = 2, scientist = perpetua
timestamp:与数据关联的日期和时间。 InfluxDB 中的所有时间都是 UTC。
保留策略
```bash
语法格式
CREATE RETENTION POLICY
ON DURATION REPLICATION [SHARD DURATION ] [DEFAULT] 创建一个七天保留策略
create retention policy 7day on tenant_2 duration 1w replication 1
查看保留策略
show retention policies name duration shardGroupDuration replicaN default
autogen 0s 168h0m0s 1 true seven_day 168h0m0s 24h0m0s 1 false
创建一个四周的保留策略
create retention policy four_week on tenant_2 duration 4w replication 1
设置为默认的保留策略
alter retention policy four_week on tenant_2 default show retention policies name duration shardGroupDuration replicaN default
autogen 0s 168h0m0s 1 false seven_day 168h0m0s 24h0m0s 1 false four_week 672h0m0s 24h0m0s 1 true
删除保留策略
drop retention policy seven_day on tenant_2 ```
管理数据库
```bash
创建数据库语法格式
CREATE DATABASE
[WITH [DURATION ] [REPLICATION ] [SHARD DURATION ] [NAME ]] 创建数据库NOAA_water_database
CREATE DATABASE “NOAA_water_database”
创建数据库并且指定一个保留策略
CREATE DATABASE “NOAA_water_database” WITH DURATION 3d REPLICATION 1 SHARD DURATION 1h NAME “liquid”
删除数据库语法格式
DROP DATABASE
DROP DATABASE “NOAA_water_database”
```bash#drop series和delete series#区别两者都会删除数据库中满足条件的series,不同的是drop会把索引也删除,但是delte不会删除索引里面的series#格式DROP SERIES FROM <measurement_name[,measurement_name]> WHERE <tag_key>='<tag_value>'DELETE FROM <measurement_name> WHERE [<tag_key>='<tag_value>'] | [<time interval>]#例1DROP SERIES FROM "h2o_feet"DROP SERIES FROM "h2o_feet" WHERE "location" = 'santa_monica'DROP SERIES WHERE "location" = 'santa_monica'#例2DELETE FROM "h2o_feet"DELETE FROM "h2o_quality" WHERE "randtag" = '3'DELETE WHERE time < '2020-01-01'
正则表达式
支持的场景
- field keys and tag keys in the SELECTclause
- measurements in the FROMclause
- tag values and string field values in the WHEREclause.
- tag keys in the GROUP BYclause
目前,InfluxQL 不支持使用正则表达式来匹配 WHERE 子句、数据库和保留策略中的非字符串字段值
正则表达式比较比精确字符串比较计算量更大;使用正则表达式的查询不如没有正则表达式的查询性能好。
SELECT /<regular_expression_field_key>/ FROM /<regular_expression_measurement>/ WHERE [<tag_key> <operator> /<regular_expression_tag_value>/ | <field_key> <operator> /<regular_expression_field_value>/] GROUP BY /<regular_expression_tag_key>/
正则表达式被 / 字符包围,使用 Golang 的正则表达式语法
=~ 匹配 !~ 不匹配
#从filed keys和tag keys中查询所有包含l的列#请注意,SELECT 子句中的正则表达式必须至少匹配一个field key,才能返回与正则表达式匹配的tag key的结果。#暂时没有啊u分tag eky和field key的正则表达式,不支持/<regular_expression>/::[field | tag] 表达式SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= 1439856000000000000 AND time <= 1439856720000000000#计算measurement包含temperature的degrees列的平均值SELECT MEAN("degrees") FROM /temperature/#使用正则表达式在 WHERE 子句中指定标记值SELECT MEAN(water_level) FROM "h2o_feet" WHERE "location" =~ /[m]/ AND "water_level" > 3#使用正则表达式指定 WHERE 子句中没有值的标记SELECT * FROM "h2o_feet" WHERE "location" !~ /./#使用正则表达式在 WHERE 子句中指定带有值的标记SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location" =~ /./#使用正则表达式在 WHERE 子句中指定字段值> SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location" = 'santa_monica' AND "level description" =~ /between/#使用正则表达式在 GROUP BY 子句中指定标签键SELECT FIRST("index") FROM "h2o_quality" GROUP BY /l/
