跳至主要內容

...大约 3 分钟

元数据模板

IoTDB 支持元数据模板功能,实现同类型不同实体的物理量元数据共享,减少元数据内存占用,同时简化同类型实体的管理。

注:以下语句中的 schema 关键字可以省略。

创建元数据模板

创建元数据模板的 SQL 语法如下:

CREATE SCHEMA? TEMPLATE <templateName> ALIGNED? '(' <measurementId> <attributeClauses> [',' <measurementId> <attributeClauses>]+ ')'

示例1: 创建包含两个非对齐序列的元数据模板

IoTDB> create schema template t1 (temperature FLOAT encoding=RLE, status BOOLEAN encoding=PLAIN compression=SNAPPY)

示例2: 创建包含一组对齐序列的元数据模板

IoTDB> create schema template t2 aligned (lat FLOAT encoding=Gorilla, lon FLOAT encoding=Gorilla)

其中,物理量 latlon 是对齐的。

挂载元数据模板

为了更好地适配未来版本的更新及各模块的协作,我们强烈建议您将模板设置在存储组及存储组下层的节点中。

挂载元数据模板的 SQL 语句如下所示:

IoTDB> set schema template t1 to root.sg1.d1

挂载好元数据模板后,即可进行数据的写入。例如存储组为 root.sg1,模板 t1 被挂载到了节点 root.sg1.d1,那么可直接向时间序列(如 root.sg1.d1.temperature 和 root.sg1.d1.status)写入时间序列数据,该时间序列已可被当作正常创建的序列使用。

注意:在插入数据之前,模板定义的时间序列不会被创建。可以使用如下SQL语句在插入数据前创建时间序列:

IoTDB> create timeseries of schema template on root.sg1.d1

示例: 执行以下语句

set schema template t1 to root.sg1.d1
set schema template t2 to root.sg1.d2
create timeseries of schema template on root.sg1.d1
create timeseries of schema template on root.sg1.d2

查看此时的时间序列:

show timeseries root.sg1.**
+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+
|             timeseries|alias|storage group|dataType|encoding|compression|tags|attributes|deadband|deadband parameters|
+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+
|root.sg1.d1.temperature| null|     root.sg1|   FLOAT|     RLE|     SNAPPY|null|      null|    null|               null|
|     root.sg1.d1.status| null|     root.sg1| BOOLEAN|   PLAIN|     SNAPPY|null|      null|    null|               null|
|        root.sg1.d2.lon| null|     root.sg1|   FLOAT| GORILLA|     SNAPPY|null|      null|    null|               null|
|        root.sg1.d2.lat| null|     root.sg1|   FLOAT| GORILLA|     SNAPPY|null|      null|    null|               null|
+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+

查看此时的设备:

show devices root.sg1.**
+---------------+---------+
|        devices|isAligned|
+---------------+---------+
|    root.sg1.d1|    false|
|    root.sg1.d2|     true|
+---------------+---------+

查看元数据模板

  • 查看所有元数据模板

SQL 语句如下所示:

IoTDB> show schema templates

执行结果如下:

+-------------+
|template name|
+-------------+
|           t2|
|           t1|
+-------------+
  • 查看某个元数据模板下的物理量

SQL 语句如下所示:

IoTDB> show nodes in schema template t1

执行结果如下:

+-----------+--------+--------+-----------+
|child nodes|dataType|encoding|compression|
+-----------+--------+--------+-----------+
|temperature|   FLOAT|     RLE|     SNAPPY|
|     status| BOOLEAN|   PLAIN|     SNAPPY|
+-----------+--------+--------+-----------+
  • 查看挂载了某个元数据模板的路径前缀
IoTDB> show paths set schema template t1

执行结果如下:

+-----------+
|child paths|
+-----------+
|root.sg1.d1|
+-----------+
  • 查看使用了某个元数据模板(即序列已创建)的路径前缀
IoTDB> show paths using schema template t1

执行结果如下:

+-----------+
|child paths|
+-----------+
|root.sg1.d1|
+-----------+

解除元数据模板

对于挂载了元数据模板的节点或其孩子节点,如果:1)曾按模板中的序列写入了数据,或2)使用了 create timeseries of schema template,则在使用以下命令之前,不能够卸载或删除元数据模板:

IoTDB> deactivate schema template t1 from root.sg.d1

注意:这一操作会删除对应节点下按照模板中的序列写入的数据。

卸载元数据模板

卸载元数据模板的 SQL 语句如下所示:

IoTDB> unset schema template t1 from root.sg1.d1

删除元数据模板

删除元数据模板的 SQL 语句如下所示:

IoTDB> drop schema template t1

注意:不能删除尚未卸载的模板。

Copyright © 2024 The Apache Software Foundation.
Apache and the Apache feather logo are trademarks of The Apache Software Foundation

Have a question? Connect with us on QQ, WeChat, or Slack. Join the community now.