当前位置: 首页 > news >正文

IoTDB的基本概念及常用命令


IoTDB 是针对时间序列数据收集、存储与分析一体化的数据管理引擎。具有体量轻、性能高、易使用的特点,适用于工业物联网应用中海量时间序列数据高速写入和复杂分析查询的需求。
IoTDB 是以目录结构的时间序列组织管理方式来管理,适用于物联网场景:
1)支持复杂结构的智能网联设备的时间序列组织(多层树形结构,层级数量无限制);
2)支持大量同类物联网设备的时间序列组织;
3)可用模糊方式对海量复杂的时间序列目录结构进行检索。
IoTDB提供了统一的数据访问模式,无需进行分库分表处理,无需区分实时库和历史库。

IoTDB使用时根据企业组织结构和设备实体层次结构来构建数据模型,例如,以风电场物联网场景为例,构建数据模型如下:

截图上传 (https://iotdb.apache.org/zh/UserGuide/V1.2.x/Basic-Concept/Data-Model-and-Terminology.html)

电力集团层-风电场层-实体(设备)层-物理量(工况/字段)层。
其中 ROOT 为根节点,物理量层的每一个节点为叶子节点。IoTDB 采用树形结构定义数据模式,以从 ROOT 节点到叶子节点的路径来命名一个时间序列,层次间以“.”连接。例如,下图最左侧路径对应的时间序列名称为ROOT.ln.wf01.wt01.status。
wf: wind farm(风电场)。
wt: wind turbine(风机)。

IoTDB基本概念

基本概念的详细介绍,参考官方文档:
https://iotdb.apache.org/zh/UserGuide/V1.2.x/Basic-Concept/Data-Model-and-Terminology.html
https://www.bookstack.cn/read/iotdb-1.1-zh/59fac61aa3bd7e4d.md


IoTDB 数据库的基本概念

* 数据库(database)/ 存储组(storage group)
  低版本(例如:V0.3.x)上没有数据库(Database)的概念,只有存储组(Storage Group)的概念,高版本(V1.0.0及以上)增加了数据库(Database)的概念。
  数据库、存储组的管理命令有区别,详情参考官方手册。低版本只能用 storage group 命令管理、高版本可以用 storage group 命令和 database 命令管理。
  show databases 或 show storage group 查看数据库/存储组。

* 设备(device):
  物理设备(device)也称实体(entitiy)。在 IoTDB 当中,所有的物理量都有其对应的归属实体。实体无需手动创建,默认为倒数第二层。
  show devices 查看设备。

* 物理量(measurement):是指传感器或设备采集的数据点,也成测点(measurement)。另外还有多种叫法,也称指标(metrics)、信号(signals)、工况或字段(filed)。

* 时间序列(timeseries):也称时间线(timeline)、测量值。
  一个物理实体即设备的某个物理量在时间轴上的记录,是数据点的序列。一个实体的一个物理量对应一个时间序列,即:设备+物理量=时间序列。
  例如:
  device/entity = root.ln.wf01.wt01
  measurement = temperature
  timeseries = root.ln.wf01.wt01.temperature
  measurement = status
  timeseries = root.ln.wf01.wt01.status
  root.ln.wf01.wt01.temperature 和 root.ln.wf01.wt01.status 是两个时间序列。
  时间序列又分为普通时间序列(非对齐)和对齐时间序列(aligned timeseries)。
  一个时间序列是又多个 key-value 数据点,
  时间戳(timestamp):时间戳是一个数据到来的时间点。写入 IoTDB 数据时,时间戳相同的指标值会更新、时间戳不同的指标数据会插入,这是 IoTDB 数据库的特点。
  数据点(data point):一个”时间戳-值“对。 
  show timeseries 查看时间序列。

* 路径(path)

* 路径模式(path pattern)

IoTDB 数据库集群的基本概念

* 节点:
      IoTDB包括三类节点:ConfigNode(管理节点)、DataNode(数据节点)、AINode(分析节点)。
      ConfigNode:管理集群的节点信息、配置信息、用户权限、元数据、分区信息等,负责分布式操作的调度和负载均衡,所有 ConfigNode 之间互为全量备份。
      DataNode:服务客户端请求,负责数据的存储和计算。
      AINode:负责提供机器学习能力,支持注册已训练好的机器学习模型,并通过 SQL 调用模型进行推理,目前已内置自研时序大模型和常见的机器学习算法。
      
* Region
    在 IoTDB 中,元数据和数据都被分为小的分区,即 Region,由集群的各个 DataNode 进行管理。
    IoTDB数据分区包括:SchemaRegion(元数据分区)、DataRegion(数据分区)。
    SchemaRegion:元数据分区,管理一部分设备和测点的元数据。不同 DataNode 相同 RegionID 的 SchemaRegion 互为副本。例如:SchemaRegion-1 拥有三个副本,分别放置于 DataNode-1,DataNode-2 和 DataNode-3。
    DataRegion:数据分区,管理一部分设备的一段时间的数据。不同 DataNode 相同 RegionID 的 DataRegion 互为副本。例如:DataRegion-2 拥有两个副本,分别放置于 DataNode-1 和 DataNode-2。
    IoTDB的提供了基于Region的数据迁移高级功能。

* 多副本
    数据和元数据的副本数可配置,不同部署模式下的副本数推荐如下配置(其中多副本时可提供高可用服务):
    元数据:配置项 schema_replication_factor ,单机推荐配置为 1,集群推荐配置为 3。
    数据:  配置项 data_replication_factor,   单机推荐配置为 1,集群推荐配置为 2。

IoTDB的两种模型

IoTDB 提供了两种数据建模方式——树模型和表模型,其特点分别如下:

树模型

以测点为对象进行管理,每个测点对应一条时间序列,测点名按.分割可形成一个树形目录结构,与物理世界一一对应,对测点的读写操作简单直观。
树模型以测点为核心,每个测点对应一条时间序列,通过树形结构组织设备和测点的层级关系。这种模型直观且易于理解,特别适用于工业现场监控场景。
注意:在构建树模型路径时,节点命名若存在包含非标准字符或特殊符号的可能性,则建议对所有层级节点实施反引号封装策略。这样可以有效规避因字符解析异常导致的测点注册失败及数据写入中断问题,确保路径标识符在语法解析层面的准确性。

树模型特点:
层级结构:树模型采用层级结构,能够直观地映射物理设备的层级关系。
极简SQL:IoTDB为树模型设计了一套极简的SQL语法,便于自动化领域用户的数据采集和管理。

适用场景:
工业监控场景:如DCS、SCADA系统等,需要直观展示设备层级关系和实时监控数据。

优点:
直观易懂:树形结构类似于文件系统,易于用户理解和接受
高效采集:适用于大量异构设备和独立测点的数据采集和管理


表模型

IoTDB 从 2.0 版本开始支持表模型。使用表模型时,推荐为每类设备创建一张表,同类设备的物理量采集都具备一定共性(如都采集温度和湿度物理量),数据分析灵活丰富。

表模型特点:
设备为单位:表模型以设备为管理单位,每个时序表对应一类设备,字段统一。
标准SQL兼容:表模型的查询语言兼容标准SQL,支持丰富的查询和分析功能。
动态扩展:表结构可以在写入时自动创建和扩展,适应设备的动态变化。

适用场景:
大规模设备管理:需要高效管理大量设备及其相关数据。
复杂查询分析:需要进行多属性关联分析和复杂查询的场景。


优点:
分析能力强:由于兼容标准SQL,表模型在复杂查询和分析方面表现优异。
学习成本低:对于从关系型数据库迁移的用户,学习成本较低。


树模型与表模型差异对比
查询语言:树模型使用极简SQL,表模型兼容标准SQL。
动态扩展:表模型支持写入时自动建表和扩展列,树模型则不支持。
复杂查询:表模型在复杂查询和分析方面表现更优,树模型则相对简单。


IoTDB常用命令

IoTDB不同版本的SQL语法有差异,可查看官网找到自己对应的查看更多详细语法说明:
https://iotdb.apache.org/zh/UserGuide/V1.2.x/User-Manual/Query-Data.html
https://www.bookstack.cn/read/iotdb-1.1-zh/9257fc9c60b36e7d.md

# cli登录
./start-cli.sh -h 192.168.108.250 -u root
./start-cli.sh -h 192.168.108.250 -p 6667 -u root
./start-cli.sh -h 192.168.108.250 -p 6667 -u root -pw root


# cli命令帮助
# ./start-cli.sh -help
usage: ./start-cli.sh(start-cli.bat if Windows) [-c] [-disableISO8601] [-e <execute>]
       [-h <host>] [-help] [-maxPRC <maxPrintRowCount>] [-p <port>] [-pw <password>]
       [-timeout <queryTimeout>] -u <username>
 -c                           Rpc Compression enabled or not
 -disableISO8601              Display timestamp in number(optional)
 -e <execute>                 execute statement (optional)
 -h <host>                    Host Name (optional, default 127.0.0.1)
 -help                        Display help information(optional)
 -maxPRC <maxPrintRowCount>   Maximum number of rows displayed (optional)
 -p <port>                    Port (optional, default 6667)
 -pw <password>               password (optional)
 -timeout <queryTimeout>      The timeout in second. Using the configuration of server
                              if it's not set (optional)
 -u <username>                User name (required)
 

#查看cli命令帮助
IoTDB> help

#查看版本信息
IoTDB> show version;


# 查看集群状态
IoTDB> show cluster details;


#时区
IoTDB> show time_zone
IoTDB> set time_zone=xxx (例如:+08:00, Asia/Shanghai)

#时间显示方式
IoTDB> show time_display_type
IoTDB> set time_display_type=xxx (例如:long, default, ISO8601, yyyy-MM-dd HH:mm:ss)


#从服务器查询数据时的读取条数
IoTDB> set fetch_size=xxx
IoTDB> show fetch_size

#设置 CLI 一次展示的最大数据条数, 设置为-1表示无限制
IoTDB> set max_display_num=xxx


# 创建用户
DROP USER user01;
CREATE USER user01 '1q2w3e';


#查看数据库(database)/存储组(storage group)
IoTDB> show databases;
IoTDB> show databases root.*;
IoTDB> show databases root.**;

#低版本仅支持 storage group 管理方式、高版本(V1.0.0及以上) storage group 和 database 两种方式都支持
IoTDB> show storage group;
IoTDB> show storage group root.*;
IoTDB> show storage group root.**;


#创建数据库(Database)/存储组(storage group)
#IoTDB 的 database 是以 "root." 前缀开头的字符串、最长64,支持.分多级
#database 的父子节点都不能再设置成 database。例如:已经设置 root.ln 为 database了,就不能再设置 root.ln.wf01 为 database
#database 命令只支持中英文字符、数字、下划线、英文句号和反引号的组合,如果想设置为纯数字或者包含下划线和英文句号,需要用反引号(``)把 database 名称引起来。
#windows系统上database命名大小写不敏感

IoTDB > create database root.ln;
IoTDB > create database root.ln;
IoTDB > create database root.sg;
IoTDB > create database root.a.b.c;


#低版本仅支持 storage group 管理方式、高版本(V1.0.0及以上) storage group 和 database 两种方式都支持
IoTDB > set storage group root.ln;

IoTDB > create storage group root.ln;

注意:IoTDB支持无模式写入,可以在未定义元数据时(未创建database和timeseries时),通过 insert 语句直接写入数据,数据库中将自动识别并注册所需的元数据,实现自动建模。依赖配置参数 enable_auto_create_schema 默认开启(true)。


#删除数据库(Database)/存储组(storage group)
IoTDB > drop   database root.ln;
IoTDB > delete database root.ln;

#低版本仅支持 storage group 管理方式、高版本(V1.0.0及以上) storage group 和 database 两种方式都支持
IoTDB > drop   storage group root.ln;
IoTDB > delete storage group root.ln;

#查看时间序列(timeseries)
show timeseries;
show timeseries <Path>
show timeseries root.**;
show timeseries root.ln.**;
show timeseries root.ln.** limit 10;
show timeseries root.ln.** limit 10 offset 4;
show timeseries root.ln.** where Timeseries contains 'wf01.wt';  #低版本过滤无效?
show timeseries root.ln.** where dataType=FLOAT;  #低版本过滤无效?
show latest timeseries;


#创建时间序列(timeseries)
#对于 FLOAT 与 DOUBLE 类型的序列,如果编码方式采用 RLE 或 TS_2DIFF,可以在创建序列时通过 MAX_POINT_NUMBER 属性指定浮点数的小数点后位数
#不同数据类型支持的编目不同。压缩方式:UNCOMPRESSED、SNAPPY、LZ4、GZIP。
#不同IoTDB版本所支持的编码和压缩格式,可能有差异,详情参考官网介绍:
# V1.2.x:https://iotdb.apache.org/zh/UserGuide/V1.2.x/Basic-Concept/Encoding-and-Compression.html
# V1.1.x:https://www.bookstack.cn/read/iotdb-1.1-zh/8d706c8681f8ceae.md
#从 v0.13 开始支持简化版的创建时间序列SQL:
create timeseries root.ln.wf01.wt01.status(状态) BOOLEAN ENCODING=PLAIN;
-- create timeseries root.ln.wf01.wt01.status(状态) BOOLEAN ENCODING=PLAIN COMPRESSOR=SNAPPY;
-- create timeseries root.ln.wf01.wt01.status(状态) BOOLEAN ENCODING=PLAIN COMPRESSOR=UNCOMPRESSED;
create timeseries root.ln.wf01.wt01.temperature(温度) FLOAT ENCODING=RLE MAX_POINT_NUMBER=2;
create timeseries root.ln.wf01.wt01.temperature(温度) FLOAT ENCODING=RLE MAX_POINT_NUMBER=2;

create timeseries root.ln.wf01.wt02.status(状态) BOOLEAN ENCODING=PLAIN;
create timeseries root.ln.wf01.wt02.speed(风速) FLOAT ENCODING=RLE MAX_POINT_NUMBER=2;

create timeseries root.ln.wf01.wt03.status BOOLEAN ENCODING=PLAIN;
create timeseries root.ln.wf01.wt03.hardware TEXT ENCODING=PLAIN;


#低版本(version < v0.13仅支持完整版的创建时间序列SQL:
create timeseries root.ln.wf01.wt01.status(状态) WITH DATATYPE=BOOLEAN, ENCODING=PLAIN;
create timeseries root.ln.wf01.wt01.temperature(温度) WITH DATATYPE=FLOAT, ENCODING=RLE, MAX_POINT_NUMBER=2;

create timeseries root.ln.wf01.wt02.status(状态) WITH DATATYPE=BOOLEAN, ENCODING=PLAIN;
create timeseries root.ln.wf01.wt02.speed(风速) WITH DATATYPE=FLOAT, ENCODING=RLE, MAX_POINT_NUMBER=2;

create timeseries root.ln.wf01.wt03.status WITH DATATYPE=BOOLEAN, ENCODING=PLAIN;
create timeseries root.ln.wf01.wt03.hardware WITH DATATYPE=TEXT, ENCODING=PLAIN;


#自动创建时间序列(自动创建元数据)
#自动创建元数据:自动创建元数据指的是根据写入数据的特征自动创建出用户未定义的时间序列, 这既能解决海量序列场景下设备及测点难以提前预测与建模的难题,又能为用户提供开箱即用的写入体验。
#配置参数 enable_auto_create_schema  = true 决定是否允许自动创建时间序列,默认值是  true (允许自动创建)
#配置参数 default_storage_group_level = 1 指定 database 在时间序列所处的层级,默认为第 1 层(root为第 0 层)

#数据库和时间序列等都支持无模式写入(可以不提前创建元数据)、而是直接 insert 时自动注册所需的元数据,实现自动建模
insert into root.ln.wf20.wt20(timestamp, temperature) values(now(), 25.15);
insert into root.dd.wf20.wt20(timestamp, temperature) values(now(), 25.15);
insert into root.sg.wf31.wt31(timestamp, temperature) values(now(), 25.15);


show timeseries root.**.wt20.**;

IoTDB> show timeseries root.**.wt20.**;
+-----------------------------+-----+--------+--------+--------+-----------+----+----------+--------+------------------+
|                   Timeseries|Alias|Database|DataType|Encoding|Compression|Tags|Attributes|Deadband|DeadbandParameters|
+-----------------------------+-----+--------+--------+--------+-----------+----+----------+--------+------------------+
|root.ln.wf20.wt20.temperature| null| root.ln|   FLOAT| GORILLA|     SNAPPY|null|      null|    null|              null|
+-----------------------------+-----+--------+--------+--------+-----------+----+----------+--------+------------------+
Total line number = 1
It costs 0.008s
IoTDB> 

-- 查看创建结果
show timeseries;
show timeseries root.**.wf01.**;
show timeseries root.**.wt01.*;
show timeseries root.**.wt02.*;
show timeseries root.**.wt03.*;
show timeseries root.**.wt20.*;


#删除时间序列
delete timeseries root.ln.wf01.wt01.status;
delete timeseries root.ln.wf01.wt01.status, root.ln.wf01.wt01.temperature;
delete timeseries root.ln.wf01.wt01.status, root.ln.wf01.wt01.temperature, root.ln.wf01.wt02.status;

delete timeseries root.ln.wf01.*
delete timeseries root.ln.wf02.*

drop timeseries root.ln.wf01.*
drop timeseries root.ln.wf02.*

drop timeseries root.ln.wf01.**
drop timeseries root.ln.wf02.**

#创建对齐时间序列(Aligned Timeseries)
#root.ln.wf01.wt04.status
#root.ln.wf01.wt04.hardware
create aligned timeseries root.ln.wf01.wt04(status(状态) BOOLEAN ENCODING=PLAIN, hardware(硬件) TEXT ENCODING=PLAIN);

#如果要指定编码 / 压缩方式
create aligned timeseries root.ln.wf01.wt04(status(状态) BOOLEAN ENCODING=PLAIN COMPRESSOR=SNAPPY, hardware(硬件) TEXT ENCODING=PLAIN COMPRESSOR=SNAPPY);

在实际应用中,存在某些实体的多个物理量同时采样,形成一组时间列相同的时间序列,这样的一组时间序列在Apache IoTDB中可以建模为对齐时间序列。
在插入数据时,一组对齐序列的时间戳列在内存和磁盘中仅需存储一次,而不是每个时间序列存储一次。
对齐的一组时间序列最好同时创建。
不可以在对齐序列所属的实体下创建非对齐的序列,不可以在非对齐序列所属的实体下创建对齐序列。
查询数据时,可以对于每一条时间序列单独查询。
插入数据时,对齐的时间序列中某列的某些行允许有空值。

#创建带标签(tags)和属性(attributes)的时间序列(Timeseries)
我们可以在创建时间序列的时候,为它添加别名和额外的标签和属性信息。
标签和属性的区别在于:
标签可以用来查询时间序列路径,会在内存中维护标点到时间序列路径的倒排索引:标签 -> 时间序列路径
属性只能用时间序列路径来查询:时间序列路径 -> 属性
所用到的扩展的创建时间序列的 SQL 语句如下所示:

-- create timeseries root.ln.wf01.wt05.status(状态) BOOLEAN ENCODING=PLAIN tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2);
drop timeseries root.ln.wf01.wt05.*;
create timeseries root.ln.wf01.wt05.status(状态) BOOLEAN ENCODING=PLAIN tags(deviceCode=1002001_101) attributes(description=test_desc, location=hangzhou);
create timeseries root.ln.wf01.wt05.temperature(温度) FLOAT ENCODING=RLE MAX_POINT_NUMBER=2 tags(deviceCode=1002001_101) attributes(description=test_desc, location=hangzhou);

