All Products
Search
Document Center

Tablestore:Query time series

Last Updated:Feb 04, 2024

If you want to query the information about time series, such as the metric name and data source, or you want to list time series that meet specified conditions, you can call the QueryTimeseriesMeta operation. This operation allows you to query time series by specific conditions such as metric name, data source, tag, attribute, and update time.

Note

For more information, see QueryTimeseriesMeta.

Prerequisites

  • Time series data is written to the time series table from which you want to query data. For more information, see Write time series data.

  • A TimeseriesClient instance is initialized. For more information, see Initialize a client.

Class definition

public class QueryTimeseriesMetaRequest implements Request {
    /** The name of the time series table. */
    private final String timeseriesTableName;
    /** The query conditions.*/
    private MetaQueryCondition condition;
    /** Specifies whether to return the total number of time series that meet the query conditions. */
    private boolean getTotalHits = false;
    /** The number of time series to return at a time. */
    private int limit = -1;
    /** The token that is used in the next request to query time series. */
    private byte[] nextToken;
}

The following classes define query conditions.

CompositeMetaQueryCondition

public class CompositeMetaQueryCondition implements MetaQueryCondition {
    /** The logical operator. */
    private final MetaQueryCompositeOperator operator;
    /** The list of subconditions. */
    private List<MetaQueryCondition> subConditions = new ArrayList<MetaQueryCondition>();
}

MeasurementMetaQueryCondition

public class MeasurementMetaQueryCondition implements MetaQueryCondition {
    /** The relational operator. */
    private final MetaQuerySingleOperator operator;
    /** The name of the metric of the time series that you want to query. */
    private final String value;
}

DataSourceMetaQueryCondition

public class DataSourceMetaQueryCondition implements MetaQueryCondition {
    /** The relational operator. */
    private final MetaQuerySingleOperator operator;
     /** The name of the data source of the time series that you want to query. */
    private final String value;
}

TagMetaQueryCondition

public class TagMetaQueryCondition implements MetaQueryCondition {
    /** The relational operator. */
    private final MetaQuerySingleOperator operator;
    /** The tag name. */
    private final String tagName;
    /** The tag value. */
    private final String value;
}

AttributeMetaQueryCondition

public class AttributeMetaQueryCondition implements MetaQueryCondition {
    /** The relational operator. */
    private final MetaQuerySingleOperator operator;
    /** The attribute name. */
    private final String attributeName;
    /** The attribute value. */
    private final String value;
}

UpdateTimeMetaQueryCondition

public class UpdateTimeMetaQueryCondition implements MetaQueryCondition {
    /** The relational operator. */
    private final MetaQuerySingleOperator operator;
    /** The timestamp when the time series metadata is updated. */
    private final long timeInUs;
}

Parameters

To query time series, you must specify the timeseriesTableName and condition parameters. The following types of conditions are supported: compositeMetaQueryCondition, measurementMetaQueryCondition, dataSourceMetaQueryCondition, tagMetaQueryCondition, attributeMetaQueryCondition, and updateTimeMetaQueryCondition. The following table describes the conditions.

Condition

Description

compositeMetaQueryCondition

The composite condition that includes the following content:

  • operator: the logical operator. Valid values: AND, OR, and NOT.

  • subConditions: the subconditions that can be combined by using operators for complex queries.

measurementMetaQueryCondition

The metric name condition that includes the following content:

  • operator: the relational operator or the prefix match condition. Valid values: =, !=, >, >=, <, and <=.

  • value: the name of the metric of the time series that you want to query. Type: STRING.

dataSourceMetaQueryCondition

The data source condition that includes the following content:

  • operator: the relational operator or the prefix match condition. Valid values: =, !=, >, >=, <, and <=.

  • value: the data source of the time series that you want to query. Type: STRING.

tagMetaQueryCondition

