Influx Sql系列教程八:query数据查询基本篇

mac2022-06-30  23

前面几篇介绍了InfluxDB的添加,删除修改数据,接下来进入查询篇,掌握一定的SQL知识对于理解本篇博文有更好的帮助,下面在介绍查询的基础操作的同时,也会给出InfluxSql与SQL之间的一些差别

在开始之前,先看一下供查询的数据

> show measurements name: measurements name ---- yhh > select * from yhh name: yhh time age blog id name phone ---- --- ---- -- ---- ----- 1563889538654374538 26 http://blog.hhui.top 10 一灰灰 1563889547738266214 30 http://blog.hhui.top 11 一灰灰 1563889704754695002 30 http://blog.hhui.top 11 一灰灰2 1563889723440000821 30 http://blog.hhui.top 11 一灰灰3 110 > show tag keys from yhh name: yhh tagKey ------ name phone

1. 基本查询

基本查询语法如下

SELECT <field_key>[,<field_key>,<tag_key>] FROM <measurement_name>[,<measurement_name>]

上面的语法中,划分了select和from两块

select语句

select * : 表示查询所有的field和tag对应的值select field_key: 表示查询特定的field对应的值select tag_key: 表示查询的特定的tag对应的值SELECT "<field_key>"::field,"<tag_key>"::tag: 注意::field和::tag用来限定这个数据的类型为tag或者是field

from语句

from后面需要接上measurement,表示从这个mesaurement中查询数据

FROM <measurement_name> 从指定的measurement中获取数据FROM <measurement_name>,<measurement_name> 从多个measurement中获取数据FROM <database_name>.<retention_policy_name>.<measurement_name> 从某个数据库中某个保留策略中查询measurement中的数据

实例演示

下面给出几个简答的演示实例,分别介绍查询指定的field/tag的方式

> select age from yhh; name: yhh time age ---- --- 1563889538654374538 26 1563889547738266214 30 1563889704754695002 30 1563889723440000821 30 > select "age"::field, "name"::tag from yhh; name: yhh time age name ---- --- ---- 1563889538654374538 26 一灰灰 1563889547738266214 30 一灰灰 1563889704754695002 30 一灰灰2 1563889723440000821 30 一灰灰3

2. 保留策略数据查询

上面的定义中,说明了可以查询指定保留策略中的数据,下面演示一下应该如何实现

# 创建保留策略 > create retention policy "1D" duration 1d on test # 插入一条数据 > insert into "1D" yhh,name=二灰,phone=119 email="bangzewu@126.com",blog="http://spring.hhui.top",id=27 # 查询 > select * from "1D".yhh name: yhh time blog email id name phone ---- ---- ----- -- ---- ----- 1565693045801509796 http://spring.hhui.top bangzewu@126.com 27 二灰 119 >

查询语句和一般的select没有什么特别的区别,唯一需要注意的是measurement前面需要加上保留策略

3. Where语句

前面的查询主要是限定需要获取的数据,而我们实际的场景中,更多的是查询某类满足条件的数据,也就是常见的SQL中加上where查询条件限定

语法如下

SELECT_clause FROM_clause WHERE <conditional_expression> [(AND|OR) <conditional_expression> [...]]

主要看一下where后面的条件表达式,因为influxdb中的数据可以划分为两类,这两种不同的类型,在构建查询语句的时候,会有一些区别

field查询条件

我们已知field的类型有四种:string|int|boolean|float,所以它支持的操作符有

操作符说明=相等<>, !=不相同>, >=大于,大于等于<, <=小于,小于等于

tag查询条件

在influxdb中tag都是string类型,会建立索引,所以基于tag的查询效率一般来讲是优于field查询的,它支持的操作符为

操作符说明=相等<>, !=不相同

在influxdb中没有in查询,不同的查询条件可以使用and/or来连接,表示同时满足or一个满足即可,下满给出几个简单的实例

# 根据field进行查询 > select * from yhh where age=26 name: yhh time age blog id name phone ---- --- ---- -- ---- ----- 1563889538654374538 26 http://blog.hhui.top 10 一灰灰 # 根据tag进行查询 > select * from yhh where phone!='' name: yhh time age blog id name phone ---- --- ---- -- ---- ----- 1563889723440000821 30 http://blog.hhui.top 11 一灰灰3 110 # 简单的运算查询 > select * from yhh where age + 2>30 name: yhh time age blog id name phone ---- --- ---- -- ---- ----- 1563889547738266214 30 http://blog.hhui.top 11 一灰灰 1563889704754695002 30 http://blog.hhui.top 11 一灰灰2 1563889723440000821 30 http://blog.hhui.top 11 一灰灰3 110 > select * from yhh where "name"='一灰灰' name: yhh time age blog id name phone ---- --- ---- -- ---- ----- 1563889538654374538 26 http://blog.hhui.top 10 一灰灰 1563889547738266214 30 http://blog.hhui.top 11 一灰灰

4. 小结

这一小节内容,介绍的是最基础的inflxudb查询操作,和我们了解的SQL基本上没有太多的区别,可能唯一需要注意的就是制定保留策略查询时,需要使用"<retention policy>".<measurement>的方式跟在from语句之后

其次一个需要注意的时,查询语句中,推荐的写法是

tag key或field key请使用双引号括起来如果类型为string,请用单引号把过滤条件括起来

如下面这种写法,否则可能会出现问题

select * from yhh where "name"='一灰灰'

下一篇,我们将介绍查询语句中常见的分组,排序,分页等场景的使用姿势

II. 其他

0. 系列博文

190730-Influx Sql系列教程七:delete 删除数据190729-Influx Sql系列教程六:insert 修改数据190726-Influx Sql系列教程五:insert 添加数据190723-Influx Sql系列教程四:series/point/tag/field190721-Influx Sql系列教程三:measurement 表190719-Influx Sql系列教程二:retention policy 保存策略190718-Influx Sql系列教程一:database 数据库190717-Influx Sql系列教程零:安装及influx-cli使用姿势介绍190509-InfluxDb之时间戳显示为日期格式190506-InfluxDB之配置修改190505-InfluxDB之权限管理180727-时序数据库InfluxDB之备份和恢复策略180726-InfluxDB基本概念小结180725-InfluxDB-v1.6.0安装和简单使用小结

参考博文

https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/#the-basic-select-statement

1. 一灰灰Blog: https://liuyueyi.github.io/hexblog

一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

2. 声明

尽信书则不如,已上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

微博地址: 小灰灰BlogQQ: 一灰灰/3302797840

3. 扫描关注

一灰灰blog

转载于:https://www.cnblogs.com/yihuihui/p/11386691.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)