show timeseries root.ln.**.wt05.*;
show timeseries root.**.wt05.*;
show timeseries root.ln.wf01.wt05.*;


IoTDB> show timeseries root.ln.**.wt05.*;
+-----------------------------+-----+--------+--------+--------+-----------+------------------------------------------+-------------------------------------------------+--------+------------------+
|                   Timeseries|Alias|Database|DataType|Encoding|Compression|                                      Tags|                                       Attributes|Deadband|DeadbandParameters|
+-----------------------------+-----+--------+--------+--------+-----------+------------------------------------------+-------------------------------------------------+--------+------------------+
|root.ln.wf01.wt05.temperature| 温度| root.ln|   FLOAT|     RLE|     SNAPPY|{"deviceCode":"1002001_101"}|{"description":"test_desc","location":"hangzhou"}|    null|              null|
|     root.ln.wf01.wt05.status| 状态| root.ln| BOOLEAN|   PLAIN|     SNAPPY|{"deviceCode":"1002001_101"}|{"description":"test_desc","location":"hangzhou"}|    null|              null|
+-----------------------------+-----+--------+--------+--------+-----------+------------------------------------------+-------------------------------------------------+--------+------------------+
Total line number = 2
It costs 0.009s
IoTDB> 


# 标签(tags)&属性(attributes)的管理命令

