时间序列管理

创建时间序列

根据建立的数据模型,我们可以分别在两个存储组中创建相应的时间序列。创建时间序列的 SQL 语句如下所示:

  1. IoTDB > create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN,encoding=PLAIN
  2. IoTDB > create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT,encoding=RLE
  3. IoTDB > create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT,encoding=PLAIN
  4. IoTDB > create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN,encoding=PLAIN
  5. IoTDB > create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN,encoding=PLAIN
  6. IoTDB > create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT,encoding=RLE

从 v0.13 起,可以使用简化版的 SQL 语句创建时间序列:

  1. IoTDB > create timeseries root.ln.wf01.wt01.status BOOLEAN encoding=PLAIN
  2. IoTDB > create timeseries root.ln.wf01.wt01.temperature FLOAT encoding=RLE
  3. IoTDB > create timeseries root.ln.wf02.wt02.hardware TEXT encoding=PLAIN
  4. IoTDB > create timeseries root.ln.wf02.wt02.status BOOLEAN encoding=PLAIN
  5. IoTDB > create timeseries root.sgcc.wf03.wt01.status BOOLEAN encoding=PLAIN
  6. IoTDB > create timeseries root.sgcc.wf03.wt01.temperature FLOAT encoding=RLE

需要注意的是,当创建时间序列时指定的编码方式与数据类型不对应时,系统会给出相应的错误提示,如下所示:

  1. IoTDB> create timeseries root.ln.wf02.wt02.status WITH DATATYPE=BOOLEAN, ENCODING=TS_2DIFF
  2. error: encoding TS_2DIFF does not support BOOLEAN

详细的数据类型与编码方式的对应列表请参见 编码方式

创建对齐时间序列

创建一组对齐时间序列的SQL语句如下所示:

  1. IoTDB> CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT encoding=PLAIN compressor=SNAPPY, longitude FLOAT encoding=PLAIN compressor=SNAPPY)

一组对齐序列中的序列可以有不同的数据类型、编码方式以及压缩方式。

对齐的时间序列也支持设置别名、标签、属性。

删除时间序列

我们可以使用(DELETE | DROP) TimeSeries <PathPattern>语句来删除我们之前创建的时间序列。SQL 语句如下所示:

  1. IoTDB> delete timeseries root.ln.wf01.wt01.status
  2. IoTDB> delete timeseries root.ln.wf01.wt01.temperature, root.ln.wf02.wt02.hardware
  3. IoTDB> delete timeseries root.ln.wf02.*
  4. IoTDB> drop timeseries root.ln.wf02.*

查看时间序列

  • SHOW LATEST? TIMESERIES pathPattern? whereClause? limitClause?

    SHOW TIMESERIES 中可以有四种可选的子句,查询结果为这些时间序列的所有信息

时间序列信息具体包括:时间序列路径名,database,Measurement 别名,数据类型,编码方式,压缩方式,属性和标签。

示例:

  • SHOW TIMESERIES

    展示系统中所有的时间序列信息

  • SHOW TIMESERIES <Path>

    返回给定路径的下的所有时间序列信息。其中 Path 需要为一个时间序列路径或路径模式。例如,分别查看root路径和root.ln路径下的时间序列,SQL 语句如下所示:

  1. IoTDB> show timeseries root.**
  2. IoTDB> show timeseries root.ln.**

