All Products
Search
Document Center

Tablestore:Query time series data

Last Updated:Dec 20, 2024

You can query the time series data in a time series within a specific time range. If you are not sure about the information about the time series in which you want to query time series data, such as the metric name and data source, you can query the time series by specifying conditions, and then query time series data in the time series.

Overview

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

When you query time series data in a time series, you must specify the information about the time series and the query conditions based on your business requirements.

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

  • You can specify the data columns that you want to return. By default, all data columns are returned.

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

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

To query time series data, you must specify the identifiers of the time series in which you want to query time series data.

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, find the time series table that you want to manage and click Query in the Actions column.

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

  5. Specify query conditions.

    1. By default, the query results are sorted in chronological order. To sort query results in reverse chronological order, set the Reverse Chronological Oder parameter to Yes.

    2. Select a search method and specify a time range.

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

    Display query results in a list

    image

    Display query results in a figure

    image

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 Query time series data.

The following sample code provides an example on how to query time series data that is generated within the time range from 0 to 1667638230000000 in the time series whose metric name is cpu, data source is localhost, and tags are "region=hangzhou" and "os=ubuntu". In this example, the query results are sorted in chronological order and up to 100 pieces of time series data are returned.

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

FAQ

References