# 更新标签或属性
ALTER timeseries xxx RENAME 'tag1' TO 'newTag1';
eg:
ALTER timeseries root.ln.wf01.wt05.status RENAME 'deviceCode' TO 'deviceId';
ALTER timeseries root.ln.wf01.wt05.temperature RENAME 'deviceCode' TO 'deviceId';

ALTER timeseries root.ln.wf01.wt05.status RENAME 'description' TO 'desc';
ALTER timeseries root.ln.wf01.wt05.temperature RENAME 'description' TO 'desc';

# 重新设置标签或属性的值
ALTER timeseries xxx SET 'newTag1'='newV1', 'attr1'='newV1'
eg:
ALTER timeseries root.ln.wf01.wt05.status SET 'deviceId'='1001', 'desc'='test';
ALTER timeseries root.ln.wf01.wt05.temperature SET 'deviceId'='1001', 'desc'='test';

# 删除标签或属性
ALTER timeseries xxx DROP 'tag1', 'tag2', 'attrbuite1'
eg:
ALTER timeseries root.ln.wf01.wt05.status DROP 'deviceId', 'location', 'desc';
ALTER timeseries root.ln.wf01.wt05.temperature DROP 'deviceId', 'location', 'desc';

# 添加新标签
ALTER timeseries xxx ADD TAGS 'tag3'='v3', 'tag4'='v4'
eg:
ALTER timeseries root.ln.wf01.wt05.status ADD TAGS 'deviceId'='1002001_101';
ALTER timeseries root.ln.wf01.wt05.temperature ADD TAGS 'deviceId'='1002001_101';

# 添加新属性
ALTER timeseries xxx ADD ATTRIBUTES 'attr3'='v3', 'attr4'='v4'
eg:
ALTER timeseries root.ln.wf01.wt05.status ADD ATTRIBUTES 'desc'='test', 'location'='hangzhou';
ALTER timeseries root.ln.wf01.wt05.temperature ADD ATTRIBUTES 'desc'='test', 'location'='hangzhou';

# 更新插入别名,标签和属性
ALTER timeseries xxx UPSERT ALIAS='newAlias' TAGS('tag2'='newV2', 'tag3'='v3') ATTRIBUTES('attr3'='v3', 'attr4'='v4')
eg:
ALTER timeseries root.ln.wf01.wt05.temperature UPSERT ALIAS='摄氏温度' TAGS('deviceId'='1002001_111') ATTRIBUTES('desc'='for test', 'location'='hz')

# 设置回原来的值
ALTER timeseries root.ln.wf01.wt05.status DROP 'deviceId', 'location', 'desc';
ALTER timeseries root.ln.wf01.wt05.temperature DROP 'deviceId', 'location', 'desc';
show timeseries root.ln.wf01.wt05.*;
ALTER timeseries root.ln.wf01.wt05.status UPSERT ALIAS='状态' TAGS('deviceId'='1002001_101') ATTRIBUTES('desc'='test desc', 'location'='hangzhou')
ALTER timeseries root.ln.wf01.wt05.temperature UPSERT ALIAS='温度' TAGS('deviceId'='1002001_101') ATTRIBUTES('desc'='test desc', 'location'='hangzhou')


# 使用标签作为过滤条件查询时间序列,使用 'tags_key' 来标识作为过滤条件的标签
SHOW TIMESERIES (<`PathPattern`>)? timeseriesWhereClause
show timeseries root.ln.wf01.wt05.*;
show timeseries root.ln.**.wt05.*;
show timeseries root.ln.**.wt05.* where 'deviceId'='1002001_101';
show timeseries root.ln.**.wt05.* where 'deviceId' contains '1002001_101';

# 创建元数据模板
# IoTDB 支持元数据模板功能,实现同类型不同实体(设备)的物理量元数据共享,减少元数据内存占用,同时简化同类型实体的管理。
CREATE SCHEMA? TEMPLATE <templateName> ALIGNED? '(' <measurementId> <attributeClauses> [',' <measurementId> <attributeClauses>]+ ')'
eg:
create schema template t1 (temperature FLOAT encoding=RLE, status BOOLEAN encoding=PLAIN compression=SNAPPY);
create schema template t2 aligned (hardware TEXT encoding=PLAIN compression=SNAPPY, status BOOLEAN encoding=PLAIN compression=SNAPPY);

# 查看元数据模板
show schema templates;

# 挂载元数据模板
# 为了更好地适配未来版本的更新及各模块的协作,我们强烈建议您将模板设置在存储组及存储组下层的节点中
# 注意:在插入数据之前,模板定义的时间序列不会被创建。可以使用如下 create timeseries of schema template on 语句在插入数据前创建时间序列
set schema template t1 to root.ln.wf01.wt61;
create timeseries of schema template on root.ln.wf01.wt61;

