influxdb版本1.8

安装:

  1. //拉取镜像
  2. docker pull influxdb:1.8.0
  3. //运行
  4. 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服务执行备份和恢复操作。

基本使用

  1. #使用docker安装,进入容器内部
  2. docker exec -it 容器Id bash
  3. #控制台输入命令,进入influx制台命令行模式
  4. influx -precision rfc3339
  5. #上面的命令解释 -precision代表使用的时间格式是rfc3339 一种标准的时间格式 YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ
  6. #不指定默认使用纳秒时间戳,其他可用的格式是h, m, s, ms, u or ns
  7. #使用帮助命令查看其他可选项
  8. root@823b110209fa:/# influx --help
  9. Usage of influx:
  10. -version
  11. 当前influxdb的版本
  12. -path-prefix 'url path'
  13. Path that follows the host and port
  14. -host 'host name'
  15. 需要连接的主机名,默认是使用本机
  16. -port 'port #'
  17. 端口号 默认8086
  18. -socket 'unix domain socket'
  19. Unix socket to connect to.
  20. -database 'database name'
  21. 使用的数据库名
  22. -password 'password'
  23. 连接的密码,如果指定,默认密码为空字符串''
  24. -username 'username'
  25. 如连接设置了账号密码,此选项输入账号
  26. -ssl
  27. Use https for requests.
  28. -unsafeSsl
  29. Set this when connecting to the cluster using https and not use SSL verification.
  30. -execute 'command'
  31. 指定要执行的命令
  32. -type 'influxql|flux'
  33. Type specifies the query language for executing commands or when invoking the REPL.
  34. -format 'json|csv|column'
  35. 指定查询出来的数据的格式json格式 csv格式和列表格式
  36. -precision 'rfc3339|h|m|s|ms|u|ns'
  37. 指定时间显示格式
  38. -consistency 'any|one|quorum|all'
  39. Set write consistency level: any, one, quorum, or all
  40. -pretty
  41. 当使用json格式的时候,是否开始格式化
  42. -import
  43. Import a previous database export from file
  44. -pps
  45. How many points per second the import will allow. By default it is zero and will not throttle importing.
  46. -path
  47. Path to file to import
  48. -compressed
  49. Set to true if the import file is compressed
  50. Examples:
  51. # Use influx in a non-interactive mode to query the database "metrics" and pretty print json:
  52. $ influx -database 'metrics' -execute 'select * from cpu' -format 'json' -pretty
  53. # Connect to a specific database on startup and set database context:
  54. $ influx -database 'metrics' -host 'localhost' -port '8086'
  1. #创建数据库 没有提示任何错误消息就是代表正常执行了
  2. CREATE DATABASE mydb
  3. #查看数据库
  4. SHOW DATABASES
  5. #使用数据库
  6. Using database mydb
  7. #插入数据的格式
  8. insert <表名>[,<tag-key>=<tag-value>...] 空格隔开 <field-key>=<field-value>[,<field2-key>=<field2-value>...] 空格隔开 [unix-nano-timestamp]
  9. #例如 在插入数据的时候可以不提供时间戳,会自动使用系统的当前时间
  10. INSERT cpu,host=serverA,region=us_west value=0.64
  11. INSERT temperature,machine=unit42,type=assembly external=25,internal=37
  12. #查询
  13. SELECT "host", "region", "value" FROM "cpu"
  14. #在大型数据库上使用不带 LIMIT 子句的 * 可能会导致性能问题。您可以使用 Ctrl+C 取消响应时间过长的查询
  15. SELECT * FROM "temperature"
  16. #表名使用正则进行查询
  17. SELECT * FROM /.*/ LIMIT 1

