All Products
Search
Document Center

Tablestore:Query time series data

Last Updated:Apr 22, 2025

You can query time series data that meets specific conditions by calling the GetTimeseriesData operation.

Prerequisites

A client is initialized. For more information, see Initialize a Tablestore client.

Parameters

The following table describes the parameters included in request.

Parameter

Description

timeseriesTableName (required)

The name of the time series table.

timeseriesKey (required)

The identifiers of the time series. This parameter includes the following configuration items:

  • measurement_name (required): the metric name of the time series.

  • data_source (required): the data source information.

  • tags (required): the tag information of the time series, which consists of multiple key-value pairs.

beginTimeInUs (optional)

The time range to query, in microseconds. Default value: 0.

beginTimeInUs and endTimeInUs specify the start timestamp and end timestamp, respectively. The time range for the query is a left-closed, right-open interval.

endTimeInUs (optional)

backward (optional)

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:

  • True: sorts the query results in reverse chronological order.

  • False (default): does not sort the query results in reverse chronological order.

fieldsToGet (optional)

The columns to retrieve. If you do not specify this parameter, all columns are queried.

Important

You must explicitly specify the name and data type of each column that you want to retrieve by using the fieldsToGet parameter. If the data type does not match the actual data type, the data in the column cannot be read.

limit (optional)

The maximum number of rows that can be returned by this request. Default value: 5000. Valid values: (0,5000].

Note

The limit parameter limits 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 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 (optional)

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.

Important

If you want to persist nextToken or transfer nextToken to the frontend page, you can use Base64 to encode nextToken into a string. nextToken itself is not a string. If you directly use str(nextToken) to encode the token as a string, token information will be lost.

Examples

The following sample code provides an example on how to query time series data that meets specified conditions in a time series table:

# The tag information of the time series.
tags = {"tag1": "t1", "tag2": "t2"}
# The identifiers of the time series.
key = TimeseriesKey("measure1", "datasource1", tags)

try:
    request = GetTimeseriesDataRequest("")
    # The identifiers of the time series.
    request.timeseriesKey = key
    # The end timestamp of the time range to query.
    request.endTimeInUs = int(time.time() * 1000000)
    # The maximum number of rows that can be returned by the request.
    request.limit = 20
    # The columns to retrieve. If you do not specify this parameter, all columns are returned.
    request.fieldsToGet = {"string_field": DataType.STRING, "long_field": DataType.LONG}

    # Call the operation to query time series data.
    response = ots_client.get_timeseries_data(request)
    print(response.rows)

    # If nextToken is not empty, you can initiate the next request.
    if response.nextToken is not None:
        # Obtain nextToken.
        next_token = response.nextToken

        # If you want to persist nextToken or transfer nextToken to the frontend page, you can use Base64 to encode nextToken into a string for storage and transmission.
        # The token itself is not a string. If you directly use str(nextToken) to encode the token as a string, token information will be lost.
        token_as_string = base64.b64encode(next_token).decode('utf-8')
        # Decode the string into bytes.
        next_token_byte = base64.b64decode(token_as_string.encode('utf-8'))

        # Call the operation to query time series data.
        request.nextToken = next_token
        response = ots_client.get_timeseries_data(request)
        print(response.rows)
    print("get timeseries data succeeded.")
except Exception as e:
    # If an exception is thrown, the call fails. Handle the exception.
    print("get timeseries data failed. %s" % e)