# 查看元数据模板下的物理量
show nodes in schema template t1;

# 查看挂载了某个元数据模板的路径前缀
show paths set schema template t1;

# 查看使用了某个元数据模板(即序列已创建)的路径前缀
show paths using schema template t1;

# 解除元数据模板,注意:需要先解除元数据模板的应用、否则会报错 Template is in use on xxx
deactivate schema template t1 from root.ln.wf01.wt61;
unset schema template t1 from root.ln.wf01.wt61;


# 删除元数据模板
# 注意:不能删除尚未卸载的模板
drop schema template t1;
drop schema template t2;

#统计时间序列数量
COUNT TIMESERIES root.**
COUNT TIMESERIES root.ln.**
COUNT TIMESERIES root.ln.*.*.status
COUNT TIMESERIES root.ln.wf01.wt01.status
COUNT TIMESERIES root.** WHERE 'tags_key' contains 'c' 
COUNT TIMESERIES root.** WHERE 'tags_key' = 'c' 
COUNT TIMESERIES root.** GROUP BY LEVEL=1
COUNT TIMESERIES root.ln.** GROUP BY LEVEL=1
COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2
COUNT TIMESERIES root.ln.** GROUP BY LEVEL=3

不同的IoTDB版本、语法上有差异:
COUNT TIMESERIES root.**
COUNT TIMESERIES root.ln.**
COUNT TIMESERIES root.ln.*.*.status
COUNT TIMESERIES root.ln.wf01.wt01.status
COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' 
COUNT TIMESERIES root.** WHERE DATATYPE = INT64
COUNT TIMESERIES root.** WHERE TAGS(unit) contains 'c' 
COUNT TIMESERIES root.** WHERE TAGS(unit) = 'c' 
COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' group by level = 1

IoTDB> 
IoTDB> COUNT TIMESERIES root.** GROUP BY LEVEL=1
+-------+-----------------+
| Column|count(timeseries)|
+-------+-----------------+
|root.ln|               17|
+-------+-----------------+
Total line number = 1
It costs 0.005s
IoTDB> COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2
+------------+-----------------+
|      Column|count(timeseries)|
+------------+-----------------+
|root.ln.wf20|                1|
|root.ln.wf02|                5|
|root.ln.wf01|               11|
+------------+-----------------+
Total line number = 3
It costs 0.006s
IoTDB> COUNT TIMESERIES root.ln.** GROUP BY LEVEL=3
+-----------------+-----------------+
|           Column|count(timeseries)|
+-----------------+-----------------+
|root.ln.wf02.wt03|                2|
|root.ln.wf02.wt02|                3|
|root.ln.wf01.wt03|                2|
|root.ln.wf01.wt04|                2|
|root.ln.wf01.wt01|                2|
|root.ln.wf01.wt02|                3|
|root.ln.wf01.wt05|                2|
|root.ln.wf20.wt20|                1|
+-----------------+-----------------+
Total line number = 8
It costs 0.007s
IoTDB> 

# delete 删除数据
delete from root.ln.wf01.wt01.status;
delete from root.ln.wf01.wt01.temperature;
delete from root.ln.wf01.wt01.status, root.ln.wf01.wt01.temperature;
delete from root.ln.wf01.wt01.*;
delete from root.ln.wf01.wt01.* where Time > now() - 1d;

delete from root.ln.wf01.wt01.* where time=2025-06-13T17:32:00.240
delete from root.ln.wf01.wt01.* where time=2025-06-15 12:30:20.123


# insert 插入数据
# root.ln.wf01.wt01.status
# root.ln.wf01.wt01.temperature
-- delete from root.ln.wf01.wt01.status, root.ln.wf01.wt01.temperature;
delete from root.ln.wf01.wt01.*;
insert into root.ln.wf01.wt01(timestamp, status) values(now(), true);
insert into root.ln.wf01.wt01(timestamp, temperature) values(now(), 25.15);
insert into root.ln.wf01.wt01(timestamp, status, temperature) values(now() + 1s, false, null);
insert into root.ln.wf01.wt01(timestamp, status, temperature) values(now() + 2s, true, 26.60);
insert into root.ln.wf01.wt01(timestamp, status, temperature) values(now() + 1d, true, 28.80);
insert into root.ln.wf01.wt01(timestamp, status, temperature) values(now() + 1d, true, 28.80);


关于时间戳传值的方式:
例如:2025-06-15 12:30:20.123 对应的时间戳值为 1749961820123(精确到毫秒),插入时,下面几种时间传值方式都可以:
insert into root.ln.wf01.wt01(timestamp, status, temperature) values(1749961820123, true, 29.99);
insert into root.ln.wf01.wt01(timestamp, status, temperature) values(2025-06-15 12:30:20.123, true, 29.99);
insert into root.ln.wf01.wt01(timestamp, status, temperature) values(2025-06-15T12:30:20.123, true, 29.99);
insert into root.ln.wf01.wt01(timestamp, status, temperature) values(2025-06-15T12:30:20.123+08:00, true, 29.99);
后面几种方式插入时,数据库内部会自动隐式转换成 timestamp long 数值进行插入,数值和日期插入两者是等效的。查询也同样支持两种方式。

但注意:timestamp 值不能传入以字符串(单引号包含)的方式传值,例如:
insert into root.ln.wf01.wt01(timestamp, status, temperature) values('2025-06-15 12:30:20.123', true, 29.99); 这种不支持、内部无法自动转换。

插入时时间戳支持变量获取当前系统时间:
insert into root.ln.wf01.wt01(timestamp, status, temperature) values(now() + 1d, true, 28.80);

select * from root.ln.wf01.wt01;


#插入数据时 timestamp 列可以省略、此时系统将使用当前的系统时间作为该数据点的时间戳
insert into root.ln.wf01.wt01(status, temperature) values(true, 28.58);

#多行数据(多values)插入
insert into root.ln.wf01.wt01(timestamp, status, temperature) values(now() + 3s, true, 29.70),(now() + 4s, true, 30.80);

注意:多行插入时、不支持省略 timestamp 列、必须指定 timestamp 列,不支持下面这种插入:
insert into root.ln.wf01.wt01(status, temperature) values(true, 29.70),(true, 30.80);
会报错:Msg: 701: need timestamps when insert multi rows


# root.ln.wf01.wt02.status
# root.ln.wf01.wt02.speed
-- delete from root.ln.wf01.wt02.status, root.ln.wf01.wt02.speed
delete from root.ln.wf01.wt02.*;
insert into root.ln.wf01.wt02(timestamp, status) values(now(), true);
insert into root.ln.wf01.wt02(timestamp, speed) values(now(), 3.50);
insert into root.ln.wf01.wt02(timestamp, status, temperature) values(now() + 1s, false, null);
insert into root.ln.wf01.wt02(timestamp, status, temperature) values(now() + 2s, true, 5.45);
insert into root.ln.wf01.wt02(timestamp, status, temperature) values(now() + 3s, true, 5.45),(now() + 4s, true, 5.55);
insert into root.ln.wf01.wt02(timestamp, status, temperature) values(now() + 1d, true, 6.66);

select * from root.ln.wf01.wt02;

# root.ln.wf02.wt02.status
# root.ln.wf02.wt02.speed
-- delete from root.ln.wf02.wt02.status, root.ln.wf02.wt02.speed;
delete from root.ln.wf02.wt02.*;
insert into root.ln.wf02.wt02(timestamp, status) values(now(), true);
insert into root.ln.wf02.wt02(timestamp, speed) values(now(), 5.50);
insert into root.ln.wf02.wt02(timestamp, status, temperature) values(now() + 1s, false, null);
insert into root.ln.wf02.wt02(timestamp, status, temperature) values(now() + 2s, true, 6.44);
-- insert into root.ln.wf02.wt02(timestamp, status, temperature) values(now() + 3s, true, 6.44),(now() + 4s, true, 6.55);
select * from root.ln.wf02.wt02;

# root.ln.wf02.wt03.status
# root.ln.wf02.wt03.hardware
-- delete from root.ln.wf02.wt03.status, root.ln.wf02.wt03.hardware;
delete from root.ln.wf02.wt03.*;
insert into root.ln.wf02.wt03(timestamp, status) values(now(), true);
insert into root.ln.wf02.wt03(timestamp, hardware) values(now(), 'v1');
insert into root.ln.wf02.wt03(timestamp, status, hardware) values(now() + 1s, false, null);
insert into root.ln.wf02.wt03(timestamp, status, hardware) values(now() + 2s, true, 'v2');
insert into root.ln.wf02.wt03(timestamp, status, hardware) values(now() + 3s, true, 'v3'),(now() + 4s, true, 'v4');
select * from root.ln.wf02.wt03;


