This topic describes how to query time series data that meets specific conditions in a time series table by using Tablestore SDK for Java.
Prerequisites
A client is initialized. For more information, see Initialize a Tablestore client.
Method
public GetTimeseriesDataResponse getTimeseriesData(GetTimeseriesDataRequest request) throws TableStoreException, ClientExceptionSample code
The following sample code provides an example on how to query time series data that meets specific conditions in a time series table named timeseries_table_sample:
private static void getTimeseriesData(TimeseriesClient client) {
String tableName = "timeseries_table_sample";
GetTimeseriesDataRequest getTimeseriesDataRequest = new GetTimeseriesDataRequest(tableName);
// Specify the time series identifiers. In this example, the metric name is cpu, the data source is host_0, and the tags are "region=hangzhou" and "os=Ubuntu16.04".
Map<String, String> tags = new HashMap<String, String>();
tags.put("region", "hangzhou");
tags.put("os", "Ubuntu16.04");
TimeseriesKey timeseriesKey = new TimeseriesKey("cpu", "host_0", tags);
getTimeseriesDataRequest.setTimeseriesKey(timeseriesKey);
// Specify the time range.
getTimeseriesDataRequest.setTimeRange(0, (System.currentTimeMillis() + 60 * 1000) * 1000);
getTimeseriesDataRequest.setLimit(20);
// Query time series data.
GetTimeseriesDataResponse getTimeseriesDataResponse = client.getTimeseriesData(getTimeseriesDataRequest);
// Print the query results.
getTimeseriesDataResponse.getRows().forEach(row -> {
System.out.println("TimeseriesKey: " + row.getTimeseriesKey() + "; TimeInUs: " + row.getTimeInUs() + "; Fields: " + row.getFields());
});
}When you query time series data, you can also refer to the following sample code to configure specific settings:
Specify the maximum number of rows to return in a single request
getTimeseriesDataRequest.setLimit(20);Specify that data is read in reverse chronological order
// Read data in reverse chronological order to obtain the latest data of a time series. getTimeseriesDataRequest.setBackward(true);Specify the data columns that you want to return
getTimeseriesDataRequest.addFieldToGet("col_string", ColumnType.STRING); getTimeseriesDataRequest.addFieldToGet("col_long", ColumnType.INTEGER); getTimeseriesDataRequest.addFieldToGet("col_double", ColumnType.DOUBLE);Perform paged query by using the
nextTokenparameter// If the value of the nextToken parameter is not empty, you can initiate the next request. if (getTimeseriesDataResponse.getNextToken() != null) { // Obtain the value of the nextToken parameter. byte[] nextToken = getTimeseriesDataResponse.getNextToken(); /* // If you want to persist the nextToken parameter or transfer the nextToken parameter to the frontend page, you can use Base64 to encode the nextToken parameter into a string for storage and transmission. String tokenAsString = Base64.toBase64String(nextToken); // Decode the string back to byte[] type nextToken. byte[] tokenAsByte = Base64.fromBase64String(tokenAsString); */ // Specify the value of the nextToken parameter. getTimeseriesDataRequest.setNextToken(nextToken); // Query time series data. getTimeseriesDataResponse = client.getTimeseriesData(getTimeseriesDataRequest); // Print the query results. getTimeseriesDataResponse.getRows().forEach(row -> { System.out.println("TimeseriesKey: " + row.getTimeseriesKey() + "; TimeInUs: " + row.getTimeInUs() + "; Fields: " + row.getFields()); }); }