All Products
Search
Document Center

Tablestore:Query time series data

Last Updated:May 14, 2025

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

GetTimeseriesDataRequest parameters

  • timeseriesTableName (required) String: the name of the time series table.

  • timeseriesKey (required) TimeseriesKey: the identifiers of the time series. The following tables describes the parameters that specify the identifiers.

    Parameter

    Type

    Description

    measurementName (required)

    String

    The metric name of the time series.

    dataSource (required)

    String

    The data source information of the time series.

    tags (required)

    SortedMap<String, String>

    The tag information of the time series, which consists of multiple key-value pairs.

  • beginTimeInUs (required) long: the start timestamp in microseconds. The value must be greater than or equal to 0.

    • The query conditions include the start timestamp.

  • endTimeInUs (required) long: the end timestamp in microseconds. The value must be greater than 0.

    • The query conditions do not include the end timestamp.

  • limit (optional) int: the maximum number of rows to return in a single request. The default value is 5000. Valid value range: (0,5000].

    • Even if the number of rows that meet the query conditions exceeds the value of the limit parameter, the number of rows returned in a single request may still be less than the value of the limit parameter due to factors such as the amount of data scanned. You can use the nextToken parameter to obtain the remaining data.

  • nextToken (optional) byte[]: the token for paged query, which is used to obtain the next page of data. This parameter is empty by default.

    • For the first request, you must leave the nextToken parameter empty. If a request does not return all data that meets the query conditions, the nextToken parameter in the response is not empty. You can use the nextToken parameter for paged query.

    • 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. The nextToken parameter itself is not a string. If you directly use new String(nextToken) for encoding, token information will be lost.

  • backward (optional) boolean: specifies whether to read data in reverse chronological order. The default value is false.

  • fieldsToGet (optional) List<Pair<String, ColumnType>>: the data columns that you want to return. If you do not configure this parameter, all data columns are returned.

    • When you specify a value for the fieldsToGet parameter, you must explicitly specify the name and data type of each data column you want to return. If the data type of a data column does not match the actual type, the data in that column cannot be read.

Sample 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 nextToken parameter

    // 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());
        });
    }

FAQ