private <R, S, P extends Message, Q extends Message, T extends Message> Pair<S, Long>
    getAvgArgs(final Table table, final ColumnInterpreter<R, S, P, Q, T> ci, final Scan scan)
      throws Throwable {
    final AggregateRequest requestArg = validateArgAndGetPB(scan, ci, false, false);
    class AvgCallBack implements Batch.Callback<Pair<S, Long>> {
      S sum = null;
      Long rowCount = 0L;

      public synchronized Pair<S, Long> getAvgArgs() {
        return new Pair<>(sum, rowCount);
      }

      @Override
      public synchronized void update(byte[] region, byte[] row, Pair<S, Long> result) {
        sum = ci.add(sum, result.getFirst());
        rowCount += result.getSecond();
      }
    }

    AvgCallBack avgCallBack = new AvgCallBack();
    table.coprocessorService(AggregateService.class, scan.getStartRow(), scan.getStopRow(),
      new Batch.Call<AggregateService, Pair<S, Long>>() {
        @Override
        public Pair<S, Long> call(AggregateService instance) throws IOException {
          RpcController controller = new AggregationClientRpcController();
          CoprocessorRpcUtils.BlockingRpcCallback<AggregateResponse> rpcCallback =
            new CoprocessorRpcUtils.BlockingRpcCallback<>();
          instance.getAvg(controller, requestArg, rpcCallback);
          AggregateResponse response = rpcCallback.get();
          if (controller.failed()) {
            throw new IOException(controller.errorText());
          }
          Pair<S, Long> pair = new Pair<>(null, 0L);
          if (response.getFirstPartCount() == 0) {
            return pair;
          }
          ByteString b = response.getFirstPart(0);
          T t = getParsedGenericInstance(ci.getClass(), 4, b);
          S s = ci.getPromotedValueFromProto(t);
          pair.setFirst(s);
          ByteBuffer bb =
            ByteBuffer.allocate(8).put(getBytesFromResponse(response.getSecondPart()));
          bb.rewind();
          pair.setSecond(bb.getLong());
          return pair;
        }
      }, avgCallBack);
    return avgCallBack.getAvgArgs();
  }
It computes average while fetching sum and row count from all the corresponding regions.
Approach is to compute a global sum of region level sum and rowcount and then compute the
average.
@param table table to scan.
@param scan  the HBase scan object to use to read data from HBase
@throws Throwable The caller is supposed to handle the exception as they are thrown &amp;
                  propagated to it.
hbase-endpoint/src/main/java/org/apache/hadoop/hbase/client/coprocessor/AggregationClient.java