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
https://docs.influxdata.com/influxdb/v1.7/concepts/glossary/#point
在influxdb中,你可以將一條mysql中的記錄簡單的理解為一個point,它由四個組件
- measurement
- tag set
- field set
- timestamp
每個point是根據 timestamp + series
來保證唯一性。
關於point可以怎么理解呢?因為influxdb是時序數據庫,簡單來講就是每個數據都是時間軸上的一個點,這些數據與時間強相關,其中的tag用來檢索,field用來記錄一些信息,measurement用來將相同類型的數據歸集
4. series
https://docs.influxdata.com/influxdb/v1.7/concepts/glossary/#series
上面說到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,name=一灰灰 age=26,id=10,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=一灰灰
>
我們插入兩個point
到yhh
這個measurement
中,但是他們的tag相同都是一灰灰
,此時我們查看series時,發現只有一條yhh,name=一灰灰
,包含measurement
和tag set
接下來我們試一下,新增一個tag,series是否會增加呢?
> insert yhh,name=一灰灰2 age=30,id=11,blog="http://blog.hhui.top"
> insert yhh,name=一灰灰3,phone=110 age=30,id=11,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,name=一灰灰3,phone=110
官方定義中series還與保存策略有關,前面兩個case都是默認的保存測錄,我們現在在新的保存策略中測試
> create retention policy "1D" on test duration 1d replication 1
> insert into "1D" yhh,name=一灰灰4 age=26,id=10,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,name=一灰灰
yhh,name=一灰灰2
yhh,name=一灰灰3,phone=110
yhh,name=一灰灰4
插入到"1D"保存策略中的point也構成了一個series: yhh,name=一灰灰4
注意
show series
預計中還支持基於tag
的where
查詢,下面是一個簡單的示例
show series from yhh where "name" = '一灰灰'
key
---
yhh,name=一灰灰
> show series from yhh where phone != ''
key
---
yhh,name=一灰灰3,phone=110
II. 其他
0. 系列博文
- 190721-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/concepts/glossary
- https://docs.influxdata.com/influxdb/v1.7/query_language/schema_exploration
1. 一灰灰Blog: https://liuyueyi.github.io/hexblog
一灰灰的個人博客,記錄所有學習和工作中的博文,歡迎大家前去逛逛
2. 聲明
盡信書則不如,已上內容,純屬一家之言,因個人能力有限,難免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激
- 微博地址: 小灰灰Blog
- QQ: 一灰灰/3302797840
3. 掃描關注
一灰灰blog