Executing the Query

The server computes the metric results based on the facts published. You can query the server about the computed metric results using the Query API. The steps help you create and execute a query and browse through the results.

Procedure

  1. Define a Query Object.

    The code snippets defines query object is:

    final Query query = session.createQuery();
    QueryByFilterDef queryDef = query.newQueryByFilterDef(
                                SCHEMA_NAME, CUBE_NAME,
                                DIM_HIERARCHY_NAME,
                                MEASUREMENT_NAME);
    queryDef.setName("SnapshotQueryDef-Gt-filter");
    queryDef.setBatchSize(5);
  2. Set the type of query you are using. The valid types are Snapshot and Streaming. The snapshot queries help you evaluate the metric results at a given point-in-time, whereas the streaming queries are executed continuously.
    • queryDef.setQueryType(QueryType.SNAPSHOT);
    • queryDef.setQueryType(QueryType.STREAMING);
  3. Define a filter.

    The filter is similar to the WHERE clause in a standard SQL query. You can add various filter conditions using the filters that are available.

    Filter eqFilter = QueryFactory.INSTANCE.newEqFilter(
                      MetricQualifier.DIMENSION_LEVEL, 
                      DIM_LEVEL_SERVICE);
    Filter gtFilter = QueryFactory.INSTANCE.newGtFilter(
                      FilterKeyQualifier.MEASUREMENT_NAME, 
                      MEASUREMENT_HITCOUNT, 5.0);
    AndFilter andFilter = QueryFactory.INSTANCE.newAndFilter();
    andFilter.addFilter(eqFilter, gtFilter);
    queryDef.setFilter(andFilter);
  4. After setting the filter, you can execute the query. In case of streaming queries, it is recommended to execute the query in a separate thread.
    Browser<MetricResultTuple> browser = query.execute();

    You can get two types of query results: Streaming query results and snapshot query results.

  5. To get Streaming Query results, you must implement QueryResultHandler and register it with the query.
    1. Implementing Query ResultHandler
      private class MyQueryResultHandler implements QueryResultHandler {
      @Override
          public void onData(QueryResultTuple queryResultTuple) {            
          MetricResultTuple rs = 
          queryResultTuple.getMetricResultTuple();
          if (rs != null) {
            for (String metricName : rs.getMetricNames()) {
            SingleValueMetric metric = 
            (SingleValueMetric) rs.getMetric(metricName); 
            if (metric != null) {
              String measurementName = 
              metric.getDescriptor().getMeasurementName();
              MetricKey key = (MetricKey) metric.getKey();
              System.out.println(String.format("%s: Level = %s", 
              queryResultTuple.getQueryName(), 
              key.getDimensionLevelName()));
              for (String dimName : key.getDimensionNames()) {
                System.out.println(String.format("%s: Dimension name = 
                %s, value = %s", pad, dimName, 
                key.getDimensionValue(dimName)));
              }
              System.out.println(String.format("%s: Metric = %s, 
              value = %s", pad, 
              metric.getDescriptor().getMeasurementName(),
              metric.getValue()));
              System.out.println();
            }
           }
          }
         }            
      @Override
          public void onError(Object errorContext) {
            System.out.println("OnError...");
          }
      }
    2. Registering QueryResultHandler with the Query
      query.setResultHandler(new MyQueryResultHandler());
  6. To get snapshot query results, browse the resultset from the Browser object returned after executing the query.
    while (browser.hasNext()) {
        MetricResultTuple rs = browser.next();
        SingleValueMetric<Long> metric = 
        (SingleValueMetric<Long>) rs.getMetric("HitCount");
        MetricKey key = (MetricKey) metric.getKey();
        System.out.println(String.format(" Level = %s", 
        key.getDimensionLevelName()));
        for (String dimName : key.getDimensionNames()) {
          System.out.println(String.format("Dimension name = %s,
          value = %s", dimName, key.getDimensionValue(dimName)));
        }
        System.out.println(String.format(" Metric = %s,value = %s", 
        metric.getDescriptor().getMeasurementName(), 
        metric.getValue()));
        System.out.println();
    }