IoTDB-SQL Language

Data Definition Language (DDL)

Stroage Group Management

Create Storage Group

According to the storage model we can set up the corresponding storage group. Two SQL statements are supported for creating storage groups, as follows:

  1. IoTDB > set storage group to root.ln
  2. IoTDB > create storage group root.sgcc

We can thus create two storage groups using the above two SQL statements.

It is worth noting that when the path itself or the parent/child layer of the path is already set as a storage group, the path is then not allowed to be set as a storage group. For example, it is not feasible to set root.ln.wf01 as a storage group when two storage groups root.ln and root.sgcc exist. The system gives the corresponding error prompt as shown below:

  1. IoTDB> set storage group to root.ln.wf01
  2. Msg: 300: root.ln has already been set to storage group.
  3. IoTDB> create storage group root.ln.wf01
  4. Msg: 300: root.ln has already been set to storage group.

The LayerName of storage group can only be characters, numbers, underscores and hyphens.

Besides, if deploy on Windows system, the LayerName is case-insensitive, which means it’s not allowed to set storage groups root.ln and root.LN at the same time.

Show Storage Group

After creating the storage group, we can use the SHOW STORAGE GROUP statement and SHOW STORAGE GROUP <PrefixPath> to view the storage groups. The SQL statements are as follows:

  1. IoTDB> show storage group
  2. IoTDB> show storage group root.ln

The result is as follows:

  1. +-------------+
  2. |storage group|
  3. +-------------+
  4. | root.sgcc|
  5. | root.ln|
  6. +-------------+
  7. Total line number = 2
  8. It costs 0.060s

Delete Storage Group

User can use the DELETE STORAGE GROUP <PrefixPath> statement to delete all storage groups under the prefixPath. Please note the data in the storage group will also be deleted.

  1. IoTDB > DELETE STORAGE GROUP root.ln
  2. IoTDB > DELETE STORAGE GROUP root.sgcc
  3. // delete all data, all timeseries and all storage groups
  4. IoTDB > DELETE STORAGE GROUP root.*

Timeseries Management

Create Timeseries

According to the storage model selected before, we can create corresponding timeseries in the two storage groups respectively. The SQL statements for creating timeseries are as follows:

  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

Notice that when in the CRATE TIMESERIES statement the encoding method conflicts with the data type, the system gives the corresponding error prompt as shown below:

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

Please refer to Encoding for correspondence between data type and encoding.

Delete Timeseries

To delete the timeseries we created before, we are able to use DELETE TimeSeries <PrefixPath> statement.

The usage are as follows:

  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.*

Show Timeseries

  • SHOW LATEST? TIMESERIES prefixPath? showWhereClause? limitClause?

    There are four optional clauses added in SHOW TIMESERIES, return information of time series

Timeseries information includes: timeseries path, alias of measurement, storage group it belongs to, data type, encoding type, compression type, tags and attributes.

Examples:

  • SHOW TIMESERIES

    presents all timeseries information in JSON form

  • SHOW TIMESERIES <Path>

    returns all timeseries information under the given <Path>. <Path> needs to be a prefix path or a path with star or a timeseries path. SQL statements are as follows:

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

