有一個您正在尋找的Java API。
參見下面的代碼(source):
// Create a config object with a path to the file for parsing. Or manually
// override settings.
// e.g. config.overrideConfig("tsd.storage.hbase.zk_quorum", "localhost");
final Config config = new Config("path/to/opentsdb.conf");
final TSDB tsdb = new TSDB(config);
// main query
final TSQuery query = new TSQuery();
// use any string format from
//http://opentsdb.net/docs/build/html/user_guide/query/dates.html
query.setStart("28d-ago");
// Optional: set other global query params
query.setEnd("26d-ago");
// at least one sub query required. This is where you specify the metric and tags
final TSSubQuery sub_query = new TSSubQuery();
sub_query.setMetric("sys_cpu");
// tags are optional but you can create and populate a map
final HashMap<String, String> tags = new HashMap<String, String>(1);
tags.put("node", "Test_100");
sub_query.setTags(tags);
// To apply downsampling of data <time interval>-<function>
sub_query.setDownsample("10m-avg");
// you do have to set an aggregator. Just provide the name as a string
sub_query.setAggregator("avg");
// IMPORTANT: don't forget to add the sub_query
final ArrayList<TSSubQuery> sub_queries = new ArrayList<TSSubQuery>(1);
sub_queries.add(sub_query);
query.setQueries(sub_queries);
// make sure the query is valid. This will throw exceptions if something
// is missing
query.validateAndSetQuery();
// compile the queries into TsdbQuery objects behind the scenes
Query[] tsdbqueries = query.buildQueries(tsdb);
// create some arrays for storing the results and the async calls
final int numberOfQueries = tsdbqueries.length;
final ArrayList<DataPoints[]> results = new ArrayList<DataPoints[]>(numberOfQueries);
final ArrayList<Deferred<DataPoints[]>> deferreds = new ArrayList<Deferred<DataPoints[]>>(numberOfQueries);
// this executes each of the sub queries asynchronously and puts the
// deferred in an array so we can wait for them to complete.
for (int i = 0; i < numberOfQueries; i++) {
deferreds.add(tsdbqueries[i].runAsync());
}
// This is a required callback class to store the results after each
// query has finished
class QueriesCB implements Callback<Object, ArrayList<DataPoints[]>> {
public Object call(final ArrayList<DataPoints[]> query_results) throws Exception {
results.addAll(query_results);
return null;
}
}
// this will cause the calling thread to wait until ALL of the queries
// have completed.
Deferred.groupInOrder(deferreds).addCallback(new QueriesCB()).joinUninterruptibly();
// now all of the results are in so we just iterate over each set of
// results and do any processing necessary.
for (final DataPoints[] data_sets : results) {
for (final DataPoints data : data_sets) {
System.out.print(data.metricName());
Map<String, String> resolved_tags = data.getTags();
for (final Map.Entry<String, String> pair : resolved_tags.entrySet()) {
System.out.print(" " + pair.getKey() + "=" + pair.getValue());
}
System.out.print("\n");
final SeekableView it = data.iterator();
while (it.hasNext()) {
final DataPoint dp = it.next();
System.out.println(" " + dp.timestamp() + " " + (dp.isInteger() ? dp.longValue() : dp.doubleValue()));
}
System.out.println("");
}
}
tsdb.shutdown();
來源
2016-05-05 07:49:38
rvd
感謝。您是否可以爲每個度量指定不同的標籤?含義metric1-> metric1的度量標籤,metric2-> metric2的標籤等。 – florins
是的,每個度量標準可以有不同的標籤。每個指標都有自己的sub_query,您可以爲每個sub_query設置不同的標籤。您可以忽略每個變量的最終關鍵字。 – rvd