Query time series data that meets specific conditions from a time series table using the 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, ClientException
Sample code
The following example queries time series data from 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());
});
}
You can also configure the following optional settings:
-
Set the maximum number of rows per request
getTimeseriesDataRequest.setLimit(20); -
Read data in reverse chronological order
// Read data in reverse chronological order to get the latest data of a time series. getTimeseriesDataRequest.setBackward(true); -
Specify the data columns to return
getTimeseriesDataRequest.addFieldToGet("col_string", ColumnType.STRING); getTimeseriesDataRequest.addFieldToGet("col_long", ColumnType.INTEGER); getTimeseriesDataRequest.addFieldToGet("col_double", ColumnType.DOUBLE); -
Paginate results using the
nextTokenparameter// If nextToken is not null, more data is available. if (getTimeseriesDataResponse.getNextToken() != null) { // Get the nextToken value. byte[] nextToken = getTimeseriesDataResponse.getNextToken(); /* // To persist or transfer nextToken, encode it as a Base64 string. String tokenAsString = Base64.toBase64String(nextToken); // Decode the string back to byte[]. byte[] tokenAsByte = Base64.fromBase64String(tokenAsString); */ // Pass nextToken to retrieve the next page. 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()); }); }