关键概念

  1. #模拟数据
  2. #使用mydb数据库
  3. use mydb
  4. #插入数据
  5. insert census,location=1,scientist=langstroth butterflies=12,honeybees=23
  6. insert census,location=1,scientist=perpetua butterflies=1,honeybees=30
  7. insert census,location=1,scientist=langstroth butterflies=11,honeybees=28
  8. insert census,location=1,scientist=perpetua butterflies=3,honeybees=28
  9. insert census,location=2,scientist=langstroth butterflies=2,honeybees=11
  10. insert census,location=2,scientist=langstroth butterflies=1,honeybees=10
  11. insert census,location=2,scientist=perpetua butterflies=8,honeybees=23
  12. insert census,location=2,scientist=perpetua butterflies=7,honeybees=22
  13. #查询数据
  14. >select * from census
  15. name: census
  16. time butterflies honeybees location scientist
  17. ---- ----------- --------- -------- ---------
  18. 1649484087733993996 12 23 1 langstroth
  19. 1649484087744790339 1 30 1 perpetua
  20. 1649484087749043086 11 28 1 langstroth
  21. 1649484087764539831 3 28 1 perpetua
  22. 1649484087795830840 2 11 2 langstroth
  23. 1649484087827259231 1 10 2 langstroth
  24. 1649484087831655393 8 23 2 perpetua
  25. 1649484089338011595 7 22 2 perpetua
  • database:用户、保留策略、连续查询和时间序列数据的逻辑容器 数据库 mydb
  • measurement:InfluxDB 数据结构中描述存储在相关字段中的数据的部分。字符串格式,类似于关系数据库中的table census
  • series:由共享measurement:、tag setfield key 定义的数据逻辑分组。

    1. census,location=1,scientist=langstroth
    2. census,location=1,scientist=perpetua
    3. census,location=2,scientist=langstroth
    4. census,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

  1. - [field value](https://docs.influxdata.com/influxdb/v1.8/concepts/glossary/#field-value):构成字段的键值对的值部分。field value是实际数据;它们可以是字符串、浮点数、整数或布尔值。字段值始终与时间戳相关联。字段值未编入索引 - 对字段值的查询会扫描与指定时间范围匹配的所有点,因此不够高效
  2. ```bash
  3. 12 23
  4. 1 30
  5. 11 28
  6. 3 28
  7. 2 11
  8. 1 10
  9. 8 23
  10. 7 22
  • field set :一个点上的字段键和字段值的集合。

    1. butterflies = 12 honeybees = 23
    2. butterflies = 1 honeybees = 30
    3. butterflies = 11 honeybees = 28
    4. butterflies = 3 honeybees = 28
    5. butterflies = 2 honeybees = 11
    6. butterflies = 1 honeybees = 10
    7. butterflies = 8 honeybees = 23
    8. butterflies = 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

  1. - [tag key](https://docs.influxdata.com/influxdb/v1.8/concepts/glossary/#tag-key):构成标签的键值对的关键部分。标签key是字符串,它们存储元数据。tag key已编入索引,因此对tag key的查询是高效的。
  2. ```bash
  3. name: census
  4. tagKey
  5. ------
  6. location
  7. scientist
  • 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的集合

    1. location = 1, scientist = langstroth
    2. location = 2, scientist = langstroth
    3. location = 1, scientist = perpetua
    4. location = 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”

  1. ```bash
  2. #drop series和delete series
  3. #区别两者都会删除数据库中满足条件的series,不同的是drop会把索引也删除,但是delte不会删除索引里面的series
  4. #格式
  5. DROP SERIES FROM <measurement_name[,measurement_name]> WHERE <tag_key>='<tag_value>'
  6. DELETE FROM <measurement_name> WHERE [<tag_key>='<tag_value>'] | [<time interval>]
  7. #例1
  8. DROP SERIES FROM "h2o_feet"
  9. DROP SERIES FROM "h2o_feet" WHERE "location" = 'santa_monica'
  10. DROP SERIES WHERE "location" = 'santa_monica'
  11. #例2
  12. DELETE FROM "h2o_feet"
  13. DELETE FROM "h2o_quality" WHERE "randtag" = '3'
  14. DELETE WHERE time < '2020-01-01'

正则表达式

支持的场景

目前,InfluxQL 不支持使用正则表达式来匹配 WHERE 子句、数据库和保留策略中的非字符串字段值
正则表达式比较比精确字符串比较计算量更大;使用正则表达式的查询不如没有正则表达式的查询性能好。

  1. 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 的正则表达式语法
=~ 匹配 !~ 不匹配

  1. #从filed keys和tag keys中查询所有包含l的列
  2. #请注意,SELECT 子句中的正则表达式必须至少匹配一个field key,才能返回与正则表达式匹配的tag key的结果。
  3. #暂时没有啊u分tag eky和field key的正则表达式,不支持/<regular_expression>/::[field | tag] 表达式
  4. SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= 1439856000000000000 AND time <= 1439856720000000000
  5. #计算measurement包含temperature的degrees列的平均值
  6. SELECT MEAN("degrees") FROM /temperature/
  7. #使用正则表达式在 WHERE 子句中指定标记值
  8. SELECT MEAN(water_level) FROM "h2o_feet" WHERE "location" =~ /[m]/ AND "water_level" > 3
  9. #使用正则表达式指定 WHERE 子句中没有值的标记
  10. SELECT * FROM "h2o_feet" WHERE "location" !~ /./
  11. #使用正则表达式在 WHERE 子句中指定带有值的标记
  12. SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location" =~ /./
  13. #使用正则表达式在 WHERE 子句中指定字段值
  14. > SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location" = 'santa_monica' AND "level description" =~ /between/
  15. #使用正则表达式在 GROUP BY 子句中指定标签键
  16. SELECT FIRST("index") FROM "h2o_quality" GROUP BY /l/