1. 程式人生 > 程式設計 >Influx Sql系列教程四:series/point/tag/field

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

influxdb中的一條記錄point,主要可以分為三類,必須存在的time(時間),string型別的tag,以及其他成員field;而series則是一個measurement中儲存策略和tag集構成;本篇教程將介紹一些這幾個概念

1. tag

influxdb資料結構中記錄元資料(metadata)的kv對,不要求必須存在,tag key/value 都是字串型別,而且會建立索引,因此基於tag進行查詢效率比單純的基於field進行查詢是要高的;後續的一些sql也會發現,某些查詢只能基於tag

重點提煉

  • tag key/value: 字串型別
  • 有索引

常見的查詢tag的語法如下

show tag keys on <database> from <measurement>
複製程式碼

下面給出一個實際的例子,insert語句後面會說到,我們塞入的一條資料,指定name為tag,另外三個為field

> insert yhh,name=一灰灰 age=26,id=10,blog="http://blog.hhui.top"
> select * from yhh
name: yhh
time                age blog                 id name
----                --- ----                 -- ----
1563888301725811554 26  http://blog.hhui.top 10 一灰灰
> show tag keys from yhh
name: yhh
tagKey
------
name
複製程式碼

上面是獲取tag keys的查詢方式,下面介紹下查詢tag values的使用姿勢

show tag values on <database> from <measurement> with KEY [ [<operator> "<tag_key>" | <regular_expression>] | [IN ("<tag_key1>","<tag_key2")]] [WHERE <tag_key> <operator> ['<tag_value>' | <regular_expression>]] [LIMIT_clause] [OFFSET_clause]
複製程式碼
  • with key 後面帶上查詢條件,必須存在,如查詢匯率表中,base_symbol有哪些
  • 連線符號可以為:等於 =,不等於:!=,<>,正則:=~,!~
> show tag values from currency_rate with key="base"
name: currency_rate
key  value
---  -----
base AUD
base CAD
base CNY
base DKK
base EUR
base GBP
base HKD
base IDR
base INR
base JPY
base KRW
base NZD
base PHP
base PLN
base RUB
base SGD
base THB
base TRY
base UAH
base USD
複製程式碼

2. field

成員,也可以理解為一條記錄中,不需要建立索引的資料,一般來說,不太會有參與查詢語句建設的可以設定為field

區別與tag,field有下面幾個特性

  • 型別可以為:浮點,字串,整形
  • 沒有索引

檢視field key的語句如下

show field keys on <database> from <measurement>
複製程式碼

下面演示一下檢視的姿勢

> show field keys from yhh
name: yhh
fieldKey fieldType
-------- ---------
age      float
blog     string
id       float
複製程式碼

3. point

docs.influxdata.com/influxdb/v1…

在influxdb中,你可以將一條mysql中的記錄簡單的理解為一個point,它由四個元件

  • measurement
  • tag set
  • field set
  • timestamp

每個point是根據 timestamp + series 來保證唯一性。

關於point可以怎麼理解呢?因為influxdb是時序資料庫,簡單來講就是每個資料都是時間軸上的一個點,這些資料與時間強相關,其中的tag用來檢索,field用來記錄一些資訊,measurement用來將相同型別的資料歸集

4. series

docs.influxdata.com/influxdb/v1…

上面說到point的唯一性時,說到了series,這個概念又是啥呢?

官方的說明是:

The collection of data in the InfluxDB data structure that share a measurement,tag set,and retention policy.

influxdb中measurement + tags set + retention policy 組成的資料集合

直接看定義可能有點懵逼,官方提供檢視series的命令如下

show series on <database> from <measurement>
複製程式碼

下面是幾個例項輔助說明

> insert yhh,blog="http://blog.hhui.top"
> insert yhh,name=一灰灰 age=30,id=11,blog="http://blog.hhui.top"
> select * from yhh;
name: yhh
time                age blog                 id name
----                --- ----                 -- ----
1563889538654374538 26  http://blog.hhui.top 10 一灰灰
1563889547738266214 30  http://blog.hhui.top 11 一灰灰
> show series on test from yhh
key
---
yhh,name=一灰灰
>
複製程式碼

我們插入兩個pointyhh這個measurement中,但是他們的tag相同都是一灰灰,此時我們檢視series時,發現只有一條yhh,name=一灰灰,包含measurementtag set

接下來我們試一下,新增一個tag,series是否會增加呢?

> insert yhh,name=一灰灰2 age=30,name=一灰灰3,phone=110 age=30,blog="http://blog.hhui.top"
> 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 series on test from yhh
key
---
yhh,name=一灰灰
yhh,name=一灰灰2
yhh,phone=110
複製程式碼

官方定義中series還與儲存策略有關,前面兩個case都是預設的儲存測錄,我們現在在新的儲存策略中測試

> create retention policy "1D" on test duration 1d replication 1
> insert into "1D" yhh,name=一灰灰4 age=26,blog="http://blog.hhui.top"
> 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
> select * from "1D".yhh
name: yhh
time                age blog                 id name phone
----                --- ----                 -- ---- -----
1563890614849474879 26  http://blog.hhui.top 10 一灰灰4
> show series
key
---
yhh,phone=110
yhh,name=一灰灰4
複製程式碼

插入到"1D"儲存策略中的point也構成了一個series: yhh,name=一灰灰4

注意

show series預計中還支援基於tagwhere查詢,下面是一個簡單的示例

show series from yhh where "name" = '一灰灰'
key
---
yhh,name=一灰灰
> show series from yhh where phone != ''
key
---
yhh,phone=110
複製程式碼

II. 其他

0. 系列博文

參考博文

1. 一灰灰Blogliuyueyi.github.io/hexblog

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

2. 宣告

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

3. 掃描關注

一灰灰blog

image