The results are shown below respectively:

  1. +-------------------------------+--------+-------------+--------+--------+-----------+-------------------------------------------+--------------------------------------------------------+
  2. | timeseries| alias|storage group|dataType|encoding|compression| tags| attributes|
  3. +-------------------------------+--------+-------------+--------+--------+-----------+-------------------------------------------+--------------------------------------------------------+
  4. |root.sgcc.wf03.wt01.temperature| null| root.sgcc| FLOAT| RLE| SNAPPY| null| null|
  5. | root.sgcc.wf03.wt01.status| null| root.sgcc| BOOLEAN| PLAIN| SNAPPY| 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"}|
  7. | root.ln.wf02.wt02.hardware| null| root.ln| TEXT| PLAIN| SNAPPY| null| null|
  8. | root.ln.wf02.wt02.status| null| root.ln| BOOLEAN| PLAIN| SNAPPY| null| null|
  9. | root.ln.wf01.wt01.temperature| null| root.ln| FLOAT| RLE| SNAPPY| null| null|
  10. | root.ln.wf01.wt01.status| null| root.ln| BOOLEAN| PLAIN| SNAPPY| null| null|
  11. +-------------------------------+--------+-------------+--------+--------+-----------+-------------------------------------------+--------------------------------------------------------+
  12. Total line number = 7
  13. It costs 0.016s
  14. +-----------------------------+-----+-------------+--------+--------+-----------+----+----------+
  15. | timeseries|alias|storage group|dataType|encoding|compression|tags|attributes|
  16. +-----------------------------+-----+-------------+--------+--------+-----------+----+----------+
  17. | root.ln.wf02.wt02.hardware| null| root.ln| TEXT| PLAIN| SNAPPY|null| null|
  18. | root.ln.wf02.wt02.status| null| root.ln| BOOLEAN| PLAIN| SNAPPY|null| null|
  19. |root.ln.wf01.wt01.temperature| null| root.ln| FLOAT| RLE| SNAPPY|null| null|
  20. | root.ln.wf01.wt01.status| null| root.ln| BOOLEAN| PLAIN| SNAPPY|null| null|
  21. +-----------------------------+-----+-------------+--------+--------+-----------+----+----------+
  22. Total line number = 4
  23. It costs 0.004s
  • SHOW TIMESERIES (<PrefixPath>)? WhereClause

    returns all the timeseries information that satisfy the where condition and start with the prefixPath SQL statements are as follows:

  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'

The results are shown below respectly:

  1. +--------------------------+-----+-------------+--------+--------+-----------+------------+----------+
  2. | timeseries|alias|storage group|dataType|encoding|compression| tags|attributes|
  3. +--------------------------+-----+-------------+--------+--------+-----------+------------+----------+
  4. |root.ln.wf02.wt02.hardware| null| root.ln| TEXT| PLAIN| SNAPPY|{"unit":"c"}| null|
  5. +--------------------------+-----+-------------+--------+--------+-----------+------------+----------+
  6. Total line number = 1
  7. It costs 0.005s
  8. +------------------------+-----+-------------+--------+--------+-----------+-----------------------+----------+
  9. | timeseries|alias|storage group|dataType|encoding|compression| tags|attributes|
  10. +------------------------+-----+-------------+--------+--------+-----------+-----------------------+----------+
  11. |root.ln.wf02.wt02.status| null| root.ln| BOOLEAN| PLAIN| SNAPPY|{"description":"test1"}| null|
  12. +------------------------+-----+-------------+--------+--------+-----------+-----------------------+----------+
  13. Total line number = 1
  14. It costs 0.004s

Notice that, we only support one condition in the where clause. Either it’s an equal filter or it is an contains filter. In both case, the property in the where condition must be a tag.

  • SHOW TIMESERIES LIMIT INT OFFSET INT

    returns all the timeseries information start from the offset and limit the number of series returned

  • SHOW LATEST TIMESERIES

    all the returned timeseries information should be sorted in descending order of the last timestamp of timeseries

It is worth noting that when the queried path does not exist, the system will return no timeseries.

Count Timeseries

IoTDB is able to use COUNT TIMESERIES <Path> to count the number of timeseries in the path. SQL statements are as follows:

  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

Besides, LEVEL could be defined to show count the number of timeseries of each node at the given level in current Metadata Tree. This could be used to query the number of sensors under each device. The grammar is: COUNT TIMESERIES <Path> GROUP BY LEVEL=<INTEGER>.

For example, if there are several timeseries (use show timeseries to show all timeseries):

  1. +-------------------------------+--------+-------------+--------+--------+-----------+-------------------------------------------+--------------------------------------------------------+
  2. | timeseries| alias|storage group|dataType|encoding|compression| tags| attributes|
  3. +-------------------------------+--------+-------------+--------+--------+-----------+-------------------------------------------+--------------------------------------------------------+
  4. |root.sgcc.wf03.wt01.temperature| null| root.sgcc| FLOAT| RLE| SNAPPY| null| null|
  5. | root.sgcc.wf03.wt01.status| null| root.sgcc| BOOLEAN| PLAIN| SNAPPY| 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"}|
  7. | root.ln.wf02.wt02.hardware| null| root.ln| TEXT| PLAIN| SNAPPY| {"unit":"c"}| null|
  8. | root.ln.wf02.wt02.status| null| root.ln| BOOLEAN| PLAIN| SNAPPY| {"description":"test1"}| null|
  9. | root.ln.wf01.wt01.temperature| null| root.ln| FLOAT| RLE| SNAPPY| null| null|
  10. | root.ln.wf01.wt01.status| null| root.ln| BOOLEAN| PLAIN| SNAPPY| null| null|
  11. +-------------------------------+--------+-------------+--------+--------+-----------+-------------------------------------------+--------------------------------------------------------+
  12. Total line number = 7
  13. It costs 0.004s