The tag condition that includes the following content:

  • operator: the relational operator or the prefix match condition. Valid values: =, !=, >, >=, <, and <=.

  • tagName: the name of the tag of the time series that you want to query. Type: STRING.

  • value: the value of the tag of the time series that you want to query. Type: STRING.

attributeMetaQueryCondition

The attribute condition for the metadata of the time series. The attribute condition includes the following content:

  • operator: the relational operator or the prefix match condition. Valid values: =, !=, >, >=, <, and <=.

  • attributeName: the name of the attribute. Type: STRING.

  • value: the value of the attribute. Type: STRING.

updateTimeMetaQueryCondition

The update time condition for the metadata of the time series. The update time condition includes the following content:

  • operator: the relational operator. Valid values: =, !=, >, >=, <, and <=.

  • timeInUs: the timestamp when the time series metadata was updated. Unit: microseconds.

Example

The following sample code provides an example on how to query all time series whose metric name is cpu and that have the os tag whose value is prefixed with Ubuntu in a time series table.

private static void queryTimeseriesMeta(TimeseriesClient client) {
    // Specify the name of the time series table. 
    String tableName = "<TIME_SERIES_TABLE>";
    QueryTimeseriesMetaRequest queryTimeseriesMetaRequest = new QueryTimeseriesMetaRequest(tableName);
    // Query all time series whose metric name is cpu and that have the os tag whose value is prefixed with Ubuntu. measurement_name="cpu" and have_prefix(os, "Ubuntu") 
    CompositeMetaQueryCondition compositeMetaQueryCondition = new CompositeMetaQueryCondition(MetaQueryCompositeOperator.OP_AND);
    compositeMetaQueryCondition.addSubCondition(new MeasurementMetaQueryCondition(MetaQuerySingleOperator.OP_EQUAL, "cpu"));
    compositeMetaQueryCondition.addSubCondition(new TagMetaQueryCondition(MetaQuerySingleOperator.OP_PREFIX, "os", "Ubuntu"));
    queryTimeseriesMetaRequest.setCondition(compositeMetaQueryCondition);
    queryTimeseriesMetaRequest.setGetTotalHits(true);
    // Specify the maximum number of time series metadata entries that can be returned for a single request. 
    queryTimeseriesMetaRequest.setLimit(100);
    // Initiate the query. 
    QueryTimeseriesMetaResponse queryTimeseriesMetaResponse = client.queryTimeseriesMeta(queryTimeseriesMetaRequest);
    // Display the total number of time series that meet the query conditions. 
    System.out.println(queryTimeseriesMetaResponse.getTotalHits());

    // Save the request results. 
    List<TimeseriesMeta> timeseriesMetas = new ArrayList<TimeseriesMeta>();
    timeseriesMetas.addAll(queryTimeseriesMetaResponse.getTimeseriesMetas());

    // If NextToken is included in the response, you can initiate a new request to obtain the remaining results. 
    while (queryTimeseriesMetaResponse.getNextToken() != null) {
        queryTimeseriesMetaRequest.setNextToken(queryTimeseriesMetaResponse.getNextToken());
        queryTimeseriesMetaResponse = client.queryTimeseriesMeta(queryTimeseriesMetaRequest);
        timeseriesMetas.addAll(queryTimeseriesMetaResponse.getTimeseriesMetas());
        // Specify the maximum number of time series that can be returned. 
        if (timeseriesMetas.size() >= 1000) {
            break;
        }
    }

    System.out.println(timeseriesMetas.size());
    for (TimeseriesMeta timeseriesMeta : timeseriesMetas) {
        System.out.println(timeseriesMeta.getTimeseriesKey().getMeasurementName());
        System.out.println(timeseriesMeta.getTimeseriesKey().getDataSource());
        System.out.println(timeseriesMeta.getTimeseriesKey().getTags());
        System.out.println(timeseriesMeta.getAttributes());
        System.out.println(timeseriesMeta.getUpdateTimeInUs());
    }
}

What to do next

After you query time series, you can perform the following operations on the time series: