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. For more information, see Write time series data.
  • The TimeseriesClient is initialized. For more information, see Initialization.

Parameters

Parameter Description
timeseriesKey The identifier of the time series that you want to query. The identifier includes the following content:
  • measurementName: the measurement name of the time series.
  • dataSource: the data source of the time series. You can leave this parameter empty.
  • tags: the tags of the time series. The tags are multiple key-value pairs of the STRING type.
timeRange The time range for the query. The time range includes the following content:
  • beginTimeInUs: the start time.
  • endTimeInUs: the end time.
backward Specifies whether to sort the query results in reverse chronological order. This allows you to obtain the latest data in a time series. Valid values:
  • true: sorts the query results in reverse chronological order.
  • false: sorts the query results in chronological order. This is the default value.
fieldsToGet The columns that you want to query. If you do not specify this parameter, all columns are queried.
Notice When you specify the fieldsToGet parameter, you must specify the name and data type of each column that you want to query. If the specified data type of a column is not that of the column in the time series table, the data of the column cannot be queried.
limit The maximum number of rows to return.
Note The limit parameter limits only the maximum number of rows to return. Even if the number of rows that meets the specified conditions exceeds the limit, the number of rows that are returned may be less than the value of the limit parameter due to other limits such as the maximum amount of data for a scan. In this case, you can obtain the remaining rows by using the nextToken parameter.
nextToken The token that is used to obtain more results. 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.

Example

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");
    // Use the measurement name, data source, and tags of a time series to construct the identifier 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 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 query. If you do not specify this parameter, all columns are queried. 
    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 a request again to obtain the remaining data. 
        getTimeseriesDataRequest.setNextToken(getTimeseriesDataResponse.getNextToken());
        getTimeseriesDataResponse = client.getTimeseriesData(getTimeseriesDataRequest);
        System.out.println(getTimeseriesDataResponse.getRows().size());
    }
}