Then the Metadata Tree will be as below:

DDL (Data Definition Language) - 图1

As can be seen, root is considered as LEVEL=0. So when you enter statements such as:

  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

You will get following results:

  1. +------------+-----+
  2. | column|count|
  3. +------------+-----+
  4. | root.sgcc| 2|
  5. |root.turbine| 1|
  6. | root.ln| 4|
  7. +------------+-----+
  8. Total line number = 3
  9. It costs 0.002s
  10. +------------+-----+
  11. | column|count|
  12. +------------+-----+
  13. |root.ln.wf02| 2|
  14. |root.ln.wf01| 2|
  15. +------------+-----+
  16. Total line number = 2
  17. It costs 0.002s
  18. +------------+-----+
  19. | column|count|
  20. +------------+-----+
  21. |root.ln.wf01| 2|
  22. +------------+-----+
  23. Total line number = 1
  24. It costs 0.002s

Note: The path of timeseries is just a filter condition, which has no relationship with the definition of level.

Tag and attribute management

We can also add an alias, extra tag and attribute information while creating one timeseries. The SQL statements for creating timeseries with extra tag and attribute information are extended as follows:

  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)

The temprature in the brackets is an alias for the sensor s1. So we can use temprature to replace s1 anywhere.

IoTDB also supports using AS function to set alias. The difference between the two is: the alias set by the AS function is used to replace the whole time series name, temporary and not bound with the time series; while the alias mentioned above is only used as the alias of the sensor, which is bound with it and can be used equivalent to the original sensor name.

The only difference between tag and attribute is that we will maintain an inverted index on the tag, so we can use tag property in the show timeseries where clause which you can see in the following Show Timeseries section.

Notice that the size of the extra tag and attribute information shouldn’t exceed the tag_attribute_total_size.

We can update the tag information after creating it as following:

  • Rename the tag/attribute key
  1. ALTER timeseries root.turbine.d1.s1 RENAME tag1 TO newTag1
  • reset the tag/attribute value
  1. ALTER timeseries root.turbine.d1.s1 SET newTag1=newV1, attr1=newV1
  • delete the existing tag/attribute
  1. ALTER timeseries root.turbine.d1.s1 DROP tag1, tag2
  • add new tags
  1. ALTER timeseries root.turbine.d1.s1 ADD TAGS tag3=v3, tag4=v4
  • add new attributes
  1. ALTER timeseries root.turbine.d1.s1 ADD ATTRIBUTES attr3=v3, attr4=v4
  • upsert alias, tags and attributes

add alias or a new key-value if the alias or key doesn’t exist, otherwise, update the old one with new value.

  1. ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(tag3=v3, tag4=v4) ATTRIBUTES(attr3=v3, attr4=v4)

Node Management

Show Child Paths

  1. SHOW CHILD PATHS prefixPath

Return all child paths of the prefixPath, the prefixPath could contains *.

Example:

  • return the child paths of root.ln:show child paths root.ln
  1. +------------+
  2. | child paths|
  3. +------------+
  4. |root.ln.wf01|
  5. |root.ln.wf02|
  6. +------------+
  7. Total line number = 2
  8. It costs 0.002s

get all paths in form of root.xx.xx.xx:show child paths root.xx.xx

Show Child Nodes

  1. SHOW CHILD NODES prefixPath

Return all child nodes of the prefixPath.

Example:

  • return the child nodes of root:show child nodes root
  1. +------------+
  2. | child nodes|
  3. +------------+
  4. | ln|
  5. +------------+
  • return the child nodes of root.ln:show child nodes root.ln
  1. +------------+
  2. | child nodes|
  3. +------------+
  4. | wf01|
  5. | wf02|
  6. +------------+

Count Nodes

IoTDB is able to use COUNT NODES <PrefixPath> LEVEL=<INTEGER> to count the number of nodes at the given level in current Metadata Tree. This could be used to query the number of devices. The usage are as follows:

  1. IoTDB > COUNT NODES root LEVEL=2
  2. IoTDB > COUNT NODES root.ln LEVEL=2
  3. IoTDB > COUNT NODES root.ln.wf01 LEVEL=3

