| <!-- |
| |
| Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you under the Apache License, Version 2.0 (the |
| "License"); you may not use this file except in compliance |
| with the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, |
| software distributed under the License is distributed on an |
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| KIND, either express or implied. See the License for the |
| specific language governing permissions and limitations |
| under the License. |
| |
| --> |
| |
| ## 元数据模板 |
| |
| IoTDB 支持元数据模板功能,实现同类型不同实体的物理量元数据共享,减少元数据内存占用,同时简化同类型实体的管理。 |
| |
| 注:以下语句中的 `schema` 关键字可以省略。 |
| |
| ### 创建元数据模板 |
| |
| 创建元数据模板的 SQL 语法如下: |
| |
| ```sql |
| CREATE SCHEMA? TEMPLATE <templateName> ALIGNED? '(' <measurementId> <attributeClauses> [',' <measurementId> <attributeClauses>]+ ')' |
| ``` |
| |
| **示例1:** 创建包含两个非对齐序列的元数据模板 |
| |
| ```shell |
| IoTDB> create schema template t1 (temperature FLOAT encoding=RLE, status BOOLEAN encoding=PLAIN compression=SNAPPY) |
| ``` |
| |
| **示例2:** 创建包含一组对齐序列的元数据模板 |
| |
| ```shell |
| IoTDB> create schema template t2 aligned (lat FLOAT encoding=Gorilla, lon FLOAT encoding=Gorilla) |
| ``` |
| |
| 其中,物理量 `lat` 和 `lon` 是对齐的。 |
| |
| ### 挂载元数据模板 |
| |
| **为了更好地适配未来版本的更新及各模块的协作,我们强烈建议您将模板设置在存储组及存储组下层的节点中。** |
| |
| 挂载元数据模板的 SQL 语句如下所示: |
| |
| ```shell |
| IoTDB> set schema template t1 to root.sg1.d1 |
| ``` |
| |
| 挂载好元数据模板后,即可进行数据的写入。例如存储组为 root.sg1,模板 t1 被挂载到了节点 root.sg1.d1,那么可直接向时间序列(如 root.sg1.d1.temperature 和 root.sg1.d1.status)写入时间序列数据,该时间序列已可被当作正常创建的序列使用。 |
| |
| **注意**:在插入数据之前,模板定义的时间序列不会被创建。可以使用如下SQL语句在插入数据前创建时间序列: |
| |
| ```shell |
| IoTDB> create timeseries of schema template on root.sg1.d1 |
| ``` |
| |
| **示例:** 执行以下语句 |
| ```shell |
| 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 |
| ``` |
| |
| 查看此时的时间序列: |
| ```sql |
| show timeseries root.sg1.** |
| ``` |
| |
| ```shell |
| +-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+ |
| | 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| |
| +-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+ |
| ``` |
| |
| 查看此时的设备: |
| ```sql |
| show devices root.sg1.** |
| ``` |
| |
| ```shell |
| +---------------+---------+ |
| | devices|isAligned| |
| +---------------+---------+ |
| | root.sg1.d1| false| |
| | root.sg1.d2| true| |
| +---------------+---------+ |
| ``` |
| |
| ### 查看元数据模板 |
| |
| - 查看所有元数据模板 |
| |
| SQL 语句如下所示: |
| |
| ```shell |
| IoTDB> show schema templates |
| ``` |
| |
| 执行结果如下: |
| ```shell |
| +-------------+ |
| |template name| |
| +-------------+ |
| | t2| |
| | t1| |
| +-------------+ |
| ``` |
| |
| - 查看某个元数据模板下的物理量 |
| |
| SQL 语句如下所示: |
| |
| ```shell |
| IoTDB> show nodes in schema template t1 |
| ``` |
| |
| 执行结果如下: |
| ```shell |
| +-----------+--------+--------+-----------+ |
| |child nodes|dataType|encoding|compression| |
| +-----------+--------+--------+-----------+ |
| |temperature| FLOAT| RLE| SNAPPY| |
| | status| BOOLEAN| PLAIN| SNAPPY| |
| +-----------+--------+--------+-----------+ |
| ``` |
| |
| - 查看挂载了某个元数据模板的路径前缀 |
| |
| ```shell |
| IoTDB> show paths set schema template t1 |
| ``` |
| |
| 执行结果如下: |
| ```shell |
| +-----------+ |
| |child paths| |
| +-----------+ |
| |root.sg1.d1| |
| +-----------+ |
| ``` |
| |
| - 查看使用了某个元数据模板(即序列已创建)的路径前缀 |
| |
| ```shell |
| IoTDB> show paths using schema template t1 |
| ``` |
| |
| 执行结果如下: |
| ```shell |
| +-----------+ |
| |child paths| |
| +-----------+ |
| |root.sg1.d1| |
| +-----------+ |
| ``` |
| |
| ### 解除元数据模板 |
| |
| 对于挂载了元数据模板的节点或其孩子节点,如果:1)曾按模板中的序列写入了数据,或2)使用了 `create timeseries of schema template`,则在使用以下命令之前,不能够卸载或删除元数据模板: |
| |
| ```shell |
| IoTDB> deactivate schema template t1 from root.sg.d1 |
| ``` |
| |
| **注意**:这一操作会删除对应节点下按照模板中的序列写入的数据。 |
| |
| ### 卸载元数据模板 |
| |
| 卸载元数据模板的 SQL 语句如下所示: |
| |
| ```shell |
| IoTDB> unset schema template t1 from root.sg1.d1 |
| ``` |
| |
| ### 删除元数据模板 |
| |
| 删除元数据模板的 SQL 语句如下所示: |
| |
| ```shell |
| IoTDB> drop schema template t1 |
| ``` |
| |
| **注意**:不能删除尚未卸载的模板。 |
| |