To query data, first create a Query object. Query objects represents query conditions, which are used to specify tags, metrics, and aggregation query conditions that meet criteria.
Build query conditions
Sample code
Create a Query object, or the query condition:
Query query = Query
.timeRange(startTime, endTime) // Set up the query time condition
.sub(SubQuery.metric("hello").aggregator(Aggregator.AVG).tag("tagk1", "tagv1").build()) // Set up SubQuery
.sub(SubQuery.metric("world").aggregator(Aggregator.SUM).tag("tagk2", "tagv2").build()) // Set up SubQuery
.build();
SubQuery
For every Query object, you can set up multiple SubQuery to query on multiple conditions.
Sample code
This is the architecture of a typical SubQuery:
SubQuery subQuery = SubQuery
.metric("test-metric")
.aggregator(Aggregator.AVG)
.downsample("60m-avg")
.tag("tagk1", "tagv1")
.tag("tagk2", "tagv2")
.build();
Where SubQuery represents a SubQuery object and Aggregator is an enumeration type.
Synchronous query
HiTSDB SDK provides thread-synchronized data query.
Sample code
Use the Query method for synchronized data query:
List<QueryResult> result = tsdb.query(query);
System.out.println("Returned results:" + result);
Asynchronous query
HiTSDB SDK provides thread-synchronized data query.
Sample code
Use the Query method for synchronized data query, and set up the behaviors after asynchronous query by setting up the callback:
QueryCallback cb = new QueryCallback() {
@Override
public void response(Query input, List<QueryResult> result) {
System.out.println("Query parameters:" + input);
System.out.println("Returned results:" + result);
}
};
tsdb.query(query, cb);
Query results
Queried data by the Query method is returned in the form List<QueryResult>
, where QueryResult
is the result of each SubQuery. You can use the getDps()
method of QueryResult
to get queried data.