Schema Template

IoTDB supports the schema template function, enabling different entities of the same type to share metadata, reduce the memory usage of metadata, and simplify the management of numerous entities and measurements.

Note: The schema keyword in the following statements can be omitted.

Create Schema Template

The SQL syntax for creating a metadata template is as follows:

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

Example 1: Create a template containing two non-aligned timeseires

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

Example 2: Create a template containing a group of aligned timeseires

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

The lat and lon measurements are aligned.

Set Schema Template

After a schema template is created, it should be set to specific path before creating related timeseries or insert data.

It should be ensured that the related database has been set before setting template.

It is recommended to set schema template to database path. It is not suggested to set schema template to some path above database

The SQL Statement for setting schema template is as follow:

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

Activate Schema Template

After setting the schema template, with the system enabled to auto create schema, you can insert data into the timeseries. For example, suppose there’s a database root.sg1 and t1 has been set to root.sg1.d1, then timeseries like root.sg1.d1.temperature and root.sg1.d1.status are available and data points can be inserted.

Attention: Before inserting data or the system not enabled to auto create schema, timeseries defined by the schema template will not be created. You can use the following SQL statement to create the timeseries or activate the schema template, act before inserting data:

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

Example: Execute the following statement

  1. IoTDB> set schema template t1 to root.sg1.d1
  2. IoTDB> set schema template t2 to root.sg1.d2
  3. IoTDB> create timeseries of schema template on root.sg1.d1
  4. IoTDB> create timeseries of schema template on root.sg1.d2

Show the time series:

  1. show timeseries root.sg1.**
  1. +-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+
  2. | timeseries|alias| database|dataType|encoding|compression|tags|attributes|deadband|deadband parameters|
  3. +-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+
  4. |root.sg1.d1.temperature| null| root.sg1| FLOAT| RLE| SNAPPY|null| null| null| null|
  5. | root.sg1.d1.status| null| root.sg1| BOOLEAN| PLAIN| SNAPPY|null| null| null| null|
  6. | root.sg1.d2.lon| null| root.sg1| FLOAT| GORILLA| SNAPPY|null| null| null| null|
  7. | root.sg1.d2.lat| null| root.sg1| FLOAT| GORILLA| SNAPPY|null| null| null| null|
  8. +-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+

Show the devices:

  1. show devices root.sg1.**
  1. +---------------+---------+
  2. | devices|isAligned|
  3. +---------------+---------+
  4. | root.sg1.d1| false|
  5. | root.sg1.d2| true|
  6. +---------------+---------+

Show Schema Template

  • Show all schema templates

The SQL statement looks like this:

  1. IoTDB> show schema templates

The execution result is as follows:

  1. +-------------+
  2. |template name|
  3. +-------------+
  4. | t2|
  5. | t1|
  6. +-------------+
  • Show nodes under in schema template

The SQL statement looks like this:

  1. IoTDB> show nodes in schema template t1

The execution result is as follows:

  1. +-----------+--------+--------+-----------+
  2. |child nodes|dataType|encoding|compression|
  3. +-----------+--------+--------+-----------+
  4. |temperature| FLOAT| RLE| SNAPPY|
  5. | status| BOOLEAN| PLAIN| SNAPPY|
  6. +-----------+--------+--------+-----------+
  • Show the path prefix where a schema template is set
  1. IoTDB> show paths set schema template t1

The execution result is as follows:

  1. +-----------+
  2. |child paths|
  3. +-----------+
  4. |root.sg1.d1|
  5. +-----------+
  • Show the path prefix where a schema template is used (i.e. the time series has been created)
  1. IoTDB> show paths using schema template t1

The execution result is as follows:

  1. +-----------+
  2. |child paths|
  3. +-----------+
  4. |root.sg1.d1|
  5. +-----------+

Deactivate Schema Template

To delete a group of timeseries represented by schema template, namely deactivate the schema template, use the following SQL statement:

  1. IoTDB> delete timeseries of schema template t1 from root.sg1.d1

or

  1. IoTDB> deactivate schema template t1 from root.sg1.d1

The deactivation supports batch process.

  1. IoTDB> delete timeseries of schema template t1 from root.sg1.*, root.sg2.*

or

  1. IoTDB> deactivate schema template t1 from root.sg1.*, root.sg2.*

If the template name is not provided in sql, all template activation on paths matched by given path pattern will be removed.

Unset Schema Template

The SQL Statement for unsetting schema template is as follow:

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

Attention: It should be guaranteed that none of the timeseries represented by the target schema template exists, before unset it. It can be achieved by deactivation operation.

Drop Schema Template

The SQL Statement for dropping schema template is as follow:

  1. IoTDB> drop schema template t1

Attention: Dropping an already set template is not supported.