1. 程式人生 > 程式設計 >Influx Sql系列教程九:query資料查詢基本篇二

Influx Sql系列教程九:query資料查詢基本篇二

前面一篇介紹了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,age=15,id=15
> insert yhh,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,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. 一灰灰Blogliuyueyi.github.io/hexblog

一灰灰的個人部落格,記錄所有學習和工作中的博文,歡迎大家前去逛逛

2. 宣告

盡信書則不如,已上內容,純屬一家之言,因個人能力有限,難免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激

3. 掃描關注

一灰灰blog

image