前面一篇介绍了influxdb中基本的查询操作,在结尾处提到了如果我们希望对查询的结果进行分组,排序,分页时,应该怎么操作,接下来我们看一下上面几个场景的支持

在开始本文之前,建议先阅读上篇博文: 190813-Influx Sql系列教程八:query数据查询基本篇

0. 数据准备

在开始查询之前,先看一下我们准备的数据,其中name,phone为tag, age,blog,id为field

> 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. 分组查询

和sql语法一样,influxdb sql的分组也是使用group by语句,其定义如下

SELECT_clause FROM_clause [WHERE_clause] GROUP BY [* | <tag_key>[,<tag_key]]

a. group by tag

从上面的定义中,有一点需要特别强调,用来分组的必须是tag,也就是说对于influxdb而言,不支持根据field进行分组

一个实际的演示如下:

> select * from yhh group by phone
name: yhh
tags: phone=
time age blog id name
---- --- ---- -- ----
1563889538654374538 26 http://blog.hhui.top 10 一灰灰
1563889547738266214 30 http://blog.hhui.top 11 一灰灰
1563889704754695002 30 http://blog.hhui.top 11 一灰灰2 name: yhh
tags: phone=110
time age blog id name
---- --- ---- -- ----
1563889723440000821 30 http://blog.hhui.top 11 一灰灰3

注意上面的输出结果,比较有意思,分成了两个结构段落,且可以输出完整的数据;而mysql的分组查询条件中一般需要带上分组key,然后实现一些数据上的聚合查询

如果我的分组中,使用field进行分组查询,会怎样?报错么?

> select * from yhh group by age
name: yhh
tags: age=
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

从上面的case中可以看出,虽然执行了,但是返回的结果并不是我们预期的。

b. group by *

另外一个与一般SQL语法不一样的是group by 后面可以跟上*,表示根据所有的tag进行分组,一个测试如下

> select * from yhh group by *
name: yhh
tags: name=一灰灰, phone=
time age blog id
---- --- ---- --
1563889538654374538 26 http://blog.hhui.top 10
1563889547738266214 30 http://blog.hhui.top 11 name: yhh
tags: name=一灰灰2, phone=
time age blog id
---- --- ---- --
1563889704754695002 30 http://blog.hhui.top 11 name: yhh
tags: name=一灰灰3, phone=110
time age blog id
---- --- ---- --
1563889723440000821 30 http://blog.hhui.top 11
>

c. group by time

除了上面的根据tag进行分组之外,还有一个更高级的特性,根据时间来分组,这个时间还支持一些简单的函数操作

定义如下

SELECT <function>(<field_key>) FROM_clause WHERE <time_range> GROUP BY time(<time_interval>),[tag_key] [fill(<fill_option>)]

我们知道influxdb的一个重要应用场景就是监控的记录,在监控面板上经常会有的就是根据时间进行聚合,比如查询某个服务每分钟的异常数,qps, rt等

下面给出一个简单的使用case

# 为了显示方便,将数据的时间戳改成日期方式展示
> precision rfc3339
> select * from yhh
name: yhh
time age blog id name phone
---- --- ---- -- ---- -----
2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10 一灰灰
2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11 一灰灰
2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11 一灰灰2
2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11 一灰灰3 110
> select count(*) from yhh where time>'2019-07-23T13:44:38.654374538Z' and time<'2019-07-23T13:50:43.440000821Z' GROUP BY time(2m)
name: yhh
time count_age count_blog count_id
---- --------- ---------- --------
2019-07-23T13:44:00Z 2 2 2
2019-07-23T13:46:00Z 0 0 0
2019-07-23T13:48:00Z 2 2 2
2019-07-23T13:50:00Z 0 0 0

在上面的查询语句中,有几个地方需要说明一下

  • select后面跟上的是单个or多个field的聚合操作,根据时间进行分组时,不允许查询具体的field值,否则会有下面的错误提示
    > select * from yhh where time>'2019-07-23T13:44:38.654374538Z' and time<'2019-07-23T13:50:43.440000821Z'  GROUP BY time(2m)
    ERR: GROUP BY requires at least one aggregate function
  • where条件限定查询的时间范围,否则会得到很多数据
  • group by time(2m) 表示每2分钟做一个分组, group by time(2s)则表示每2s做一个分组

2. 排序

在influxdb中排序,只支持针对time进行排序,其他的field,tag(因为是string类型,也没法排)是不能进行排序的

语法比较简单,如下,根据时间倒序/升序

order by time desc/asc

一个简单的实例如下

# 根据非time进行排序时,直接报错
> select * from yhh order by age
ERR: error parsing query: only ORDER BY time supported at this time
# 根据时间进行倒排
> select * from yhh order by time desc
name: yhh
time age blog id name phone
---- --- ---- -- ---- -----
2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11 一灰灰3 110
2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11 一灰灰2
2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11 一灰灰
2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10 一灰灰
>