As for the above mentioned example and Metadata tree, you can get following results:

  1. +-----+
  2. |count|
  3. +-----+
  4. | 4|
  5. +-----+
  6. Total line number = 1
  7. It costs 0.003s
  8. +-----+
  9. |count|
  10. +-----+
  11. | 2|
  12. +-----+
  13. Total line number = 1
  14. It costs 0.002s
  15. +-----+
  16. |count|
  17. +-----+
  18. | 1|
  19. +-----+
  20. Total line number = 1
  21. It costs 0.002s

Note: The path of timeseries is just a filter condition, which has no relationship with the definition of level. PrefixPath could contains *, but all nodes after * would be ignored. Only the prefix path before * is valid.

Show Devices

  • SHOW DEVICES prefixPath? (WITH STORAGE GROUP)? limitClause? #showDevices

Similar to Show Timeseries, IoTDB also supports two ways of viewing devices:

  • SHOW DEVICES statement presents all devices’ information, which is equal to SHOW DEVICES root.
  • SHOW DEVICES <PrefixPath> statement specifies the PrefixPath and returns the devices information under the given level.

SQL statement is as follows:

  1. IoTDB> show devices
  2. IoTDB> show devices root.ln

You can get results below:

  1. +-------------------+
  2. | devices|
  3. +-------------------+
  4. | root.ln.wf01.wt01|
  5. | root.ln.wf02.wt02|
  6. |root.sgcc.wf03.wt01|
  7. | root.turbine.d1|
  8. +-------------------+
  9. Total line number = 4
  10. It costs 0.002s
  11. +-----------------+
  12. | devices|
  13. +-----------------+
  14. |root.ln.wf01.wt01|
  15. |root.ln.wf02.wt02|
  16. +-----------------+
  17. Total line number = 2
  18. It costs 0.001s

To view devices’ information with storage group, we can use SHOW DEVICES WITH STORAGE GROUP statement.

  • SHOW DEVICES WITH STORAGE GROUP statement presents all devices’ information with their storage group.
  • SHOW DEVICES <PrefixPath> WITH STORAGE GROUP statement specifies the PrefixPath and returns the devices’ information under the given level with their storage group information.

SQL statement is as follows:

  1. IoTDB> show devices with storage group
  2. IoTDB> show devices root.ln with storage group

You can get results below:

  1. +-------------------+-------------+
  2. | devices|storage group|
  3. +-------------------+-------------+
  4. | root.ln.wf01.wt01| root.ln|
  5. | root.ln.wf02.wt02| root.ln|
  6. |root.sgcc.wf03.wt01| root.sgcc|
  7. | root.turbine.d1| root.turbine|
  8. +-------------------+-------------+
  9. Total line number = 4
  10. It costs 0.003s
  11. +-----------------+-------------+
  12. | devices|storage group|
  13. +-----------------+-------------+
  14. |root.ln.wf01.wt01| root.ln|
  15. |root.ln.wf02.wt02| root.ln|
  16. +-----------------+-------------+
  17. Total line number = 2
  18. It costs 0.001s

TTL

IoTDB supports storage-level TTL settings, which means it is able to delete old data automatically and periodically. The benefit of using TTL is that hopefully you can control the total disk space usage and prevent the machine from running out of disks. Moreover, the query performance may downgrade as the total number of files goes up and the memory usage also increase as there are more files. Timely removing such files helps to keep at a high query performance level and reduce memory usage.

Set TTL

The SQL Statement for setting TTL is as follow:

  1. IoTDB> set ttl to root.ln 3600000

This example means that for data in root.ln, only that of the latest 1 hour will remain, the older one is removed or made invisible.

Unset TTL

To unset TTL, we can use follwing SQL statement:

  1. IoTDB> unset ttl to root.ln

After unset TTL, all data will be accepted in root.ln

Show TTL

To Show TTL, we can use following SQL statement:

  1. IoTDB> SHOW ALL TTL
  2. IoTDB> SHOW TTL ON StorageGroupNames

The SHOW ALL TTL example gives the TTL for all storage groups. The SHOW TTL ON root.group1 , root.group2 , root.group3 example shows the TTL for the three storage groups specified. Note: the TTL for storage groups that do not have a TTL set will display as null.