use of net.opentsdb.query.filter.TagVFilter in project opentsdb by OpenTSDB.
the class QueryExample method main.
public static void main(final String[] args) throws IOException {
// Set these as arguments so you don't have to keep path information in
// source files
String pathToConfigFile = (args != null && args.length > 0 ? args[0] : null);
// 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;
if (pathToConfigFile != null && !pathToConfigFile.isEmpty()) {
config = new Config(pathToConfigFile);
} else {
// Search for a default config from /etc/opentsdb/opentsdb.conf, etc.
config = new Config(true);
}
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("1h-ago");
// Optional: set other global query params
// at least one sub query required. This is where you specify the metric and
// tags
final TSSubQuery subQuery = new TSSubQuery();
subQuery.setMetric("my.tsdb.test.metric");
// filters are optional but useful.
final List<TagVFilter> filters = new ArrayList<TagVFilter>(1);
filters.add(new TagVFilter.Builder().setType("literal_or").setFilter("example1").setTagk("script").setGroupBy(true).build());
subQuery.setFilters(filters);
// you do have to set an aggregator. Just provide the name as a string
subQuery.setAggregator("sum");
// IMPORTANT: don't forget to add the subQuery
final ArrayList<TSSubQuery> subQueries = new ArrayList<TSSubQuery>(1);
subQueries.add(subQuery);
query.setQueries(subQueries);
// otherwise we aggregate on the second.
query.setMsResolution(true);
// 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 nqueries = tsdbqueries.length;
final ArrayList<DataPoints[]> results = new ArrayList<DataPoints[]>(nqueries);
final ArrayList<Deferred<DataPoints[]>> deferreds = new ArrayList<Deferred<DataPoints[]>>(nqueries);
// deferred in an array so we can wait for them to complete.
for (int i = 0; i < nqueries; i++) {
deferreds.add(tsdbqueries[i].runAsync());
}
// Start timer
long startTime = DateTime.nanoTime();
// query has finished
class QueriesCB implements Callback<Object, ArrayList<DataPoints[]>> {
public Object call(final ArrayList<DataPoints[]> queryResults) throws Exception {
results.addAll(queryResults);
return null;
}
}
// Make sure to handle any errors that might crop up
class QueriesEB implements Callback<Object, Exception> {
@Override
public Object call(final Exception e) throws Exception {
System.err.println("Queries failed");
e.printStackTrace();
return null;
}
}
// have completed.
try {
Deferred.groupInOrder(deferreds).addCallback(new QueriesCB()).addErrback(new QueriesEB()).join();
} catch (Exception e) {
e.printStackTrace();
}
// End timer.
double elapsedTime = DateTime.msFromNanoDiff(DateTime.nanoTime(), startTime);
System.out.println("Query returned in: " + elapsedTime + " milliseconds.");
// results and do any processing necessary.
for (final DataPoints[] dataSets : results) {
for (final DataPoints data : dataSets) {
System.out.print(data.metricName());
Map<String, String> resolvedTags = data.getTags();
for (final Map.Entry<String, String> pair : resolvedTags.entrySet()) {
System.out.print(" " + pair.getKey() + "=" + pair.getValue());
}
System.out.print("\n");
final SeekableView it = data.iterator();
/*
* An important point about SeekableView:
* Because no data is copied during iteration and no new object gets
* created, the DataPoint returned must not be stored and gets
* invalidated as soon as next is called on the iterator (actually it
* doesn't get invalidated but rather its contents changes). If you want
* to store individual data points, you need to copy the timestamp and
* value out of each DataPoint into your own data structures.
*
* In the vast majority of cases, the iterator will be used to go once
* through all the data points, which is why it's not a problem if the
* iterator acts just as a transient "view". Iterating will be very
* cheap since no memory allocation is required (except to instantiate
* the actual iterator at the beginning).
*/
while (it.hasNext()) {
final DataPoint dp = it.next();
System.out.println(" " + dp.timestamp() + " " + (dp.isInteger() ? dp.longValue() : dp.doubleValue()));
}
System.out.println("");
}
}
// Gracefully shutdown connection to TSDB
try {
tsdb.shutdown().join();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
use of net.opentsdb.query.filter.TagVFilter in project opentsdb by OpenTSDB.
the class TestTSSubQuery method validateWithFilterAndGroupByFilter.
@Test
public void validateWithFilterAndGroupByFilter() {
TSSubQuery sub = getMetricForValidate();
final List<TagVFilter> filters = new ArrayList<TagVFilter>(1);
filters.add(new TagVWildcardFilter("colo", "lga*"));
sub.setFilters(filters);
Map<String, String> tags = new HashMap<String, String>();
tags.put("host", TagVWildcardFilter.FILTER_NAME + "(*nari)");
sub.setTags(tags);
sub.validateAndSetQuery();
assertEquals("sys.cpu.0", sub.getMetric());
assertEquals(TagVWildcardFilter.FILTER_NAME + "(*nari)", sub.getTags().get("host"));
assertEquals(1, sub.getFilters().size());
assertEquals(Aggregators.SUM, sub.aggregator());
assertEquals(Aggregators.AVG, sub.downsampler());
assertEquals(300000, sub.downsampleInterval());
}
use of net.opentsdb.query.filter.TagVFilter in project opentsdb by OpenTSDB.
the class TestFilter method deserialize.
@Test
public void deserialize() throws Exception {
String json = "{\"id\":\"f1\",\"tags\":[{\"tagk\":\"host\"," + "\"filter\":\"*\",\"type\":\"iwildcard\",\"groupBy\":false}]," + "\"explicitTags\":\"true\"}";
TagVFilter tag = new TagVFilter.Builder().setFilter("*").setGroupBy(false).setTagk("host").setType("iwildcard").build();
Filter expectedFilter = Filter.Builder().setId("f1").setTags(Arrays.asList(tag)).setExplicitTags(true).build();
Filter filter = JSON.parseToObject(json, Filter.class);
filter.validate();
assertEquals(expectedFilter, filter);
}
use of net.opentsdb.query.filter.TagVFilter in project opentsdb by OpenTSDB.
the class TestFilter method serialize.
@Test
public void serialize() throws Exception {
TagVFilter tag = new TagVFilter.Builder().setFilter("*").setGroupBy(false).setTagk("host").setType("iwildcard").build();
Filter filter = Filter.Builder().setId("f1").setTags(Arrays.asList(tag)).setExplicitTags(true).build();
String actual = JSON.serializeToString(filter);
assertTrue(actual.contains("\"id\":\"f1\""));
assertTrue(actual.contains("\"tags\":["));
assertTrue(actual.contains("\"tagk\":\"host\""));
assertTrue(actual.contains("\"explicitTags\":true"));
}
use of net.opentsdb.query.filter.TagVFilter in project opentsdb by OpenTSDB.
the class TestTsdbQuery method configureFromQueryWithGroupByAndRegularFilters.
@Test
public void configureFromQueryWithGroupByAndRegularFilters() throws Exception {
setDataPointStorage();
final TSQuery ts_query = getTSQuery();
final List<TagVFilter> filters = new ArrayList<TagVFilter>(1);
filters.add(new TagVWildcardFilter("host", "*imes"));
filters.add(TagVFilter.Builder().setFilter("*").setTagk("host").setType("wildcard").setGroupBy(true).build());
ts_query.getQueries().get(0).setFilters(filters);
ts_query.validateAndSetQuery();
query = new TsdbQuery(tsdb);
query.configureFromQuery(ts_query, 0).joinUninterruptibly();
assertArrayEquals(METRIC_BYTES, ForTesting.getMetric(query));
assertEquals(2, ForTesting.getFilters(query).size());
assertEquals(1, ForTesting.getGroupBys(query).size());
assertArrayEquals(TAGK_BYTES, ForTesting.getGroupBys(query).get(0));
assertEquals(1, ForTesting.getRowKeyLiterals(query).size());
assertNull(ForTesting.getRowKeyLiterals(query).get(TAGK_BYTES));
assertNotNull(ForTesting.getRateOptions(query));
}
Aggregations