3. 查询限制

我们常见的分页就是limit语句,我们常见的limit语句为 limit page, size,可以实现分页;然而在influxdb中则不同,limit后面只能跟上一个数字,表示限定查询的最多条数

a. limit

N指定每次measurement返回的point个数

SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] LIMIT <N>

下满给出几个实际的case

> select * from yhh limit 2
name: yhh
time age blog id name phone
---- --- ---- -- ---- -----
2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10 一灰灰
2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11 一灰灰 # 分组之后,再限定查询条数
> select * from yhh group by "name" limit 1
name: yhh
tags: name=一灰灰
time age blog id phone
---- --- ---- -- -----
2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10 name: yhh
tags: name=一灰灰2
time age blog id phone
---- --- ---- -- -----
2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11 name: yhh
tags: name=一灰灰3
time age blog id phone
---- --- ---- -- -----
2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11 110

b. slimit

N指定从指定measurement返回的series数

SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] GROUP BY *[,time(<time_interval>)] [ORDER_BY_clause] SLIMIT <N>

接下来演示下这个的使用姿势,首先准备插入几条数据,确保tag相同

> insert yhh,name=一灰灰,phone=110 blog="http://spring.hhui.top",age=14,id=14
> insert yhh,name=一灰灰,phone=110 blog="http://spring.hhui.top",age=15,id=15
> insert yhh,name=一灰灰,phone=110 blog="http://spring.hhui.top",age=16,id=16
> select * from yhh
name: yhh
time age blog id name phone
---- --- ---- -- ---- -----
2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10 一灰灰
2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11 一灰灰
2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11 一灰灰2
2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11 一灰灰3 110
2019-08-14T11:18:06.804162557Z 14 http://spring.hhui.top 14 一灰灰 110
2019-08-14T11:18:10.146588721Z 15 http://spring.hhui.top 15 一灰灰 110
2019-08-14T11:18:12.753413004Z 16 http://spring.hhui.top 16 一灰灰 110
> show series on test from yhh
key
---
yhh,name=一灰灰
yhh,name=一灰灰,phone=110
yhh,name=一灰灰2
yhh,name=一灰灰3,phone=110

如下面的一个使用case

> select * from yhh group by * slimit 3
name: yhh
tags: name=一灰灰, phone=
time age blog id
---- --- ---- --
2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10
2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11 name: yhh
tags: name=一灰灰, phone=110
time age blog id
---- --- ---- --
2019-08-14T11:18:06.804162557Z 14 http://spring.hhui.top 14
2019-08-14T11:18:10.146588721Z 15 http://spring.hhui.top 15
2019-08-14T11:18:12.753413004Z 16 http://spring.hhui.top 16 name: yhh
tags: name=一灰灰2, phone=
time age blog id
---- --- ---- --
2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11 name: yhh
tags: name=一灰灰3, phone=110
time age blog id
---- --- ---- --
2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11

说实话,这一块没看懂,根据官方的文档进行翻译的,没有get这个slimit的特点

4. 分页

上面只有point个数限制,但是分页怎么办?难道不支持么?

在influxdb中,有专门的offset来实现分页

SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] LIMIT_clause OFFSET <N> [SLIMIT_clause]

简单来讲,就是limit 条数 offset 偏移

使用实例

> select * from yhh
name: yhh
time age blog id name phone
---- --- ---- -- ---- -----
2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10 一灰灰
2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11 一灰灰
2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11 一灰灰2
2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11 一灰灰3 110
2019-08-14T11:18:06.804162557Z 14 http://spring.hhui.top 14 一灰灰 110
2019-08-14T11:18:10.146588721Z 15 http://spring.hhui.top 15 一灰灰 110
2019-08-14T11:18:12.753413004Z 16 http://spring.hhui.top 16 一灰灰 110
# 查询结果只有2条数据,从第三个开始(0开始计数)
> select * from yhh limit 2 offset 3
name: yhh
time age blog id name phone
---- --- ---- -- ---- -----
2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11 一灰灰3 110
2019-08-14T11:18:06.804162557Z 14 http://spring.hhui.top 14 一灰灰 110
> select * from yhh limit 2 offset 3

5. 小结

本篇influxdb的查询篇主要介绍了sql中的三种常用case,分组,排序,分页;虽然使用姿势和我们常见的SQL大同小异,但是一些特殊点需要额外注意一下

  • 分组查询时,注意分组的key必须是time或者tag,分组查询可以返回完整的point
  • 排序,只支持根据时间进行排序,其他的字段都不支持
  • 分页,需要注意limit size offset startIndex和我们一般的使用case不同,它的两个参数分别表示查询的point个数,以及偏移量;而不是传统sql中的页和条数