# 对齐时间序列(Aligned Timeseries)数据插入,values 需要指定关键词 aligned 
# root.ln.wf01.wt04.status
# root.ln.wf01.wt04.hardware
delete from root.ln.wf01.wt04.*;
insert into root.ln.wf01.wt04(timestamp, status) aligned values(now(), true); 
insert into root.ln.wf01.wt04(timestamp, hardware) aligned values(now() + 1s, 'v1'); 
insert into root.ln.wf01.wt04(timestamp, status, hardware) aligned values(now() + 2s, true, 'v2'),(now() + 3s, true, 'v3');
insert into root.ln.wf01.wt04(timestamp, status, hardware) aligned values(now() + 4s, true, null);

select * from root.ln.wf01.wt04;
select status, hardware from root.ln.wf01.wt04;
select status from root.ln.wf01.wt04;
select hardware from root.ln.wf01.wt04;

#查询设备属性可看到对齐时间序列属性:
show devices;
show devices root.ln.wf01.wt04;

IoTDB> show devices root.ln.wf01.wt04;
+-----------------+---------+
|           Device|IsAligned|
+-----------------+---------+
|root.ln.wf01.wt04|     true|
+-----------------+---------+

# 带 tags 和 attributes 属性的数据插入和查询
# root.ln.wf01.wt05.status
# root.ln.wf01.wt05.temperature
delete from root.ln.wf01.wt05.*;
insert into root.ln.wf01.wt05(timestamp, status) values(now(), true);
insert into root.ln.wf01.wt05(timestamp, temperature) values(now(), 25.15);
insert into root.ln.wf01.wt05(timestamp, status, temperature) values(now() + 1s, false, null);
insert into root.ln.wf01.wt05(timestamp, status, temperature) values(now() + 2s, true, 26.60);
insert into root.ln.wf01.wt05(timestamp, status, temperature) values(now() + 3s, true, 26.70),(now() + 4s, true, 26.80);
select * from root.ln.wf01.wt05;


select status, temperature from root.ln.wf01.wt05;
-- as 重命名字段、否则查询结果默认 filed 显示的是 path
select status as status, temperature as temperature from root.ln.wf01.wt05;


IoTDB> select * from root.ln.wf01.wt05;
+-----------------------------+-----------------------------+------------------------+
|                         Time|root.ln.wf01.wt05.temperature|root.ln.wf01.wt05.status|
+-----------------------------+-----------------------------+------------------------+
|2025-06-12T09:41:44.911+08:00|                         null|                    true|
|2025-06-12T09:41:49.143+08:00|                        25.15|                    null|
|2025-06-12T09:41:52.662+08:00|                         null|                   false|
|2025-06-12T09:41:56.063+08:00|                         26.6|                    true|
|2025-06-12T09:41:59.574+08:00|                         26.7|                    true|
|2025-06-12T09:42:00.574+08:00|                         26.8|                    true|
+-----------------------------+-----------------------------+------------------------+
Total line number = 6
It costs 0.017s
IoTDB> select status, temperature from root.ln.wf01.wt05;
+-----------------------------+------------------------+-----------------------------+
|                         Time|root.ln.wf01.wt05.status|root.ln.wf01.wt05.temperature|
+-----------------------------+------------------------+-----------------------------+
|2025-06-12T09:41:44.911+08:00|                    true|                         null|
|2025-06-12T09:41:49.143+08:00|                    null|                        25.15|
|2025-06-12T09:41:52.662+08:00|                   false|                         null|
|2025-06-12T09:41:56.063+08:00|                    true|                         26.6|
|2025-06-12T09:41:59.574+08:00|                    true|                         26.7|
|2025-06-12T09:42:00.574+08:00|                    true|                         26.8|
+-----------------------------+------------------------+-----------------------------+
Total line number = 6
It costs 0.012s
IoTDB> 
IoTDB> select status as status, temperature as temperature from root.ln.wf01.wt05;
+-----------------------------+------+-----------+
|                         Time|status|temperature|
+-----------------------------+------+-----------+
|2025-06-12T09:41:44.911+08:00|  true|       null|
|2025-06-12T09:41:49.143+08:00|  null|      25.15|
|2025-06-12T09:41:52.662+08:00| false|       null|
|2025-06-12T09:41:56.063+08:00|  true|       26.6|
|2025-06-12T09:41:59.574+08:00|  true|       26.7|
|2025-06-12T09:42:00.574+08:00|  true|       26.8|
+-----------------------------+------+-----------+
Total line number = 6
It costs 0.014s
IoTDB> 


# select 查询
不同的版本、查询语法有差别,可再官网找到自己对应的版本查看详细语法
https://iotdb.apache.org/zh/UserGuide/V1.2.x/User-Manual/Query-Data.html
https://www.bookstack.cn/read/iotdb-1.1-zh/9257fc9c60b36e7d.md


