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

文章目录
  1. 1. tag
  2. 2. field
  3. 3. point
  4. 4. series
  • II. 其他
    1. 0. 系列博文
    2. 1. 一灰灰Blog: https://liuyueyi.github.io/hexblog
    3. 2. 声明
    4. 3. 扫描关注
  • 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的语法如下

    1
    show tag keys on <database> from <measurement>

    下面给出一个实际的例子, insert语句后面会说到,我们塞入的一条数据,指定name为tag,另外三个为field

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    > 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的使用姿势

    1
    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有哪些
    • 连接符号可以为:等于 =, 不等于:!=, <>, 正则:=~, !~
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    > 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的语句如下

    1
    show field keys on <database> from <measurement>

    下面演示一下查看的姿势

    1
    2
    3
    4
    5
    6
    7
    > 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的命令如下

    1
    show series on <database> from <measurement>

    下面是几个实例辅助说明

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    > 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=一灰灰
    >

    我们插入两个pointyhh这个measurement中,但是他们的tag相同都是一灰灰,此时我们查看series时,发现只有一条yhh,name=一灰灰,包含measurementtag set

    接下来我们试一下,新增一个tag,series是否会增加呢?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    > 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都是默认的保存测录,我们现在在新的保存策略中测试

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    > 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预计中还支持基于tagwhere查询,下面是一个简单的示例

    1
    2
    3
    4
    5
    6
    7
    8
    show series from yhh where "name" = '一灰灰'
    key
    ---
    yhh,name=一灰灰
    > show series from yhh where phone != ''
    key
    ---
    yhh,name=一灰灰3,phone=110

    II. 其他

    0. 系列博文

    参考博文

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

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

    2. 声明

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

    3. 扫描关注

    一灰灰blog

    QrCode

    知识星球

    goals

    评论

    Your browser is out-of-date!

    Update your browser to view this website correctly. Update my browser now

    ×