II. 其他

0. 系列博文

参考博文

1. 一灰灰Bloghttps://liuyueyi.github.io/hexblog

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

2. 声明

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

3. 扫描关注

一灰灰blog

Influx Sql系列教程九:query数据查询基本篇二的更多相关文章

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

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

  2. Influx Sql系列教程七:delete 删除数据

    前面介绍了使用insert实现新增和修改记录的使用姿势,接下来我们看一下另外一个简单的使用方式,如何删除数据 1. delete 语句 delete的官方语法如下 DELETE FROM <me ...

  3. Influx Sql系列教程六:insert 修改数据

    在influxdb中没有专门的修改数据的update语句,对于influxdb而言,如果想修改数据,还是得使用我们前面的说到的insert来实现,那么怎么判断一条insert语句是插入还是修改呢? 1 ...

  4. Influx Sql系列教程五:insert 添加数据

    接下来开始进入influxdb的curd篇,首先我们看一下如何添加数据,也就是insert的使用姿势 在进入本篇之前,对于不了解什么是retention policy, tag, field的同学,有 ...

  5. Influx Sql系列教程四:series/point/tag/field

    influxdb中的一条记录point,主要可以分为三类,必须存在的time(时间),string类型的tag,以及其他成员field:而series则是一个measurement中保存策略和tag集 ...

  6. Influx Sql系列教程三:measurement 表

    在influxdb中measurement相当于mysql中的表,可以理解为一条一条记录都是存与measurent中的,一个数据库中可以有多个measurement,一个measurement中可以存 ...

  7. Influx Sql系列教程二:retention policy 保存策略

    retention policy这个东西相比较于传统的关系型数据库(比如mysql)而言,是一个比较新的东西,在将表之前,有必要来看一下保存策略有什么用,以及可以怎么用 I. 基本操作 1. 创建re ...

  8. Influx Sql系列教程零:安装及influx-cli使用姿势介绍

    influxdb 时序数据库,因为实际业务中使用到了,然而并没有发现有特别好的文章,完整的介绍influx sql的使用姿势,因此记录下实际开发中学习的体会,主要参考来自于官方文档 Influx Qu ...

  9. SQL系列(九)—— 子查询(subQuery)

    1.子查询 前面的系列介绍的都是简单的查询场景,其中都只涉及到单张表的数据检索.但是在日常是实际应用中,数据模型之间的关系都非常的复杂,数据的需求一般都是来源于多个数据模型之间的组合而成,即对应多张表 ...

随机推荐

  1. postgres —— 窗口函数入门

    注:测试数据在 postgres —— 分组集与部分聚集 中 聚集将多行转变成较少.聚集的行.而窗口则不同,它把当前行与分组中的所有行对比,并且返回的行数没有变化. 组合当前行与 production ...

  2. linux下用vim写Python自动缩进的配置

    #首先用 find / -name vimrc 找到vimrc文件#一般在 /etc/vimrc#进入vimrc后加入以下命令 set number set autoindent set shiftw ...

  3. Flume高级之自定义MySQLSource

    1 自定义Source说明 Source是负责接收数据到Flume Agent的组件.Source组件可以处理各种类型.各种格式的日志数据,包括avro.thrift.exec.jms.spoolin ...

  4. RobotFrameWork框架介绍与安装

    一.RobotFrameWork介绍 1.简称RF,基于python研发的一款自动化测试框架.开源.跨平台的测试框架 2.RF是基于RF基金来研发的一款软件,目前已完全能够在python3环境下应用 ...

  5. go正则表达式

    单行模式(?s:(.?))万能用法尽量匹配少的文本,最关键的是可以匹配换行的文本,直接写.?不能匹配\n package main import ( "fmt" "reg ...

  6. Java web开发——文件夹的上传和下载

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...

  7. callbag js callback 标准-支持轻量级观测以及迭代

    callbag 是一个js 的回调标准,方便开发支持观测以及迭代的代码 类似已经有好多的实现了 callbag-basics 比rxjs 以及xstream 还快 wonka 说明 基于标准的开发,对 ...

  8. 虚拟变量陷阱(Dummy Variable Trap)

    虚拟变量陷阱(Dummy Variable Trap):指当原特征有m个类别时,如果将其转换成m个虚拟变量,就会导致变量间出现完全共线性的情况. 假设我们有一个特征“性别”,包含男性和女性两个类别,如 ...

  9. 一天一经典Efficient Estimation of Word Representations in Vector Space

    摘要 本文提出了两种从大规模数据集中计算连续向量表示(Continuous Vector Representation)的计算模型架构.这些表示的有效性是通过词相似度任务(Word Similarit ...

  10. 【大数据作业十一】分布式并行计算MapReduce

    作业要求:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3319 1.用自己的话阐明Hadoop平台上HDFS和MapReduce的功 ...