Suggestion: Before you continue with TSDB SDK, we recommend you read Terms and HTTP API Reference first to learn the basics of HiTSDB and the usage of API.

This section describes how to use the SDK to write data. Before you start, make sure an HiTSDB object exists.

Point class is the class of HiTSDB time points. You can use the HiTSDB object’s Put method to write the Point data into the HiTSDB database.

Sample code

Write a Point (point in time) data entry every second:

package com.aliyun.hitsdb.client.example;

import java.io.IOException;

import com.aliyun.hitsdb.client.HiTSDB;
import com.aliyun.hitsdb.client.HiTSDBClientFactory;
import com.aliyun.hitsdb.client.HiTSDBConfig;
import com.aliyun.hitsdb.client.value.request.Point;

public class TestWrite {
    public static void main(String[] args) throws InterruptedException, IOException {
        // Create the HiTSDB object
        HiTSDBConfig config = HiTSDBConfig.address("example.hitsdb.com", 8242).config();
        HiTSDB tsdb = HiTSDBClientFactory.connect(config);

        // Build and put data into HiTSDB
        for (int i = 0; i < 3600; i++) {
            Point point = Point.metric("test").tag("V", "1.0").value(System.currentTimeMillis(), 123.4567).build();
            Thread.sleep(1000);  // One submission per second
            tsdb.put(point);
        }

        // Safely shut down the client to prevent data loss. 
        System.out.println("Shutdown");
        tsdb.close();
    }
}

Query data

This section describes how to use the SDK to read data. Before you start, make sure a TSDB object exists.

The Query class of TSDB defines the query conditions. Build the query conditions before querying data. You can use TSDB object’s Query method to query data that meet query conditions.

Sample code

Query one hour of data:

package com.aliyun.hitsdb.client.example;

import java.io.IOException;
import java.util.List;

import com.aliyun.hitsdb.client.HiTSDB;
import com.aliyun.hitsdb.client.HiTSDBClientFactory;
import com.aliyun.hitsdb.client.HiTSDBConfig;
import com.aliyun.hitsdb.client.value.request.Query;
import com.aliyun.hitsdb.client.value.request.SubQuery;
import com.aliyun.hitsdb.client.value.response.QueryResult;
import com.aliyun.hitsdb.client.value.type.Aggregator;

public class TestRead {
    public static void main(String[] args) throws IOException {
        // Create the HiTSDB object
        HiTSDBConfig config = HiTSDBConfig.address("example.hitsdb.com", 8242).config();
        HiTSDB tsdb = HiTSDBClientFactory.connect(config);

        // Build query conditions and query data. 
        long now = System.currentTimeMillis();

        // Query one hour of data
        Query query = Query.timeRange(now - 3600 * 1000, now)
                .sub(SubQuery.metric("test").aggregator(Aggregator.NONE).tag("V", "1.0").build()).build();

        // Query data
        List<QueryResult> result = tsdb.query(query);

        // Printout
        System.out.println(result);

        // Safely shut down the client to prevent data loss.
        tsdb.close();
    }
}

Suggestion: Before using TSDB SDK, we recommend that you review Terms and [HTTP API Reference] for basic concepts of TSDB and how to use the API.