InfluxDb 注意点

基本介绍

influxdb是使用GO编写的基于时间序列的数据库,适用于存储大量带有时间戳的数据,监控数据,日志,应用程序的指标、数据分析数据等等。 通过influxdb自动保存数据,你不需要删除和清理,只需要定义一段时间DB会帮你自动清理。 influxdb默认端口8086,默认是http协议接口

influxdb怎么防止数据的丢失

建议只使用字符串类型和浮点类型,把所有的整型,长整型,浮点型,双精度型统一转为小数格式的浮点类型,再写入数据库,字符串类型的不用做转换,这样就不会出现插入数据失败和丢失数据了。

points的数据结构

point在influxdb数据库中,相当于一条记录。其中又可以细分为: image.png 注意事项:

  • time 相当于表的主键,当一条数据的time和tags完全相同时候,新数据会替换掉旧数据,旧数据则丢失(线上环境尤其要注意)。
  • tags 和time可以作为排序字段,field则不可以。如:ORDER BY time DESC
  • field是必须的,并且不能根据field来排序
  • tag是可选的,tag可以用来做索引,tag是以字符串的形式存放的
  • 设置了保存策略后,若此保存策略为设置成默认保存策略(一个库可以有多个保存策略),则在查询时,表名(measurement)前,要加上保存策略。举例:保留策略为two-hour不是默认保存策略,则查询时候,需要指定其保存策略。select * from two-hour.measure where time > now() -10
  • fields和tags的字段类型是由存入的第一条记录值决定的。 举例: 如第一条记录fieldA的值为2,想插入一条记录,fieldA字段值为3.14的值,就会报错。因为该字段已经被初始化为整型了。 如第一条记录fieldB存储的是3,想插入一条记录,fieldB字段值为hello,则也会报错,该字段已被初始化成整型,不能再写入字符串了。

数据保留策略

创建数据库时设置保留策略

create database testDB with duration 30d

查看

show retention policies on "数据库名称"

修改

alter retention policy "原策略名称" on "数据库名称" duration 180d replication 1 default

duration 单位:

  • 秒 s
  • 分 m
  • 小时 h
  • 天 d
  • 周 w

查询

#----综合使用
书写顺序
select distinct * from '表名' where '限制条件'  group by '分组依据' having '过滤条件' order by  limit '展示条数'
执行顺序
from       -- 查询
where      -- 限制条件
group by   -- 分组
having     -- 过滤条件
order by   -- 排序
limit      -- 展示条数
distinct   -- 去重
select     -- 查询的结果

查询数据表weather 的所有记录

> select * from weather
name: weather
time altitude area humidity temperature
---- -------- ---- -------- -----------
1607604432455278300 1001 南 -5 10
1607656595672442800 1000 东 -4 9
1607656662027484500 1001 南 -5 11
1607656706278952000 999 南 -5 11
1607656751612223600 1002 西 -2 11
1607656799728402900 1003 东 -2 11

按条件查询

#查询temperature=11的数据
 
> select * from weather where temperature=11
name: weather
time                altitude area humidity temperature
----                -------- ---- -------- -----------
1607656662027484500 1001     南    -5       11
1607656706278952000 999      南    -5       11
1607656751612223600 1002     西    -2       11
1607656799728402900 1003     东    -2       11

#查询altitude,temperature两列的数据
> select altitude,temperature from weather
name: weather
time                altitude temperature
----                -------- -----------
1607604432455278300 1001     10
1607656595672442800 1000     9
1607656662027484500 1001     11
1607656706278952000 999      11
1607656751612223600 1002     11
1607656799728402900 1003     11

排序

#按最新时间排序
> select * from weather order by time desc
name: weather
time                altitude area humidity temperature
----                -------- ---- -------- -----------
1607656799728402900 1003     东    -2       11
1607656751612223600 1002     西    -2       11
1607656706278952000 999      南    -5       11
1607656662027484500 1001     南    -5       11
1607656595672442800 1000     东    -4       9
1607604432455278300 1001     南    -5       10

#按最早时间排序
> select * from weather order by time asc
name: weather
time                altitude area humidity temperature
----                -------- ---- -------- -----------
1607604432455278300 1001     南    -5       10
1607656595672442800 1000     东    -4       9
1607656662027484500 1001     南    -5       11
1607656706278952000 999      南    -5       11
1607656751612223600 1002     西    -2       11
1607656799728402900 1003     东    -2       11

去重

> select distinct humidity from weather
name: weather
time distinct
---- --------
0    -5
0    -4
0    -2

group by

select 查询字段1,查询字段2,... from 表名
      where 过滤条件
      group by分组依据  # 分组后取出的是每个组的第一条数据

> select * from weather group by area
name: weather
tags: area=东
time                altitude humidity temperature
----                -------- -------- -----------
1607656595672442800 1000     -4       9
1607656799728402900 1003     -2       11

