OpenTsdb通过Java API批量查询

时间:2015-10-23 13:50:50

标签: opentsdb

我想知道是否有任何JAVA API用于查询指定的指标列表的OpenTSDB? 例如,如果我有List = {metric1,metric2,metric3},则返回的值应该类似于{metric1> list},{metric2> list},{metric3>名单}。 我HBase有类似的https://hbase.apache.org/devapidocs/org/apache/hadoop/hbase/client/HTable.html#get(java.util.List)

public Result[] get(List<Get> gets)
             throws IOException
Extracts certain cells from the given rows, in batch

我期待着你的回答。 谢谢。  林

1 个答案:

答案 0 :(得分:0)

您正在寻找的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();