跳至主要內容

TsFile 快速上手

大约 1 分钟

TsFile 快速上手

数据示例

安装方式

pom.xmldependencies中添加以下内容

<dependency>
    <groupId>org.apache.tsfile</groupId>
    <artifactId>tsfile</artifactId>
    <version>1.0.0</version>
</dependency>

写入流程

构造 TsFileWriter

File f = new File("test.tsfile");
TsFileWriter tsFileWriter = new TsFileWriter(f);

注册时间序列

List<MeasurementSchema> schema1 = new ArrayList<>();
schema1.add(new MeasurementSchema("电压", TSDataType.FLOAT));
schema1.add(new MeasurementSchema("电流", TSDataType.FLOAT));
tsFileWriter.registerTimeseries(new Path("太阳能板1"), schema1);

List<MeasurementSchema> schema2 = new ArrayList<>();
schema2.add(new MeasurementSchema("电压", TSDataType.FLOAT));
schema2.add(new MeasurementSchema("电流", TSDataType.FLOAT));
schema2.add(new MeasurementSchema("风速", TSDataType.FLOAT));
tsFileWriter.registerTimeseries(new Path("风机1"), schema2);

写入数据

TSRecord tsRecord = new TSRecord(1, "太阳能板1");
tsRecord.addTuple(DataPoint.getDataPoint(TSDataType.FLOAT, "电压", 1.1f));
tsRecord.addTuple(DataPoint.getDataPoint(TSDataType.FLOAT, "电流", 2.2f));
tsFileWriter.write(tsRecord);

关闭文件

tsFileWriter.close();

示例代码

https://github.com/apache/tsfile/blob/develop/java/examples/src/main/java/org/apache/tsfile/TsFileWriteWithTSRecord.javaopen in new window

查询流程

构造 TsFileReader

TsFileSequenceReader reader = new TsFileSequenceReader(path);
TsFileReader tsFileReader = new TsFileReader(reader);

构造查询请求

ArrayList<Path> paths = new ArrayList<>();
paths.add(new Path("太阳能板1", "电压",true));
paths.add(new Path("太阳能板1", "电流",true));

IExpression timeFilter =
    BinaryExpression.and(
        new GlobalTimeExpression(TimeFilterApi.gtEq(4L)),
        new GlobalTimeExpression(TimeFilterApi.ltEq(10L)));

QueryExpression queryExpression = QueryExpression.create(paths, timeFilter);

查询数据

QueryDataSet queryDataSet = tsFileReader.query(queryExpression);
while (queryDataSet.hasNext()) {
  queryDataSet.next();
}

关闭文件

tsFileReader.cloFan 1se();

示例代码

https://github.com/apache/tsfile/blob/develop/java/examples/src/main/java/org/apache/tsfile/TsFileRead.javaopen in new window

Copyright © 2024 The Apache Software Foundation.
Apache and the Apache feather logo are trademarks of The Apache Software Foundation

Have a question? Connect with us on issues.