edit-icon download-icon

Usage sample

Last Updated: Jul 09, 2018

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

Write data

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:

  1. package com.aliyun.hitsdb.client.example;
  2. import java.io.IOException;
  3. import com.aliyun.hitsdb.client.HiTSDB;
  4. import com.aliyun.hitsdb.client.HiTSDBClientFactory;
  5. import com.aliyun.hitsdb.client.HiTSDBConfig;
  6. import com.aliyun.hitsdb.client.value.request.Point;
  7. public class TestWrite {
  8. public static void main(String[] args) throws InterruptedException, IOException {
  9. // Create the HiTSDB object
  10. HiTSDBConfig config = HiTSDBConfig.address("example.hitsdb.com", 8242).config();
  11. HiTSDB tsdb = HiTSDBClientFactory.connect(config);
  12. // Build and put data into HiTSDB
  13. for (int i = 0; i < 3600; i++) {
  14. Point point = Point.metric("test").tag("V", "1.0").value(System.currentTimeMillis(), 123.4567).build();
  15. Thread.sleep(1000); // One submission per second
  16. tsdb.put(point);
  17. }
  18. // Safely shut down the client to prevent data loss.
  19. System.out.println("Shutdown");
  20. tsdb.close();
  21. }
  22. }

Query data

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

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

Sample code

Query one hour of data:

  1. package com.aliyun.hitsdb.client.example;
  2. import java.io.IOException;
  3. import java.util.List;
  4. import com.aliyun.hitsdb.client.HiTSDB;
  5. import com.aliyun.hitsdb.client.HiTSDBClientFactory;
  6. import com.aliyun.hitsdb.client.HiTSDBConfig;
  7. import com.aliyun.hitsdb.client.value.request.Query;
  8. import com.aliyun.hitsdb.client.value.request.SubQuery;
  9. import com.aliyun.hitsdb.client.value.response.QueryResult;
  10. import com.aliyun.hitsdb.client.value.type.Aggregator;
  11. public class TestRead {
  12. public static void main(String[] args) throws IOException {
  13. // Create the HiTSDB object
  14. HiTSDBConfig config = HiTSDBConfig.address("example.hitsdb.com", 8242).config();
  15. HiTSDB tsdb = HiTSDBClientFactory.connect(config);
  16. // Build query conditions and query data.
  17. long now = System.currentTimeMillis();
  18. // Query one hour of data
  19. Query query = Query.timeRange(now - 3600 * 1000, now)
  20. .sub(SubQuery.metric("test").aggregator(Aggregator.NONE).tag("V", "1.0").build()).build();
  21. // Query data
  22. List<QueryResult> result = tsdb.query(query);
  23. // Printout
  24. System.out.println(result);
  25. // Safely shut down the client to prevent data loss.
  26. tsdb.close();
  27. }
  28. }


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

Thank you! We've received your feedback.