All Products
Search
Document Center

Use the SDK multi-value data model to write data

Last Updated: May 24, 2020

This topic uses sample code to describe how to use the multi-value data model to write data.

Note: The multi-value data model is supported only by SDK V0.2.0 and later. For more information about the latest SDK version and download URL, see SDK release notes (66207). The multi-value data model supported by the SDK that is earlier than V0.2.0 is a simulated multi-value data model. We recommend that you do not use the simulated multi-value data model. You cannot use the latest multiField class in a SDK to query data that was previously written by using the multiValue class.

Write data based on the multi-value data model

Sample code

  1. /*
  2. * Use the multiFieldPutSync() API.
  3. * This API allows you to write a single data point or a list of multiple data points.
  4. *
  5. * You must specify the following information when you write data:
  6. * Metric: the type of the metric. It is similar to the concept of measurement in InfluxDB.
  7. * Fields: the measurement details of the metric. Fields are the sub-category of the metric. You can create multiple fields for each metric. For example, you can create the following fields for the wind metric: direction, speed, description, and temperature.
  8. * Timestamp: the timestamp of a data point.
  9. * Tags: the additional information of a timeline. Two examples of tag key-value pairs are model=ABC123 and factory No.=1234567890.
  10. */
  11. MultiFieldPoint point1 = MultiFieldPoint.metric("wind").timestamp(1346846400L)
  12. .fields("speed", 20.0)
  13. .fields("level", 5)
  14. .fields("direction", "NE")
  15. .fields("description", "Breeze")
  16. .fields("temperature", "18.9")
  17. .tag("sensor", "IOTE_1988_0001")
  18. .tag("district", "Yuhang")
  19. .tag("city", "Hangzhou")
  20. .tag("province", "Zhejiang")
  21. .build();
  22. // Synchronously write data.
  23. tsdb.multiFieldPutSync(multiValuedPoint);
  24. // Synchronously write data and obtain the summary of the write result.
  25. tsdb.multiFieldPutSync(multiValuedPoint, SummaryResult.class);
  26. // Synchronously write data and obtain the details about the write result. The write operation may succeed or fail.
  27. tsdb.multiFieldPutSync(multiValuedPoint, DetailsResult.class);

Asynchronously write data based on the multi-value data model

Note: You can asynchronously write data based on the multi-value data model only by using SDK V0.2.1 and later. For more information about the latest SDK version and download URL, see SDK release notes.

Do not use the callback method

By default, the TSDBConfig class does not include the callback method that can be used to asynchronously write data based on the multi-value data model.Sample code

  1. TSDBConfig config = TSDBConfig.address("127.0.0.1", 8242)
  2. // The amount of data that is submitted each time you perform a batch write operation.
  3. .batchPutSize(500)
  4. // The number of threads that are used to asynchronously write single-value data.
  5. .batchPutConsumerThreadCount(1)
  6. // The cache queue length for the asynchronous write of multi-value data.
  7. .multiFieldBatchPutBufferSize(10000)
  8. // The number of threads that are used to asynchronously write multi-value data.
  9. .multiFieldBatchPutConsumerThreadCount(1)
  10. .config();
  11. // Note that TSDB needs to be initialized only once.
  12. TSDB tsdb = TSDBClientFactory.connect(config);
  13. MultiFieldPoint point = MultiFieldPoint
  14. .metric("test-test-test")
  15. .tag("a", "1")
  16. .tag("b", "2")
  17. .timestamp(System.currentTimeMillis())
  18. .field("f1", Math.random())
  19. .field("f2", Math.random())
  20. .build();
  21. tsdb.multiFieldPut(point);
  22. // Note that you do not need to close the TSDB instance unless the application lifecycle ends.
  23. tsdb.close();
  24. }

Use the common callback method

The common callback method returns only the data point list if the write operation is successful. If the write operation fails, the method returns the data point list and the write errors.

Sample code

  1. // Create a common callback method to handle the write result. The write operation may succeed or fail.
  2. AbstractMultiFieldBatchPutCallback callback = new MultiFieldBatchPutCallback() {
  3. @Override
  4. public void response(String address, List<MultiFieldPoint> points, Result result) {
  5. int count = num.addAndGet(points.size());
  6. System.out.println(result);
  7. System.out.println("processed" + count + "data points");
  8. }
  9. final AtomicInteger num = new AtomicInteger();
  10. @Override
  11. public void failed(String address, List<MultiFieldPoint> points, Exception ex) {
  12. System.err.println("Service callback error" + points.size() + " error!") ;
  13. ex.printStackTrace();
  14. }
  15. };
  16. TSDBConfig config = TSDBConfig.address("127.0.0.1", 8242)
  17. .httpConnectTimeout(90)
  18. // The amount of data that is submitted each time you perform a batch write operation.
  19. .batchPutSize(500)
  20. // The number of threads that are used to asynchronously write single-value data.
  21. .batchPutConsumerThreadCount(1)
  22. // The cache queue length for the asynchronous write of multi-value data.
  23. .multiFieldBatchPutBufferSize(10000)
  24. // The number of threads that are used to asynchronously write multi-value data.
  25. .multiFieldBatchPutConsumerThreadCount(1)
  26. // The callback method for the asynchronous write of multi-value data.
  27. .listenMultiFieldBatchPut(callback)
  28. .config();
  29. // Note that TSDB needs to be initialized only once.
  30. TSDB tsdb = TSDBClientFactory.connect(config);
  31. MultiFieldPoint point = MultiFieldPoint
  32. .metric("test-test-test")
  33. .tag("a", "1")
  34. .tag("b", "2")
  35. .timestamp(System.currentTimeMillis())
  36. .field("f1", Math.random())
  37. .field("f2", Math.random())
  38. .build();
  39. tsdb.multiFieldPut(point);
  40. // Note that you do not need to close the TSDB instance unless the application lifecycle ends.
  41. tsdb.close();
  42. }

