You can call the GetTimeseriesData operation to query the time series data that meets the specified conditions in a time series table.
Prerequisites
Time series data is written to the time series table from which you want to query data. For more information, see Write time series data.
A TimeseriesClient instance is initialized. For more information, see Initialize an OTSClient instance.
Parameters
Parameter | Description |
timeseriesKey | The identifiers of the time series that you want to query. You can use the following parameters to specify the identifiers:
|
timeRange | The time range for the query. The time range is a left-closed, right-open interval. You can use the following parameters to specify a time range:
|
backward | Specifies whether to sort the query results in reverse chronological order. This parameter allows you to obtain the latest data in a time series. Valid values:
|
fieldsToGet | The names of the columns that you want to return. If you do not specify this parameter, all columns are queried. Important When you configure the fieldsToGet parameter, you must specify the name and data type of each column that you want to return. If the specified name and data type of a column do not match, the data of the column cannot be returned. |
limit | The maximum number of rows that you want to return. Note The limit parameter specifies only the maximum number of rows that you want to return. Even if the number of rows that meet the specified conditions exceeds the limit, the number of rows that are returned may be less than the limit due to other reasons such as the maximum amount of data for a scan. In this case, you can obtain the remaining rows by specifying the nextToken parameter. |
nextToken | If only some rows that meet the specified conditions are returned in a query, the response contains the nextToken parameter. You can specify the nextToken parameter in the next request to obtain the remaining rows. |
Examples
The following sample code provides an example on how to query the time series data that meets the specified conditions in a time series table named test_timeseries_table:
private static void getTimeseriesData(TimeseriesClient client) {
String tableName = "test_timeseries_table";
GetTimeseriesDataRequest getTimeseriesDataRequest = new GetTimeseriesDataRequest(tableName);
Map<String, String> tags = new HashMap<String, String>();
tags.put("region", "hangzhou");
tags.put("os", "Ubuntu16.04");
// Specify the measurement name, data source, and tags of a time series to construct the identifiers of the time series.
TimeseriesKey timeseriesKey = new TimeseriesKey("cpu", "host_0", tags);
getTimeseriesDataRequest.setTimeseriesKey(timeseriesKey);
// Specify the time range.
getTimeseriesDataRequest.setTimeRange(0, (System.currentTimeMillis() + 60 * 1000) * 1000);
// Specify the maximum number of rows that you want to return.
getTimeseriesDataRequest.setLimit(10);
// Optional. Specify whether to sort the query results in reverse chronological order. Default value: false. If you set this parameter to true, the query results are sorted in reverse chronological order.
getTimeseriesDataRequest.setBackward(false);
// Optional. Specify the columns that you want to return. If you do not specify this parameter, all columns are returned.
getTimeseriesDataRequest.addFieldToGet("string_1", ColumnType.STRING);
getTimeseriesDataRequest.addFieldToGet("long_1", ColumnType.INTEGER);
GetTimeseriesDataResponse getTimeseriesDataResponse = client.getTimeseriesData(getTimeseriesDataRequest);
System.out.println(getTimeseriesDataResponse.getRows().size());
if (getTimeseriesDataResponse.getNextToken() != null) {
// If the nextToken parameter is not empty, you can initiate another request to obtain the remaining rows.
getTimeseriesDataRequest.setNextToken(getTimeseriesDataResponse.getNextToken());
getTimeseriesDataResponse = client.getTimeseriesData(getTimeseriesDataRequest);
System.out.println(getTimeseriesDataResponse.getRows().size());
}
}