All Products
Search
Document Center

Tablestore:Query time series data

Last Updated:Mar 05, 2024

You can call the GetTimeseriesData operation provided by Tablestore to query the time series data in a time series within a specific time period. If you are not sure about the information about the time series whose data you want to query, such as the metric name and data source, you can query the time series by specifying conditions and then query time series data.

Overview

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

When you query time series data, you must specify the information about the time series whose data you want to query, and specify query conditions as required.

  • Specify whether to sort the query results in reverse chronological order. The reverse chronological order allows you to obtain the latest data in a time series.

  • Specify the columns that you want Tablestore to return. By default, all columns are returned.

  • Specify the maximum number of rows that you want Tablestore to return in a call.

Methods

You can query time series data by using the Tablestore console, CLI, or SDKs.

Note

The features that are supported vary based on the method that you use to query time series data.

Before you query time series data, make sure that the following preparations are made:

  • You have obtained the information about the identifier of the time series whose data you want to query. If you are not sure about the identifier information, you need to retrieve the time series to obtain the information. For more information, see Manage time series metadata.

  • If you query time series data by using Tablestore SDKs, an OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.

  • If you query time series data by using the Tablestore CLI, the Tablestore CLI is downloaded and started, and the information about the instance that you want to access is configured. For more information, see Download the Tablestore CLI and Start the Tablestore CLI and configure access information.

Use the Tablestore console

You can use the Tablestore console to query the time series data in a time series within a specific time period.

  1. Go to the Instance Management page.

    1. Log on to the Tablestore console.

    2. In the top navigation bar, select a resource group and a region.

    3. On the Overview page, click the name of the instance that you want to manage or click Manage Instance in the Actions column of the instance.

  2. In the lower part of the Instance Details tab, click the Time Series Tables tab.

  3. On the Time Series Tables tab, click the name of the time series table that you want to manage, and then click the Query Data tab. You can also click Manage Data in the Actions column of the time series table.

  4. On the Query Data tab, find the time series that you want to manage and click Query Data in the Actions column.

  5. Select Time Range or Microsecond Timestamp from the Search Method drop-down list, specify the time, and then click Search.

    The data that meets the conditions is displayed on the Query Data tab. The query results can be displayed in a list or figure.

    Example of displaying results in a list

    fig_timeseriesdataquery

    Example of displaying results in a figure

    Note

    Different colors in the figure represent different data columns. If you move the pointer over the trend chart, the values of the corresponding data columns at a specific point in time are displayed. You can also select or clear specific data columns to display the required data columns.

    fig_picdisplay

Use the Tablestore CLI

You can use the Tablestore CLI to query the time series data in a time series within a specific time period.

Run the getts command to query the time series data in a time series within a specific time period. For more information, see the Query time series data section of the Operations on time series data topic.

The following sample code is used to query time series data that is generated before 1667638230000000 in the time series whose metric name is cpu, data source is localhost, and tags are "region=hangzhou" and "os=ubuntu". In addition, the maximum number of returned data entries is set to 100.

getts --k '["cpu","localhost",["region=hangzhou","os=ubuntu"]]' --time_start 0 --time_end 1667638230000000 --limit 100

Use Tablestore SDKs

You can use Tablestore SDK for Java and Tablestore SDK for Go to query the time series data in a time series within a specific time period. In this example, Tablestore SDK for Java is used.

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

FAQ

References