Use the callback method to obtain a summary

If the write operation is successful, the summary callback method returns the data point list and the write summary. The write summary includes the number of successfully written data points and the number of unsuccessfully written data points. If the write operation fails, the summary callback method returns the data point list and the write errors.

Sample code

  1. // Create a callback method to obtain a summary of the write operation. The write operation may succeed or fail.
  2. AbstractMultiFieldBatchPutCallback callback = new MultiFieldBatchPutSummaryCallback() {
  3. final AtomicInteger num = new AtomicInteger();
  4. @Override
  5. public void response(String address, List<MultiFieldPoint> points, SummaryResult result) {
  6. int count = num.addAndGet(points.size());
  7. System.out.println(result);
  8. System.out.println("processed" + count + "data points");
  9. }
  10. @Override
  11. public void failed(String address, List<MultiFieldPoint> points, Exception ex) {
  12. System.err.println("Service callback error" + points.size() + " error!") ;
  13. ex.printStackTrace();
  14. }
  15. };
  16. TSDBConfig config = TSDBConfig.address("127.0.0.1", 8242)
  17. .httpConnectTimeout(90)
  18. // The amount of data that is submitted each time you perform a batch write operation.
  19. .batchPutSize(500)
  20. // The number of threads that are used to asynchronously write single-value data.
  21. .batchPutConsumerThreadCount(1)
  22. // The cache queue length for the asynchronous write of multi-value data.
  23. .multiFieldBatchPutBufferSize(10000)
  24. // The number of threads that are used to asynchronously write multi-value data.
  25. .multiFieldBatchPutConsumerThreadCount(1)
  26. // The callback method for the asynchronous write of multi-value data.
  27. .listenMultiFieldBatchPut(callback)
  28. .config();
  29. // Note that TSDB needs to be initialized only once.
  30. TSDB tsdb = TSDBClientFactory.connect(config);
  31. MultiFieldPoint point = MultiFieldPoint
  32. .metric("test-test-test")
  33. .tag("a", "1")
  34. .tag("b", "2")
  35. .timestamp(System.currentTimeMillis())
  36. .field("f1", Math.random())
  37. .field("f2", Math.random())
  38. .build();
  39. tsdb.multiFieldPut(point);
  40. // Note that you do not need to close the TSDB instance unless the application lifecycle ends.
  41. tsdb.close();
  42. }

Use the callback method to obtain details

If the write operation is successful, the detail callback method returns the data point list and the write details. The write details may include the number of unsuccessfully written data points and the error information. If the write operation fails, the callback method returns the data point list and the write errors.Sample code

  1. // Create a callback method to obtain the details of a write operation. The write operation may succeed or fail.
  2. MultiFieldBatchPutDetailsCallback callback = new MultiFieldBatchPutDetailsCallback() {
  3. final AtomicInteger num = new AtomicInteger();
  4. @Override
  5. public void response(String address, List<MultiFieldPoint> points, DetailsResult result) {
  6. int count = num.addAndGet(points.size());
  7. System.out.println(result);
  8. System.out.println("processed" + count + "data points");
  9. }
  10. @Override
  11. public void failed(String address, List<MultiFieldPoint> points, Exception ex) {
  12. System.err.println("Service callback error" + points.size() + " error!") ;
  13. ex.printStackTrace();
  14. }
  15. };
  16. TSDBConfig config = TSDBConfig.address("127.0.0.1", 8242)
  17. .httpConnectTimeout(90)
  18. // The amount of data that is submitted each time you perform a batch write operation.
  19. .batchPutSize(500)
  20. // The number of threads that are used to asynchronously write single-value data.
  21. .batchPutConsumerThreadCount(1)
  22. // The cache queue length for the asynchronous write of multi-value data.
  23. .multiFieldBatchPutBufferSize(10000)
  24. // The number of threads that are used to asynchronously write multi-value data.
  25. .multiFieldBatchPutConsumerThreadCount(1)
  26. // The callback method for the asynchronous write of multi-value data.
  27. .listenMultiFieldBatchPut(callback)
  28. .config();
  29. // Note that TSDB needs to be initialized only once.
  30. TSDB tsdb = TSDBClientFactory.connect(config);
  31. MultiFieldPoint point = MultiFieldPoint
  32. .metric("test-test-test")
  33. .tag("a", "1")
  34. .tag("b", "2")
  35. .timestamp(System.currentTimeMillis())
  36. .field("f1", Math.random())
  37. .field("f2", Math.random())
  38. .build();
  39. tsdb.multiFieldPut(point);
  40. // Note that you do not need to close the TSDB instance unless the application lifecycle ends.
  41. tsdb.close();
  42. }