如何在Apache IoTDB中查询Schema及所有时间序列?含控制台与Java方案
查询Apache IoTDB的Schema与时间序列(控制台+Java API)
一、控制台操作步骤
1. 查看所有存储的时间序列
直接在控制台执行SQL命令:
SHOW TIMESERIES
如果需要过滤特定路径或名称,可加前缀匹配或模糊查询:
-- 查询指定存储组下的所有时间序列 SHOW TIMESERIES root.sg.* -- 模糊匹配包含"temp"的时间序列 SHOW TIMESERIES LIKE "%temp%"
2. 查询Schema结构信息
- 查看存储组:
SHOW STORAGE GROUP -- 查看指定路径的存储组 SHOW STORAGE GROUP root.sg
- 查看单个时间序列的详细Schema:
DESCRIBE TIMESERIES root.sg.device.temperature
执行后会返回该时间序列的数据类型、编码方式、压缩格式等核心属性。
- 查看树形节点结构:
-- 查看所有设备节点 SHOW DEVICES -- 逐层查看节点子结构 SHOW CHILD NODES root SHOW CHILD NODES root.sg
二、Java API实现方案
首先确保项目中引入IoTDB客户端依赖(以Maven为例):
<dependency> <groupId>org.apache.iotdb</groupId> <artifactId>iotdb-jdbc</artifactId> <version>1.1.0</version> <!-- 替换为你的IoTDB实际版本 --> </dependency>
1. 原生Session API(推荐,性能更优)
原生Session是IoTDB官方推荐的客户端方式,代码示例:
import org.apache.iotdb.session.Session; import org.apache.iotdb.tsfile.read.common.Field; import org.apache.iotdb.tsfile.read.common.RowRecord; import java.util.List; public class IoTDBSchemaQueryDemo { public static void main(String[] args) { // 初始化Session,参数根据实际环境调整 try (Session session = new Session("127.0.0.1", 6667, "root", "root")) { session.open(false); // 查询所有时间序列 List<RowRecord> tsRecords = session.executeQueryStatement("SHOW TIMESERIES"); for (RowRecord record : tsRecords) { List<Field> fields = record.getFields(); String tsPath = fields.get(0).getStringValue(); String dataType = fields.get(1).getStringValue(); System.out.printf("时间序列路径:%s | 数据类型:%s%n", tsPath, dataType); } // 查询存储组信息 List<RowRecord> sgRecords = session.executeQueryStatement("SHOW STORAGE GROUP"); for (RowRecord record : sgRecords) { System.out.printf("存储组路径:%s%n", record.getFields().get(0).getStringValue()); } // 查询单个时间序列的详细Schema List<RowRecord> detailRecords = session.executeQueryStatement("DESCRIBE TIMESERIES root.sg.device.temperature"); for (RowRecord record : detailRecords) { Field key = record.getFields().get(0); Field value = record.getFields().get(1); System.out.printf("%s: %s%n", key.getStringValue(), value.getStringValue()); } } catch (Exception e) { e.printStackTrace(); } } }
2. JDBC方式实现
如果习惯JDBC操作模式,也可以用标准JDBC接口:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class IoTDBJdbcSchemaDemo { public static void main(String[] args) { String jdbcUrl = "jdbc:iotdb://127.0.0.1:6667/"; String username = "root"; String password = "root"; try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password); Statement stmt = conn.createStatement()) { // 查询所有时间序列 String showTsSql = "SHOW TIMESERIES"; try (ResultSet rs = stmt.executeQuery(showTsSql)) { while (rs.next()) { String tsPath = rs.getString(1); String dataType = rs.getString(2); System.out.printf("时间序列:%s | 类型:%s%n", tsPath, dataType); } } // 查询存储组 String showSgSql = "SHOW STORAGE GROUP"; try (ResultSet rs = stmt.executeQuery(showSgSql)) { while (rs.next()) { System.out.printf("存储组:%s%n", rs.getString(1)); } } } catch (Exception e) { e.printStackTrace(); } } }
内容的提问来源于stack exchange,提问作者Thoreau I
相关产品推荐
相关产品推荐

