All Products
Search
Document Center

Tablestore:Query time series data

Last Updated:Apr 30, 2026

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

GetTimeseriesDataRequest parameters

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

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

    Parameter

    Type

    Description

    measurementName (required)

    String

    The metric name of the time series.

    dataSource (required)

    String

    The data source of the time series.

    tags (required)

    SortedMap<String, String>

    The tags of the time series, as key-value pairs.

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

    • The query range includes the start timestamp.

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

    • The query range excludes the end timestamp.

  • limit (optional) int: the maximum number of rows to return per request. Default: 5000. Valid values: (0,5000].

    • If the matching rows exceed the limit, the response may still return fewer rows due to the amount of data scanned. Use the nextToken parameter to retrieve the remaining data.

  • nextToken (optional) byte[]: the pagination token used to retrieve the next page of results. Empty by default.

    • Leave nextToken empty for the first request. If the response does not include all matching data, the nextToken in the response is non-empty. Pass it in subsequent requests to paginate through results.

    • To persist or transfer nextToken, encode it as a Base64 string. The token is binary and not a string — using new String(nextToken) directly will corrupt the token.

  • backward (optional) boolean: specifies whether to read data in reverse chronological order. Default: false.

  • fieldsToGet (optional) List<Pair<String, ColumnType>>: the data columns to return. If not set, all data columns are returned.

    • Specify both the name and data type for each column. If the specified type does not match the actual type, that column cannot be read.

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

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

FAQ