All Products
Search
Document Center

Tablestore:Query time series data

Last Updated:Aug 12, 2024

You can call the GetTimeseriesData operation to query the time series data that meets the specified conditions in a time series table.

Prerequisites

Parameters

Parameter

Description

timeseriesKey

The identifier of the time series that you want to query. The identifier includes the following parameters:

  • measurementName: the measurement name of the time series. You can leave this parameter empty.

  • dataSource: the data source of the time series. You can leave this parameter empty.

  • tags: the tags of the time series. Tags are key-value pairs of the STRING type.

timeRange

The time range for the query. The time range is a left-open, right-closed interval. The time range includes the following parameters:

  • 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 return. If you do not specify this parameter, all columns are returned.

Important

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 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

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.

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 data. 
        getTimeseriesDataRequest.setNextToken(getTimeseriesDataResponse.getNextToken());
        getTimeseriesDataResponse = client.getTimeseriesData(getTimeseriesDataRequest);
        System.out.println(getTimeseriesDataResponse.getRows().size());
    }
}