All Products
Search
Document Center

Time Series Database:Use the SDK multi-value data model to read data

Last Updated:May 22, 2020

Sample code

  1. /*
  2. * Use the multiFieldQuery() API.
  3. * You can create a request to query multi-value data by using this process: Specify the MultiFieldSubQueryDetails (list) information, create a MultiFieldSubQuery request, and then create a MultiFieldQuery request.
  4. * You can create multiple subqueries for each multi-value data query.
  5. *
  6. * You must provide the following information when you perform a query:
  7. * MultiFieldSubQueryDetails: the details of the specific fields for the target metric. The fields are the sub-category of the metric. For example, you can query only the speed or direction fields.
  8. * You can specify the following parameters: aggregator, dpValue, rate, and downsample.
  9. * Metric: the metric to be queried, for example, wind.
  10. * Time Range: Start Time and End Time
  11. *
  12. * The following information is optional:
  13. * Tags: the conditions that are used to filter timelines.
  14. * Limit/Offset: the pagination details.
  15. */
  16. // Create the fields that you want to query.
  17. MultiFieldSubQueryDetails speedFieldDetails = MultiFieldSubQueryDetails
  18. .field("speed").aggregator(Aggregator.SUM).downsample("2s-sum").build();
  19. MultiFieldSubQueryDetails levelFieldDetails = MultiFieldSubQueryDetails
  20. .field("level").aggregator(Aggregator.AVG).downsample("2s-avg").build();
  21. MultiFieldSubQueryDetails tempFieldDetails = MultiFieldSubQueryDetails
  22. .field("temperature").aggregator(Aggregator.COUNT).downsample("2s-count").build();
  23. MultiFieldSubQueryDetails allFieldsDetails1 = MultiFieldSubQueryDetails
  24. .field("*").aggregator(Aggregator.MAX).downsample("2s-max").alias("max_").build();
  25. MultiFieldSubQueryDetails allFieldsDetails2 = MultiFieldSubQueryDetails
  26. .field("*").aggregator(Aggregator.MIN).downsample("2s-min").alias("min_").build();
  27. List<MultiFieldSubQueryDetails> subQueryDetails = new LinkedList<>();
  28. subQueryDetails.add(speedFieldDetails);
  29. subQueryDetails.add(levelFieldDetails);
  30. subQueryDetails.add(tempFieldDetails);
  31. subQueryDetails.add(allFieldsDetails1);
  32. subQueryDetails.add(allFieldsDetails2);
  33. // Create a subquery for the multi-value data model.
  34. MultiFieldSubQuery subQuery = MultiFieldSubQuery.metric(metric).fieldsInfo(subQueryDetails)
  35. .filter(Filter.filter(FilterType.LiteralOr, "district", "Yuhang|Xiacheng|Xihu", true).build())
  36. .build();
  37. // Create a query for the multi-value data model.
  38. MultiFieldQuery query = MultiFieldQuery.start(1542772000L).end(1542772020L).msResolution(false)
  39. .sub(subQuery).build();
  40. List<MultiFieldQueryResult> results = tsdb.multiFieldQuery(query);