V1.1.x 语法:
SELECT [LAST] selectExpr [, selectExpr] ...
    [INTO intoItem [, intoItem] ...]
    FROM prefixPath [, prefixPath] ...
    [WHERE whereCondition]
    [GROUP BY {
        ([startTime, endTime), interval [, slidingStep]) |
        LEVEL = levelNum [, levelNum] ... |
        TAGS(tagKey [, tagKey] ... |
        VARIATION(expression[,delta][,ignoreNull=true/false])|
        CONDITION(expression,[keep>/>=/=/</<=]threshold[,ignoreNull=true/false])|
        SESSION(timeInterval)
    }]
    [HAVING havingCondition]
    [ORDER BY sortKey {ASC | DESC}]
    [FILL ({PREVIOUS | LINEAR | constant})]
    [SLIMIT seriesLimit] [SOFFSET seriesOffset]
    [LIMIT rowLimit] [OFFSET rowOffset]
    [ALIGN BY {TIME | DEVICE}]


whereCondition WHERE条件过滤的更多语法参考:https://www.bookstack.cn/read/iotdb-1.1-zh/dd6eb76363f45ed3.md


#查询所有数据
select ** from root;
select * from root.**;

select ** from root.ln;
select * from root.ln.**;

select ** from root.sg;
select * from root.sg.**;

#查询指定的数据
select * from root.ln.wf01.wt01;
select status, temperature from root.ln.wf01.wt01;
select status as status, temperature as temperature from root.ln.wf01.wt01;

select * from root.ln.wf01.wt01 where Time > 2025-06-13T17:31:58;
select status, temperature from root.ln.wf01.wt01 where Time > 2025-06-13T17:31:58;
select status, temperature from root.ln.wf01.wt01 where Time > 2025-06-13T17:31:58 and status = true;
select status, temperature from root.ln.wf01.wt01 where Time > 2025-06-13T17:31:58 and status is not null;
select status, temperature from root.ln.wf01.wt01 where Time > 2025-06-13T17:31:58 and temperature is not null;
select status, temperature from root.ln.wf01.wt01 where Time > 2025-06-13T17:31:58 and temperature is null;


select status as status, temperature as temperature from root.ln.wf01.wt01 where status = true;
select status as status, temperature as temperature from root.ln.wf01.wt01 where status in (true, false);
select status as status, temperature as temperature from root.ln.wf01.wt01 where status is not null;
select status as status, temperature as temperature from root.ln.wf01.wt01 where status is null;
select status as status, temperature as temperature from root.ln.wf01.wt01 where temperature > 26.70;
select status as status, temperature as temperature from root.ln.wf01.wt01 where temperature = 26.70;
select status as status, temperature as temperature from root.ln.wf01.wt01 where temperature between 26.00 and 26.70;
select status as status, temperature as temperature from root.ln.wf01.wt01 where temperature in (26.60, 26.70);


select * from root.ln.wf02.wt03;

-- filed字段过滤查询
select hardware from root.ln.wf02.wt03 where hardware = 'v1';
select hardware from root.ln.wf02.wt03 where hardware like '%v%';
select hardware from root.ln.wf02.wt03 where hardware in ('v1', 'v2');

-- filed字段 regexp 正则匹配过滤
select hardware from root.ln.wf02.wt03 where hardware regexp '^v[0-9]+$';

# 时间过滤查询
select temperature from root.ln.wf01.wt01 where time < 2025-06-11T18:40:07.653
select hardware from root.ln.wf02.wt03 where hardware regexp '^v[0-9]+$' and time >= 2025-06-12T11:04:01.945;

# 根据一个时间区间选择多列数据查询
select status, temperature from root.ln.wf01.wt01 where time >= 2025-06-11T18:39:54.996 and time <= 2025-06-11T18:40:07.653;
select status, temperature from root.ln.wf01.wt01 where time between 2025-06-11T18:39:54.996 and 2025-06-11T18:40:07.653;


# 查询最大值/最小值
select MAX_VALUE(temperature) from root.ln.wf01.wt01;

select MIN_VALUE(temperature) from root.ln.wf01.wt01;


# 查询所有序列下温度传感器 temperature 的最大值
select MAX_VALUE(temperature) from root.** group by level = 0;

select MAX_VALUE(temperature) from root.** group by level = 1;

IoTDB> select MAX_VALUE(temperature) from root.** group by level = 0;
+---------------------------------+
|MAX_VALUE(root.*.*.*.temperature)|
+---------------------------------+
|                            29.99|
+---------------------------------+
Total line number = 1
It costs 0.013s
IoTDB> select MAX_VALUE(temperature) from root.** group by level = 1; 
+----------------------------------+----------------------------------+
|MAX_VALUE(root.ln.*.*.temperature)|MAX_VALUE(root.sg.*.*.temperature)|
+----------------------------------+----------------------------------+
|                             29.99|                             25.15|
+----------------------------------+----------------------------------+
Total line number = 1
It costs 0.012s


# 查询所有序列下温度传感器 temperature 的所有值
select temperature from root.**;

# 查询所有序列下温度传感器 temperature 的每个序列的最大值
select MAX_VALUE(temperature) from root.**;

# 统计不同 database 下 temperature 序列的数据点个数
select count(temperature) from root.** group by level = 0;

select count(temperature) from root.** group by level = 1;
select count(temperature) from root.** group by level = 2;

IoTDB> select count(temperature) from root.** group by level = 0;
+-----------------------------+
|count(root.*.*.*.temperature)|
+-----------------------------+
|                           14|
+-----------------------------+
Total line number = 1
It costs 0.015s
IoTDB> select count(temperature) from root.** group by level = 1;
+------------------------------+------------------------------+
|count(root.ln.*.*.temperature)|count(root.sg.*.*.temperature)|
+------------------------------+------------------------------+
|                            13|                             1|
+------------------------------+------------------------------+
Total line number = 1
It costs 0.012s
IoTDB> 

IoTDB> 
IoTDB> select count(temperature) from root.** group by level = 2; 
+--------------------------------+--------------------------------+--------------------------------+--------------------------------+
|count(root.*.wf02.*.temperature)|count(root.*.wf01.*.temperature)|count(root.*.wf20.*.temperature)|count(root.*.wf31.*.temperature)|
+--------------------------------+--------------------------------+--------------------------------+--------------------------------+
|                               3|                              10|                               0|                               1|
+--------------------------------+--------------------------------+--------------------------------+--------------------------------+
Total line number = 1
It costs 0.013s
IoTDB> 

# 时间对齐模式下的排序
select * from root.ln.** where time <= 2025-06-18 13:01:01.123 order by time desc;  

IoTDB> select * from root.ln.** where time <= 2025-06-18 13:01:01.123 order by time desc;                   
+-----------------------------+--------------------------+------------------------+-----------------------------+-----------------------+------------------------+-----------------------------+------------------------+--------------------------+------------------------+--------------------------+------------------------+-----------------------------+-----------------------+------------------------+-----------------------------+------------------------+-----------------------------+
|                         Time|root.ln.wf02.wt03.hardware|root.ln.wf02.wt03.status|root.ln.wf02.wt02.temperature|root.ln.wf02.wt02.speed|root.ln.wf02.wt02.status|root.ln.wf01.wt05.temperature|root.ln.wf01.wt05.status|root.ln.wf01.wt04.hardware|root.ln.wf01.wt04.status|root.ln.wf01.wt03.hardware|root.ln.wf01.wt03.status|root.ln.wf01.wt02.temperature|root.ln.wf01.wt02.speed|root.ln.wf01.wt02.status|root.ln.wf01.wt01.temperature|root.ln.wf01.wt01.status|root.ln.wf20.wt20.temperature|
+-----------------------------+--------------------------+------------------------+-----------------------------+-----------------------+------------------------+-----------------------------+------------------------+--------------------------+------------------------+--------------------------+------------------------+-----------------------------+-----------------------+------------------------+-----------------------------+------------------------+-----------------------------+
|2025-06-15T12:30:20.123+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                        29.99|                    true|                         null|
|2025-06-13T17:33:31.088+08:00|                      null|                    null|                         null|                   null|                    null|                         26.8|                    true|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:30.088+08:00|                      null|                    null|                         null|                   null|                    null|                         26.7|                    true|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:28.913+08:00|                      null|                    null|                         null|                   null|                    null|                         26.6|                    true|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:27.898+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                   false|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:26.868+08:00|                      null|                    null|                         null|                   null|                    null|                        25.15|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:26.845+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    true|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:18.520+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                        v3|                    true|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:17.520+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                        v2|                    true|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:16.258+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                        v1|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:15.246+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                      null|                    true|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:12.839+08:00|                        v4|                    true|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:11.839+08:00|                        v3|                    true|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:10.626+08:00|                        v2|                    true|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:09.603+08:00|                      null|                   false|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:08.588+08:00|                        v1|                    null|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:08.579+08:00|                      null|                    true|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:04.865+08:00|                      null|                    null|                         6.55|                   null|                    true|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:03.865+08:00|                      null|                    null|                         6.44|                   null|                    true|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:02.658+08:00|                      null|                    null|                         6.44|                   null|                    true|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:01.641+08:00|                      null|                    null|                         null|                   null|                   false|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:00.622+08:00|                      null|                    null|                         null|                    5.5|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:33:00.603+08:00|                      null|                    null|                         null|                   null|                    true|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                         null|
|2025-06-13T17:32:51.639+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         5.55|                   null|                    true|                         null|                    null|                         null|
|2025-06-13T17:32:50.639+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         5.45|                   null|                    true|                         null|                    null|                         null|
|2025-06-13T17:32:49.422+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         5.45|                   null|                    true|                         null|                    null|                         null|
|2025-06-13T17:32:48.414+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                   false|                         null|                    null|                         null|
|2025-06-13T17:32:47.405+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                    3.5|                    null|                         null|                    null|                         null|
|2025-06-13T17:32:47.380+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    true|                         null|                    null|                         null|
|2025-06-13T17:32:00.240+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         26.6|                    true|                         null|
|2025-06-13T17:31:58.953+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                   false|                         null|
|2025-06-13T17:31:57.927+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                        25.15|                    null|                         null|
|2025-06-13T17:31:57.914+08:00|                      null|                    null|                         null|                   null|                    null|                         null|                    null|                      null|                    null|                      null|                    null|                         null|                   null|                    null|                         null|                    true|                         null|
+-----------------------------+--------------------------+------------------------+-----------------------------+-----------------------+------------------------+-----------------------------+------------------------+--------------------------+------------------------+--------------------------+------------------------+-----------------------------+-----------------------+------------------------+-----------------------------+------------------------+-----------------------------+
Total line number = 33
It costs 0.038s
IoTDB> 

# 设备对齐模式下的排序
select * from root.ln.** where time <= 2025-06-18 13:01:01.123 order by device desc, time asc align by device;

IoTDB> select * from root.ln.** where time <= 2025-06-18 13:01:01.123 order by device desc, time asc align by device;
+-----------------------------+-----------------+--------+------+-----------+-----+
|                         Time|           Device|hardware|status|temperature|speed|
+-----------------------------+-----------------+--------+------+-----------+-----+
|2025-06-13T17:33:08.579+08:00|root.ln.wf02.wt03|    null|  true|       null| null|
|2025-06-13T17:33:08.588+08:00|root.ln.wf02.wt03|      v1|  null|       null| null|
|2025-06-13T17:33:09.603+08:00|root.ln.wf02.wt03|    null| false|       null| null|
|2025-06-13T17:33:10.626+08:00|root.ln.wf02.wt03|      v2|  true|       null| null|
|2025-06-13T17:33:11.839+08:00|root.ln.wf02.wt03|      v3|  true|       null| null|
|2025-06-13T17:33:12.839+08:00|root.ln.wf02.wt03|      v4|  true|       null| null|
|2025-06-13T17:33:00.603+08:00|root.ln.wf02.wt02|    null|  true|       null| null|
|2025-06-13T17:33:00.622+08:00|root.ln.wf02.wt02|    null|  null|       null|  5.5|
|2025-06-13T17:33:01.641+08:00|root.ln.wf02.wt02|    null| false|       null| null|
|2025-06-13T17:33:02.658+08:00|root.ln.wf02.wt02|    null|  true|       6.44| null|
|2025-06-13T17:33:03.865+08:00|root.ln.wf02.wt02|    null|  true|       6.44| null|
|2025-06-13T17:33:04.865+08:00|root.ln.wf02.wt02|    null|  true|       6.55| null|
|2025-06-13T17:33:26.845+08:00|root.ln.wf01.wt05|    null|  true|       null| null|
|2025-06-13T17:33:26.868+08:00|root.ln.wf01.wt05|    null|  null|      25.15| null|
|2025-06-13T17:33:27.898+08:00|root.ln.wf01.wt05|    null| false|       null| null|
|2025-06-13T17:33:28.913+08:00|root.ln.wf01.wt05|    null|  true|       26.6| null|
|2025-06-13T17:33:30.088+08:00|root.ln.wf01.wt05|    null|  true|       26.7| null|
|2025-06-13T17:33:31.088+08:00|root.ln.wf01.wt05|    null|  true|       26.8| null|
|2025-06-13T17:33:15.246+08:00|root.ln.wf01.wt04|    null|  true|       null| null|
|2025-06-13T17:33:16.258+08:00|root.ln.wf01.wt04|      v1|  null|       null| null|
|2025-06-13T17:33:17.520+08:00|root.ln.wf01.wt04|      v2|  true|       null| null|
|2025-06-13T17:33:18.520+08:00|root.ln.wf01.wt04|      v3|  true|       null| null|
|2025-06-13T17:32:47.380+08:00|root.ln.wf01.wt02|    null|  true|       null| null|
|2025-06-13T17:32:47.405+08:00|root.ln.wf01.wt02|    null|  null|       null|  3.5|
|2025-06-13T17:32:48.414+08:00|root.ln.wf01.wt02|    null| false|       null| null|
|2025-06-13T17:32:49.422+08:00|root.ln.wf01.wt02|    null|  true|       5.45| null|
|2025-06-13T17:32:50.639+08:00|root.ln.wf01.wt02|    null|  true|       5.45| null|
|2025-06-13T17:32:51.639+08:00|root.ln.wf01.wt02|    null|  true|       5.55| null|
|2025-06-13T17:31:57.914+08:00|root.ln.wf01.wt01|    null|  true|       null| null|
|2025-06-13T17:31:57.927+08:00|root.ln.wf01.wt01|    null|  null|      25.15| null|
|2025-06-13T17:31:58.953+08:00|root.ln.wf01.wt01|    null| false|       null| null|
|2025-06-13T17:32:00.240+08:00|root.ln.wf01.wt01|    null|  true|       26.6| null|
|2025-06-15T12:30:20.123+08:00|root.ln.wf01.wt01|    null|  true|      29.99| null|
+-----------------------------+-----------------+--------+------+-----------+-----+
Total line number = 33
It costs 0.024s
IoTDB> 

#查看设备(可以理解为数据库的表的概念)
所谓设备(路径信息的末数第二层),可以对应到所要采集的设备信息上,但是建议取名的时候按照你的业务规则来,尽量一眼能看出来

show devices;
#查看设备及其database信息
show devices with database;
show devices root.ln.**;
show devices root.ln.wf01.**;

IoTDB> show devices;
+-----------------+---------+
|           Device|IsAligned|
+-----------------+---------+
|root.ln.wf02.wt03|    false|
|root.ln.wf02.wt02|    false|
|root.ln.wf01.wt08|    false|
|root.ln.wf01.wt07|    false|
|root.ln.wf01.wt05|    false|
|root.ln.wf01.wt04|     true|
|root.ln.wf01.wt03|    false|
|root.ln.wf01.wt02|    false|
|root.ln.wf01.wt01|    false|
|root.ln.wf20.wt20|    false|
|root.sg.wf31.wt31|    false|
+-----------------+---------+
Total line number = 11
It costs 0.008s
IoTDB> show devices with database;
+-----------------+--------+---------+
|           Device|Database|IsAligned|
+-----------------+--------+---------+
|root.ln.wf02.wt03| root.ln|    false|
|root.ln.wf02.wt02| root.ln|    false|
|root.ln.wf01.wt08| root.ln|    false|
|root.ln.wf01.wt07| root.ln|    false|
|root.ln.wf01.wt05| root.ln|    false|
|root.ln.wf01.wt04| root.ln|     true|
|root.ln.wf01.wt03| root.ln|    false|
|root.ln.wf01.wt02| root.ln|    false|
|root.ln.wf01.wt01| root.ln|    false|
|root.ln.wf20.wt20| root.ln|    false|
|root.sg.wf31.wt31| root.sg|    false|
+-----------------+--------+---------+
Total line number = 11
It costs 0.009s
IoTDB> show devices root.ln.**;   
+-----------------+---------+
|           Device|IsAligned|
+-----------------+---------+
|root.ln.wf02.wt03|    false|
|root.ln.wf02.wt02|    false|
|root.ln.wf01.wt08|    false|
|root.ln.wf01.wt07|    false|
|root.ln.wf01.wt05|    false|
|root.ln.wf01.wt04|     true|
|root.ln.wf01.wt03|    false|
|root.ln.wf01.wt02|    false|
|root.ln.wf01.wt01|    false|
|root.ln.wf20.wt20|    false|
+-----------------+---------+
Total line number = 10
It costs 0.024s
IoTDB> show devices root.ln.wf01.**;
+-----------------+---------+
|           Device|IsAligned|
+-----------------+---------+
|root.ln.wf01.wt08|    false|
|root.ln.wf01.wt07|    false|
|root.ln.wf01.wt05|    false|
|root.ln.wf01.wt04|     true|
|root.ln.wf01.wt03|    false|
|root.ln.wf01.wt02|    false|
|root.ln.wf01.wt01|    false|
+-----------------+---------+
Total line number = 7
It costs 0.004s
IoTDB> 


#统计节点数
count nodes root.** LEVEL=0;
count nodes root.** LEVEL=1;
count nodes root.** LEVEL=2;

count nodes root.**.temperature LEVEL=3;

IoTDB> show child nodes root;
+----------+
|ChildNodes|
+----------+
|        aa|
|        ln|
|        sg|
+----------+
Total line number = 3
It costs 0.009s
IoTDB> 

IoTDB> show child nodes root.ln;
+----------+
|ChildNodes|
+----------+
|      wf01|
|      wf02|
|      wf20|
+----------+
Total line number = 3
It costs 0.004s
IoTDB> 

IoTDB> count nodes root.** LEVEL=0;
+------------+
|count(nodes)|
+------------+
|           1|
+------------+
Total line number = 1
It costs 0.011s
IoTDB> count nodes root.** LEVEL=1;
+------------+
|count(nodes)|
+------------+
|           3|
+------------+
Total line number = 1
It costs 0.010s
IoTDB> count nodes root.** LEVEL=2;
+------------+
|count(nodes)|
+------------+
|           4|
+------------+
Total line number = 1
It costs 0.008s
IoTDB> 

#统计数据库的数量(个数)
count databases;
count databases root.*;
count databases root.ln;


IoTDB> show databases;
+--------+----+-----------------------+---------------------+---------------------+
|Database| TTL|SchemaReplicationFactor|DataReplicationFactor|TimePartitionInterval|
+--------+----+-----------------------+---------------------+---------------------+
| root.sg|null|                      1|                    1|            604800000|
| root.ln|null|                      1|                    1|            604800000|
+--------+----+-----------------------+---------------------+---------------------+
Total line number = 2
It costs 0.003s
IoTDB> count databases;
+-----+
|count|
+-----+
|    3|
+-----+
Total line number = 1
It costs 0.002s
IoTDB> count databases root.*;
+-----+
|count|
+-----+
|    3|
+-----+
Total line number = 1
It costs 0.001s
IoTDB> count databases root.ln;
+-----+
|count|
+-----+
|    1|
+-----+
Total line number = 1
It costs 0.002s
IoTDB> 

#统计时间序列的数量(个数)
count timeseries;
count timeseries root.**
count timeseries root.ln.**
count timeseries root.ln.*.*.status
count timeseries root.ln.wf01.wt01.status


#时间序列路径管理

#查看路径的所有子路径
语法:SHOW CHILD PATHS pathPattern

show child paths root.ln;
show child paths root.ln.*;
show child paths root.ln.**;


IoTDB> show child paths;
+-----------------------------+----------+
|                   ChildPaths| NodeTypes|
+-----------------------------+----------+
|                 root.ln.wf01|  INTERNAL|
|                 root.ln.wf02|  INTERNAL|
|                 root.ln.wf20|  INTERNAL|
|            root.ln.wf01.wt01|    DEVICE|
|            root.ln.wf01.wt02|    DEVICE|
|            root.ln.wf01.wt03|    DEVICE|
|            root.ln.wf01.wt04|    DEVICE|
|            root.ln.wf01.wt05|    DEVICE|
|            root.ln.wf01.wt07|    DEVICE|
|            root.ln.wf01.wt08|    DEVICE|
|            root.ln.wf02.wt02|    DEVICE|
|            root.ln.wf02.wt03|    DEVICE|
|            root.ln.wf20.wt20|    DEVICE|
|     root.ln.wf01.wt01.status|TIMESERIES|
|root.ln.wf01.wt01.temperature|TIMESERIES|
|      root.ln.wf01.wt02.speed|TIMESERIES|
|     root.ln.wf01.wt02.status|TIMESERIES|
|root.ln.wf01.wt02.temperature|TIMESERIES|
|   root.ln.wf01.wt03.hardware|TIMESERIES|
|     root.ln.wf01.wt03.status|TIMESERIES|
|   root.ln.wf01.wt04.hardware|TIMESERIES|
|     root.ln.wf01.wt04.status|TIMESERIES|
|     root.ln.wf01.wt05.status|TIMESERIES|
|root.ln.wf01.wt05.temperature|TIMESERIES|
|     root.ln.wf01.wt07.status|TIMESERIES|
|root.ln.wf01.wt07.temperature|TIMESERIES|
|     root.ln.wf01.wt08.status|TIMESERIES|
|root.ln.wf01.wt08.temperature|TIMESERIES|
|      root.ln.wf02.wt02.speed|TIMESERIES|
|     root.ln.wf02.wt02.status|TIMESERIES|
|root.ln.wf02.wt02.temperature|TIMESERIES|
|   root.ln.wf02.wt03.hardware|TIMESERIES|
|     root.ln.wf02.wt03.status|TIMESERIES|
|root.ln.wf20.wt20.temperature|TIMESERIES|
|                 root.sg.wf31|  INTERNAL|
|            root.sg.wf31.wt31|    DEVICE|
|root.sg.wf31.wt31.temperature|TIMESERIES|
+-----------------------------+----------+
Total line number = 37
It costs 0.013s
IoTDB> show child paths root;
+----------+---------+
|ChildPaths|NodeTypes|
+----------+---------+
|   root.aa| DATABASE|
|   root.ln| DATABASE|
|   root.sg| DATABASE|
+----------+---------+
Total line number = 3
It costs 0.011s
IoTDB> show child paths root.ln;
+------------+---------+
|  ChildPaths|NodeTypes|
+------------+---------+
|root.ln.wf01| INTERNAL|
|root.ln.wf02| INTERNAL|
|root.ln.wf20| INTERNAL|
+------------+---------+
Total line number = 3
It costs 0.006s
IoTDB> show child paths root.ln.*;
+-----------------+---------+
|       ChildPaths|NodeTypes|
+-----------------+---------+
|root.ln.wf01.wt01|   DEVICE|
|root.ln.wf01.wt02|   DEVICE|
|root.ln.wf01.wt03|   DEVICE|
|root.ln.wf01.wt04|   DEVICE|
|root.ln.wf01.wt05|   DEVICE|
|root.ln.wf01.wt07|   DEVICE|
|root.ln.wf01.wt08|   DEVICE|
|root.ln.wf02.wt02|   DEVICE|
|root.ln.wf02.wt03|   DEVICE|
|root.ln.wf20.wt20|   DEVICE|
+-----------------+---------+
Total line number = 10
It costs 0.005s
IoTDB> show child paths root.ln.**;
+-----------------------------+----------+
|                   ChildPaths| NodeTypes|
+-----------------------------+----------+
|            root.ln.wf01.wt01|    DEVICE|
|            root.ln.wf01.wt02|    DEVICE|
|            root.ln.wf01.wt03|    DEVICE|
|            root.ln.wf01.wt04|    DEVICE|
|            root.ln.wf01.wt05|    DEVICE|
|            root.ln.wf01.wt07|    DEVICE|
|            root.ln.wf01.wt08|    DEVICE|
|            root.ln.wf02.wt02|    DEVICE|
|            root.ln.wf02.wt03|    DEVICE|
|            root.ln.wf20.wt20|    DEVICE|
|     root.ln.wf01.wt01.status|TIMESERIES|
|root.ln.wf01.wt01.temperature|TIMESERIES|
|      root.ln.wf01.wt02.speed|TIMESERIES|
|     root.ln.wf01.wt02.status|TIMESERIES|
|root.ln.wf01.wt02.temperature|TIMESERIES|
|   root.ln.wf01.wt03.hardware|TIMESERIES|
|     root.ln.wf01.wt03.status|TIMESERIES|
|   root.ln.wf01.wt04.hardware|TIMESERIES|
|     root.ln.wf01.wt04.status|TIMESERIES|
|     root.ln.wf01.wt05.status|TIMESERIES|
|root.ln.wf01.wt05.temperature|TIMESERIES|
|     root.ln.wf01.wt07.status|TIMESERIES|
|root.ln.wf01.wt07.temperature|TIMESERIES|
|     root.ln.wf01.wt08.status|TIMESERIES|
|root.ln.wf01.wt08.temperature|TIMESERIES|
|      root.ln.wf02.wt02.speed|TIMESERIES|
|     root.ln.wf02.wt02.status|TIMESERIES|
|root.ln.wf02.wt02.temperature|TIMESERIES|
|   root.ln.wf02.wt03.hardware|TIMESERIES|
|     root.ln.wf02.wt03.status|TIMESERIES|
|root.ln.wf20.wt20.temperature|TIMESERIES|
+-----------------------------+----------+
Total line number = 31
It costs 0.011s
IoTDB> 

#查看路径的所有子节点
语法:SHOW CHILD NODES pathPattern

show child nodes root;
show child nodes root.ln;
show child nodes root.ln.wf01;

IoTDB> 
IoTDB> show child nodes root;
+----------+
|ChildNodes|
+----------+
|        aa|
|        ln|
|        sg|
+----------+
Total line number = 3
It costs 0.008s
IoTDB> show child nodes root.ln;
+----------+
|ChildNodes|
+----------+
|      wf01|
|      wf02|
|      wf20|
+----------+
Total line number = 3
It costs 0.005s
IoTDB> show child nodes root.ln.wf01;
+----------+
|ChildNodes|
+----------+
|      wt01|
|      wt02|
|      wt03|
|      wt04|
|      wt05|
|      wt07|
|      wt08|
+----------+
Total line number = 7
It costs 0.006s
IoTDB> 


#设置数据存活时间(TTL)

set ttl to root.ln 3600000;
set ttl to root.ln.** 3600000;
set ttl to root.** 3600000;

unset ttl to root.ln;
unset ttl to root.sg.**;
unset ttl to root.**;

show all ttl;

#查看 Region
show regions;
show regions of database root.ln;
show regions of database root.sg;

相关文章:

  • 官网设计报价宁波seo排名优化哪家好
  • 西部数码网站管理助手 绑定域名常州seo建站
  • 建设网站收取广告费用短视频营销优势
  • 微信小程序代码怎么弄山西优化公司
  • 深圳外贸网站制作公司如何模板建站
  • 下载做蛋糕网站seo是啥意思
  • 内存泄漏系列专题分析之二十四:内存泄漏测试Camera相机进程内存指标分布report概述
  • 02-StarRocks数据导入导出FAQ
  • 猿人学js逆向比赛第一届第十二题
  • MemcacheRedis--缓存服务器理论
  • MR7350用TTL刷机救砖过程
  • 桌面小屏幕实战课程:DesktopScreen 8 非易失性存储器NVS
  • 安卓9.0系统修改定制化____安卓9.0修改 默认开启开发者选项与usb调试的操作步骤解析 十一
  • Vue项目使用defer优化页面白屏,性能优化提升,秒加载!!!
  • 大白话蓝牙中的RPC:Remote Procedure Call远程过程调用
  • 夏季小学期
  • DEYOLO 全面复现,将双增强跨模态目标检测网络 DEYOLO 融合到 YOLOFuse 框架
  • 微信小程序节点相关总结
  • 入门级STM32F103C8T6无人机(原理图其一)
  • Proteus 8.17下载安装保姆级教程【2025最新版】附安装包
  • Android Navigation 原理解析
  • C++字符串的行输入
  • 华为服务器的选型指南
  • AI + 化学实验:从“黑匣子”到“显微镜”,人工智能如何让化学研究更聪明?
  • TouchDIVER Pro触觉手套:虚拟现实中的多模态交互新选择
  • day41/60