Skip to main content

Quick Start

About 1 min

Quick Start

Sample Data

Installation Method

Add the following content to the dependencies in pom.xml

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

Writing Process

Construct TsFileWriter

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

Registration Time Series

List<MeasurementSchema> schema1 = new ArrayList<>();
schema1.add(new MeasurementSchema("voltage", TSDataType.FLOAT));
schema1.add(new MeasurementSchema("current", TSDataType.FLOAT));
tsFileWriter.registerTimeseries(new Path("Solar_panel_1"), schema1);

List<MeasurementSchema> schema2 = new ArrayList<>();
schema2.add(new MeasurementSchema("voltage", TSDataType.FLOAT));
schema2.add(new MeasurementSchema("current", TSDataType.FLOAT));
schema2.add(new MeasurementSchema("wind_speed", TSDataType.FLOAT));
tsFileWriter.registerTimeseries(new Path("Fan_1"), schema2);

Write Data

TSRecord tsRecord = new TSRecord(1, "Solar_panel_1");
tsRecord.addTuple(DataPoint.getDataPoint(TSDataType.FLOAT, "voltage", 1.1f));
tsRecord.addTuple(DataPoint.getDataPoint(TSDataType.FLOAT, "current", 2.2f));
tsFileWriter.write(tsRecord);

Close File

tsFileWriter.close();

Sample Code

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

Query Process

Construct TsFileReader

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

Construct Query Request

ArrayList<Path> paths = new ArrayList<>();
paths.add(new Path("Solar_panel_1", "voltage",true));
paths.add(new Path("Solar_panel_1", "current",true));

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

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

Query Data

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

Close File

tsFileReader.close();

Sample Code

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.