All Products
Search
Document Center

Time Series Database:Query data

Last Updated:Oct 20, 2022

Before you query data, build a Query object. A Query object contains query conditions. You can specify tags, metrics, and aggregated query conditions in a Query object. This way, you can query the data that meets the specified conditions.

Define query conditions

Sample code

Build a Query object that contains query conditions.

Query query = Query
    .timeRange(startTime, endTime)    // Specify the time range during which you want to query data.
    .sub(SubQuery.metric("hello").aggregator(Aggregator.AVG).tag("tagk1", "tagv1").build())    // Create a subquery.
    .sub(SubQuery.metric("world").aggregator(Aggregator.SUM).tag("tagk2", "tagv2").build())    // Create a subquery.
    .build();

Subqueries

You can create multiple SubQuery objects in a Query object. This way, you can create subqueries that contain multiple query conditions.

Sample code

Create a sample subquery

SubQuery subQuery = SubQuery
                .metric("test-metric")
                .aggregator(Aggregator.AVG)
                .downsample("60m-avg")
                .tag("tagk1", "tagv1")
                .tag("tagk2", "tagv2")
                .build();

SubQuery is an object in which you can define a subquery. Aggregator is an enumeration class.

Run queries in a synchronous manner

Lindorm Time Series Database (TSDB) SDK allows you to query data in thread synchronization mode.

Sample code

Use the query() method to query data in a synchronous manner.

List<QueryResult> result = tsdb.query(query);
System.out.println("Returned result" + result);

Run queries in an asynchronous manner

TSDB SDK allows you to query data in an asynchronous manner.

Sample code

Use the query() method to query data in an asynchronous manner and configure a callback to define how the query result is returned.

QueryCallback cb = new QueryCallback() {

    @Override
    public void response(Query input, List<QueryResult> result) {
        System.out.println("Query parameters:" + input);
        System.out.println("Returned result:" + result);
    }

};

tsdb.query(query, cb);

Query data and sort the query result in descending order

TSDB SDK allows you to query data and sort the query result in descending or ascending order. The getOrderDps(boolean ordeset) method can be used to sort query results based on timestamps. If you use getOrderDps(true) in a query, the returned query result is sorted based on timestamps in descending order. If you use getOrderDps() in a query, the returned query result is sorted based on timestamps in ascending order. Sample code

Query query = Query
                .timeRange(current - 1000,start + 1000)
                .sub(SubQuery.metric(metric).aggregator(Aggregator.NONE).tag(tags).build())
                .build();

        try {
            // Run a query and obtain the query result.
            List<QueryResult> result = tsdb.query(query);


            for(QueryResult queryResult : result){
                // Obtain unordered data points.
                System.out.println(queryResult.getDps());
                System.out.println("-------------");
                // Obtain a collection of data points sorted based on timestamps in ascending order.
                System.out.println(queryResult.getOrderDps());
                System.out.println("-------------");
                // Obtain a collection of data points sorted based on timestamps in descending order.
                System.out.println(queryResult.getOrderDps(true));
            }
        } catch (HttpUnknowStatusException e) {
            e.printStackTrace();
        }

Query result

If you use the query() method to query data, the query result is returned in List<QueryResult>. QueryResult indicates the query result of a subquery. You can use the getDps() method in QueryResult to obtain query results.

Query the latest data point

TSDB SDK allows you to query the latest data point before a specified timestamp. Sample code:

Example 1

Create a query that contains subqueries. The metrics and tags are specified in subqueries.

// Create tags.
        Map<String,String> tags = new HashMap<String, String>();
        tags.put("uid","1");
        tags.put("id","6");

        String metric = "test.1";
        // Create a query to return the latest data point.
        LastPointQuery query = LastPointQuery.builder()
                // Specify a timestamp.
                .timestamp(1537520409729l)
                // Specify whether to use a timestamp in milliseconds to query data.
                .msResolution(true)
                // Create a subquery to return the latest data point of a specified time series. 
                // You can create multiple subqueries at the same time.
                .sub(LastPointSubQuery.builder(metric,tags).build()).build();

        // Obtain the latest point of each time series. Each time series has a LastDataValue.
        List<LastDataValue> lastDataValues = tsdb.queryLast(query);

        System.out.println(lastDataValues);

Example 2

Specify the ID of a time series in a subquery to query data.

// Specify the IDs of time series.
        List<String> tsuids = new ArrayList<String>();
        tsuids.add("10000B7C000095000081****FF00006F");

        // Create a query to return the latest data point.
        LastPointQuery query = LastPointQuery.builder()
                // Specify a timestamp.
                .timestamp(1537520409729l)
                // Specify whether to use a timestamp in milliseconds to query data.
                .msResolution(true)
                // Create a subquery to return the latest data point of a specified time series. 
                // You can create multiple subqueries at the same time.
                .sub(LastPointSubQuery.builder(tsuids).build()).build();

        // Obtain the latest point of each time series. Each time series has a LastDataValue.
        List<LastDataValue> lastDataValues = tsdb.queryLast(query);

        System.out.println(lastDataValues);

The tsuids value can be obtained from the query result in Example 1. The first time you query data based on Example 1, you can save the tsuids value returned in the query result for subsequent use.