执行结果分别为:

  1. +-------------------------------+--------+-------------+--------+--------+-----------+-------------------------------------------+--------------------------------------------------------+--------+-------------------+
  2. | timeseries| alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters|
  3. +-------------------------------+--------+-------------+--------+--------+-----------+-------------------------------------------+--------------------------------------------------------+--------+-------------------+
  4. |root.sgcc.wf03.wt01.temperature| null| root.sgcc| FLOAT| RLE| SNAPPY| null| null| null| null|
  5. | root.sgcc.wf03.wt01.status| null| root.sgcc| BOOLEAN| PLAIN| SNAPPY| null| null| null| null|
  6. | root.turbine.d1.s1|newAlias| root.turbine| FLOAT| RLE| SNAPPY|{"newTag1":"newV1","tag4":"v4","tag3":"v3"}|{"attr2":"v2","attr1":"newV1","attr4":"v4","attr3":"v3"}| null| null|
  7. | root.ln.wf02.wt02.hardware| null| root.ln| TEXT| PLAIN| SNAPPY| null| null| null| null|
  8. | root.ln.wf02.wt02.status| null| root.ln| BOOLEAN| PLAIN| SNAPPY| null| null| null| null|
  9. | root.ln.wf01.wt01.temperature| null| root.ln| FLOAT| RLE| SNAPPY| null| null| null| null|
  10. | root.ln.wf01.wt01.status| null| root.ln| BOOLEAN| PLAIN| SNAPPY| null| null| null| null|
  11. +-------------------------------+--------+-------------+--------+--------+-----------+-------------------------------------------+--------------------------------------------------------+--------+-------------------+
  12. Total line number = 7
  13. It costs 0.016s
  14. +-----------------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+
  15. | timeseries|alias| database|dataType|encoding|compression|tags|attributes|deadband|deadband parameters|
  16. +-----------------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+
  17. | root.ln.wf02.wt02.hardware| null| root.ln| TEXT| PLAIN| SNAPPY|null| null| null| null|
  18. | root.ln.wf02.wt02.status| null| root.ln| BOOLEAN| PLAIN| SNAPPY|null| null| null| null|
  19. |root.ln.wf01.wt01.temperature| null| root.ln| FLOAT| RLE| SNAPPY|null| null| null| null|
  20. | root.ln.wf01.wt01.status| null| root.ln| BOOLEAN| PLAIN| SNAPPY|null| null| null| null|
  21. +-----------------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+
  22. Total line number = 4
  23. It costs 0.004s
  • SHOW TIMESERIES LIMIT INT OFFSET INT

    只返回从指定下标开始的结果,最大返回条数被 LIMIT 限制,用于分页查询。例如:

  1. show timeseries root.ln.** limit 10 offset 10
  • SHOW LATEST TIMESERIES

    表示查询出的时间序列需要按照最近插入时间戳降序排列

需要注意的是,当查询路径不存在时,系统会返回 0 条时间序列。

统计时间序列总数

IoTDB 支持使用COUNT TIMESERIES<Path>来统计一条路径中的时间序列个数。SQL 语句如下所示:

  1. IoTDB > COUNT TIMESERIES root.**
  2. IoTDB > COUNT TIMESERIES root.ln.**
  3. IoTDB > COUNT TIMESERIES root.ln.*.*.status
  4. IoTDB > COUNT TIMESERIES root.ln.wf01.wt01.status

除此之外,还可以通过定义LEVEL来统计指定层级下的时间序列个数。这条语句可以用来统计每一个设备下的传感器数量,语法为:COUNT TIMESERIES <Path> GROUP BY LEVEL=<INTEGER>

例如有如下时间序列(可以使用show timeseries展示所有时间序列):

  1. +-------------------------------+--------+-------------+--------+--------+-----------+-------------------------------------------+--------------------------------------------------------+--------+-------------------+
  2. | timeseries| alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters|
  3. +-------------------------------+--------+-------------+--------+--------+-----------+-------------------------------------------+--------------------------------------------------------+--------+-------------------+
  4. |root.sgcc.wf03.wt01.temperature| null| root.sgcc| FLOAT| RLE| SNAPPY| null| null| null| null|
  5. | root.sgcc.wf03.wt01.status| null| root.sgcc| BOOLEAN| PLAIN| SNAPPY| null| null| null| null|
  6. | root.turbine.d1.s1|newAlias| root.turbine| FLOAT| RLE| SNAPPY|{"newTag1":"newV1","tag4":"v4","tag3":"v3"}|{"attr2":"v2","attr1":"newV1","attr4":"v4","attr3":"v3"}| null| null|
  7. | root.ln.wf02.wt02.hardware| null| root.ln| TEXT| PLAIN| SNAPPY| {"unit":"c"}| null| null| null|
  8. | root.ln.wf02.wt02.status| null| root.ln| BOOLEAN| PLAIN| SNAPPY| {"description":"test1"}| null| null| null|
  9. | root.ln.wf01.wt01.temperature| null| root.ln| FLOAT| RLE| SNAPPY| null| null| null| null|
  10. | root.ln.wf01.wt01.status| null| root.ln| BOOLEAN| PLAIN| SNAPPY| null| null| null| null|
  11. +-------------------------------+--------+-------------+--------+--------+-----------+-------------------------------------------+--------------------------------------------------------+--------+-------------------+
  12. Total line number = 7
  13. It costs 0.004s

那么 Metadata Tree 如下所示:

时间序列操作 - 图1

可以看到,root被定义为LEVEL=0。那么当你输入如下语句时:

  1. IoTDB > COUNT TIMESERIES root.** GROUP BY LEVEL=1
  2. IoTDB > COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2
  3. IoTDB > COUNT TIMESERIES root.ln.wf01.* GROUP BY LEVEL=2

你将得到以下结果:

  1. IoTDB> COUNT TIMESERIES root.** GROUP BY LEVEL=1
  2. +------------+-----------------+
  3. | column|count(timeseries)|
  4. +------------+-----------------+
  5. | root.sgcc| 2|
  6. |root.turbine| 1|
  7. | root.ln| 4|
  8. +------------+-----------------+
  9. Total line number = 3
  10. It costs 0.002s
  11. IoTDB > COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2
  12. +------------+-----------------+
  13. | column|count(timeseries)|
  14. +------------+-----------------+
  15. |root.ln.wf02| 2|
  16. |root.ln.wf01| 2|
  17. +------------+-----------------+
  18. Total line number = 2
  19. It costs 0.002s
  20. IoTDB > COUNT TIMESERIES root.ln.wf01.* GROUP BY LEVEL=2
  21. +------------+-----------------+
  22. | column|count(timeseries)|
  23. +------------+-----------------+
  24. |root.ln.wf01| 2|
  25. +------------+-----------------+
  26. Total line number = 1
  27. It costs 0.002s

注意:时间序列的路径只是过滤条件,与 level 的定义无关。

标签点管理

我们可以在创建时间序列的时候,为它添加别名和额外的标签和属性信息。

标签和属性的区别在于:

  • 标签可以用来查询时间序列路径,会在内存中维护标点到时间序列路径的倒排索引:标签 -> 时间序列路径
  • 属性只能用时间序列路径来查询:时间序列路径 -> 属性

所用到的扩展的创建时间序列的 SQL 语句如下所示:

  1. create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT, encoding=RLE, compression=SNAPPY tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2)

括号里的tempratures1这个传感器的别名。 我们可以在任何用到s1的地方,将其用temprature代替,这两者是等价的。

IoTDB 同时支持在查询语句中 使用 AS 函数 设置别名。二者的区别在于:AS 函数设置的别名用于替代整条时间序列名,且是临时的,不与时间序列绑定;而上文中的别名只作为传感器的别名,与其绑定且可与原传感器名等价使用。

注意:额外的标签和属性信息总的大小不能超过tag_attribute_total_size.

  • 标签点属性更新 创建时间序列后,我们也可以对其原有的标签点属性进行更新,主要有以下六种更新方式:
  • 重命名标签或属性
  1. ALTER timeseries root.turbine.d1.s1 RENAME tag1 TO newTag1
  • 重新设置标签或属性的值
  1. ALTER timeseries root.turbine.d1.s1 SET newTag1=newV1, attr1=newV1
  • 删除已经存在的标签或属性
  1. ALTER timeseries root.turbine.d1.s1 DROP tag1, tag2
  • 添加新的标签
  1. ALTER timeseries root.turbine.d1.s1 ADD TAGS tag3=v3, tag4=v4
  • 添加新的属性
  1. ALTER timeseries root.turbine.d1.s1 ADD ATTRIBUTES attr3=v3, attr4=v4
  • 更新插入别名,标签和属性

如果该别名,标签或属性原来不存在,则插入,否则,用新值更新原来的旧值

  1. ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(tag2=newV2, tag3=v3) ATTRIBUTES(attr3=v3, attr4=v4)
  • 使用标签作为过滤条件查询时间序列
  1. SHOW TIMESERIES (<`PathPattern`>)? WhereClause

返回给定路径的下的所有满足条件的时间序列信息,SQL 语句如下所示:

  1. ALTER timeseries root.ln.wf02.wt02.hardware ADD TAGS unit=c
  2. ALTER timeseries root.ln.wf02.wt02.status ADD TAGS description=test1
  3. show timeseries root.ln.** where unit=c
  4. show timeseries root.ln.** where description contains 'test1'

执行结果分别为:

  1. +--------------------------+-----+-------------+--------+--------+-----------+------------+----------+--------+-------------------+
  2. | timeseries|alias| database|dataType|encoding|compression| tags|attributes|deadband|deadband parameters|
  3. +--------------------------+-----+-------------+--------+--------+-----------+------------+----------+--------+-------------------+
  4. |root.ln.wf02.wt02.hardware| null| root.ln| TEXT| PLAIN| SNAPPY|{"unit":"c"}| null| null| null|
  5. +--------------------------+-----+-------------+--------+--------+-----------+------------+----------+--------+-------------------+
  6. Total line number = 1
  7. It costs 0.005s
  8. +------------------------+-----+-------------+--------+--------+-----------+-----------------------+----------+--------+-------------------+
  9. | timeseries|alias| database|dataType|encoding|compression| tags|attributes|deadband|deadband parameters|
  10. +------------------------+-----+-------------+--------+--------+-----------+-----------------------+----------+--------+-------------------+
  11. |root.ln.wf02.wt02.status| null| root.ln| BOOLEAN| PLAIN| SNAPPY|{"description":"test1"}| null| null| null|
  12. +------------------------+-----+-------------+--------+--------+-----------+-----------------------+----------+--------+-------------------+
  13. Total line number = 1
  14. It costs 0.004s
  • 使用标签作为过滤条件统计时间序列数量
  1. COUNT TIMESERIES (<`PathPattern`>)? WhereClause
  2. COUNT TIMESERIES (<`PathPattern`>)? WhereClause GROUP BY LEVEL=<INTEGER>

返回给定路径的下的所有满足条件的时间序列的数量,SQL 语句如下所示:

  1. count timeseries
  2. count timeseries root.** where unit = c
  3. count timeseries root.** where unit = c group by level = 2

执行结果分别为:

  1. IoTDB> count timeseries
  2. +-----------------+
  3. |count(timeseries)|
  4. +-----------------+
  5. | 6|
  6. +-----------------+
  7. Total line number = 1
  8. It costs 0.019s
  9. IoTDB> count timeseries root.** where unit = c
  10. +-----------------+
  11. |count(timeseries)|
  12. +-----------------+
  13. | 2|
  14. +-----------------+
  15. Total line number = 1
  16. It costs 0.020s
  17. IoTDB> count timeseries root.** where unit = c group by level = 2
  18. +--------------+-----------------+
  19. | column|count(timeseries)|
  20. +--------------+-----------------+
  21. | root.ln.wf02| 2|
  22. | root.ln.wf01| 0|
  23. |root.sgcc.wf03| 0|
  24. +--------------+-----------------+
  25. Total line number = 3
  26. It costs 0.011s

注意,现在我们只支持一个查询条件,要么是等值条件查询,要么是包含条件查询。当然 where 子句中涉及的必须是标签值,而不能是属性值。

创建对齐时间序列

  1. create aligned timeseries root.sg1.d1(s1 INT32 tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2), s2 DOUBLE tags(tag3=v3, tag4=v4) attributes(attr3=v3, attr4=v4))

执行结果如下:

  1. IoTDB> show timeseries
  2. +--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+
  3. | timeseries|alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters|
  4. +--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+
  5. |root.sg1.d1.s1| null| root.sg1| INT32| RLE| SNAPPY|{"tag1":"v1","tag2":"v2"}|{"attr2":"v2","attr1":"v1"}| null| null|
  6. |root.sg1.d1.s2| null| root.sg1| DOUBLE| GORILLA| SNAPPY|{"tag4":"v4","tag3":"v3"}|{"attr4":"v4","attr3":"v3"}| null| null|
  7. +--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+

支持查询:

  1. IoTDB> show timeseries where tag1='v1'
  2. +--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+
  3. | timeseries|alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters|
  4. +--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+
  5. |root.sg1.d1.s1| null| root.sg1| INT32| RLE| SNAPPY|{"tag1":"v1","tag2":"v2"}|{"attr2":"v2","attr1":"v1"}| null| null|
  6. +--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+

上述对时间序列标签、属性的更新等操作都支持。