name: weather
tags: area=南
time                altitude humidity temperature
----                -------- -------- -----------
1607604432455278300 1001     -5       10
1607656662027484500 1001     -5       11
1607656706278952000 999      -5       11

name: weather
tags: area=西
time                altitude humidity temperature
----                -------- -------- -----------
1607656751612223600 1002     -2       11

聚合

count()函数

返回一个(field)字段中的非空值的数量。

SELECT COUNT(<field_key>) FROM <measurement_name> [WHERE <stuff>] [GROUP BY <stuff>]

> select count(humidity) from weather
name: weather
time count
---- -----
0    6

MEAN() 函数

返回一个字段(field)中的值的算术平均值(平均值)。字段类型必须是长整型或float64。

语法格式:SELECT MEAN(<field_key>) FROM <measurement_name> [WHERE ] [GROUP BY ]

> SELECT MEAN(humidity) from weather
name: weather
time mean
---- ----
0    -3.8333333333333335

MEDIAN()函数

从单个字段(field)中的排序值返回中间值(中位数)。中值是在一组数值中居于中间的数值。字段值的类型必须是长整型或float64格式。

语法:SELECT MEDIAN(<field_key>) FROM <measurement_name> [WHERE ] [GROUP BY ]

> SELECT MEAN(humidity) from weather
name: weather
time mean
---- ----
0    -3.8333333333333335

SPREAD()函数

返回字段的最小值和最大值之间的差值。数据的类型必须是长整型或float64。

语法:SELECT SPREAD(<field_key>) FROM <measurement_name> [WHERE ] [GROUP BY ]

> select spread(humidity) from weather
name: weather
time spread
---- ------
0    3

SUM()函数

返回一个字段中的所有值的和。字段的类型必须是长整型或float64。

语法:SELECT SUM(<field_key>) FROM <measurement_name> [WHERE ] [GROUP BY

> select sum(humidity) from weather
name: weather
time sum
---- ---
0    -23

INTEGRAL()函数

返回曲线

语法:SELECT INTEGRAL( [ * | <field_key> | /<regular_expression>/ ] [ , ] ) [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]

> select INTEGRAL(temperature) from weather
name: weather
time integral
---- --------
0    497728.82358215

distinc()函数

> select distinct(temperature) from weather
name: weather
time distinct
---- --------
0    10
0    9
0    11

limit限制条数

#显示一条信息
> select * from weather limit 1
name: weather
time                altitude area humidity temperature
----                -------- ---- -------- -----------
1607604432455278300 1001     南    -5       10

#limit 10 offset 15,就是从第15行开始之后的10条数据
> select * from weather limit 2 offset 2
name: weather
time                altitude area humidity temperature
----                -------- ---- -------- -----------
1607656662027484500 1001     南    -5       11
1607656706278952000 999      南    -5       11

or

influxDB中没有in的操作,但是有or。对于习惯了mysql的in来说,用or就需要在代码中循环了。

> select * from weather where altitude=1001 or temperature=11
name: weather
time                altitude area humidity temperature
----                -------- ---- -------- -----------
1607656662027484500 1001     南    -5       11
1607656706278952000 999      南    -5       11
1607656751612223600 1002     西    -2       11
1607656799728402900 1003     东    -2       11

模糊查询

> select * from test
name: test
time                app count host      monitor_name num
----                --- ----- ----      ------------ ---
1585897703920290000     1     127.0.0.1 test         
1585897983909417000 ios 2     127.0.0.1 test1        3
1585898383503216000 ios 2     127.0.0.1 test1        3
1585901694441000000 ios 2     127.0.0.1 app1         3
1585901704179677000 ios 2     127.0.0.1 ios1         3


## =~/给定字段/ 包含指定字段的
> select * from test where monitor_name =~/app/
name: test
time                app count host      monitor_name num
----                --- ----- ----      ------------ ---
1585901694441000000 ios 2     127.0.0.1 app1         3


##=~/^给定字段/ 以指定字段开始的
> select * from test where monitor_name =~/^app/
name: test
time                app count host      monitor_name num
----                --- ----- ----      ------------ ---
1585901694441000000 ios 2     127.0.0.1 app1         3


##=~/给定字段$/ 以指定字段结尾的
> select * from test where monitor_name =~/p1$/
name: test
time                app count host      monitor_name num
----                --- ----- ----      ------------ ---
1585901694441000000 ios 2     127.0.0.1 app1         3

展示tag

> show tag keys from weather
name: weather
tagKey
------
altitude
area

#查询单个tag的value值

#查询所以tag为altitude的value的值
> show tag values from weather with key="altitude"
name: weather
key      value
---      -----
altitude 1000
altitude 1001
altitude 1002
altitude 1003
altitude 999

鲸之声为您拼命加载中...