Search in sources :

Example 11 with AggregateRequest

use of org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateRequest in project hbase by apache.

the class AsyncAggregationClient method min.

public static <R, S, P extends Message, Q extends Message, T extends Message> CompletableFuture<R> min(RawAsyncTable table, ColumnInterpreter<R, S, P, Q, T> ci, Scan scan) {
    CompletableFuture<R> future = new CompletableFuture<>();
    AggregateRequest req;
    try {
        req = validateArgAndGetPB(scan, ci, false);
    } catch (IOException e) {
        future.completeExceptionally(e);
        return future;
    }
    AbstractAggregationCallback<R> callback = new AbstractAggregationCallback<R>(future) {

        private R min;

        @Override
        protected void aggregate(HRegionInfo region, AggregateResponse resp) throws IOException {
            if (resp.getFirstPartCount() > 0) {
                R result = getCellValueFromProto(ci, resp, 0);
                if (min == null || (result != null && ci.compare(min, result) > 0)) {
                    min = result;
                }
            }
        }

        @Override
        protected R getFinalResult() {
            return min;
        }
    };
    table.coprocessorService(channel -> AggregateService.newStub(channel), (stub, controller, rpcCallback) -> stub.getMin(controller, req, rpcCallback), scan.getStartRow(), scan.includeStartRow(), scan.getStopRow(), scan.includeStopRow(), callback);
    return future;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) CompletableFuture(java.util.concurrent.CompletableFuture) AggregateRequest(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateRequest) AggregateResponse(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateResponse) IOException(java.io.IOException)

Example 12 with AggregateRequest

use of org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateRequest in project hbase by apache.

the class AsyncAggregationClient method rowCount.

public static <R, S, P extends Message, Q extends Message, T extends Message> CompletableFuture<Long> rowCount(RawAsyncTable table, ColumnInterpreter<R, S, P, Q, T> ci, Scan scan) {
    CompletableFuture<Long> future = new CompletableFuture<>();
    AggregateRequest req;
    try {
        req = validateArgAndGetPB(scan, ci, true);
    } catch (IOException e) {
        future.completeExceptionally(e);
        return future;
    }
    AbstractAggregationCallback<Long> callback = new AbstractAggregationCallback<Long>(future) {

        private long count;

        @Override
        protected void aggregate(HRegionInfo region, AggregateResponse resp) throws IOException {
            count += resp.getFirstPart(0).asReadOnlyByteBuffer().getLong();
        }

        @Override
        protected Long getFinalResult() {
            return count;
        }
    };
    table.coprocessorService(channel -> AggregateService.newStub(channel), (stub, controller, rpcCallback) -> stub.getRowNum(controller, req, rpcCallback), scan.getStartRow(), scan.includeStartRow(), scan.getStopRow(), scan.includeStopRow(), callback);
    return future;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) CompletableFuture(java.util.concurrent.CompletableFuture) AggregateRequest(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateRequest) AggregateResponse(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateResponse) IOException(java.io.IOException)

Example 13 with AggregateRequest

use of org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateRequest in project hbase by apache.

the class AsyncAggregationClient method sum.

public static <R, S, P extends Message, Q extends Message, T extends Message> CompletableFuture<S> sum(RawAsyncTable table, ColumnInterpreter<R, S, P, Q, T> ci, Scan scan) {
    CompletableFuture<S> future = new CompletableFuture<>();
    AggregateRequest req;
    try {
        req = validateArgAndGetPB(scan, ci, false);
    } catch (IOException e) {
        future.completeExceptionally(e);
        return future;
    }
    AbstractAggregationCallback<S> callback = new AbstractAggregationCallback<S>(future) {

        private S sum;

        @Override
        protected void aggregate(HRegionInfo region, AggregateResponse resp) throws IOException {
            if (resp.getFirstPartCount() > 0) {
                S s = getPromotedValueFromProto(ci, resp, 0);
                sum = ci.add(sum, s);
            }
        }

        @Override
        protected S getFinalResult() {
            return sum;
        }
    };
    table.coprocessorService(channel -> AggregateService.newStub(channel), (stub, controller, rpcCallback) -> stub.getSum(controller, req, rpcCallback), scan.getStartRow(), scan.includeStartRow(), scan.getStopRow(), scan.includeStopRow(), callback);
    return future;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) CompletableFuture(java.util.concurrent.CompletableFuture) AggregateRequest(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateRequest) AggregateResponse(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateResponse) IOException(java.io.IOException)

Example 14 with AggregateRequest

use of org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateRequest in project hbase by apache.

the class AggregationClient method getMedianArgs.

/**
   * It helps locate the region with median for a given column whose weight
   * is specified in an optional column.
   * From individual regions, it obtains sum of values and sum of weights.
   * @param table
   * @param ci
   * @param scan
   * @return pair whose first element is a map between start row of the region
   *  and (sum of values, sum of weights) for the region, the second element is
   *  (sum of values, sum of weights) for all the regions chosen
   * @throws Throwable
   */
private <R, S, P extends Message, Q extends Message, T extends Message> Pair<NavigableMap<byte[], List<S>>, List<S>> getMedianArgs(final Table table, final ColumnInterpreter<R, S, P, Q, T> ci, final Scan scan) throws Throwable {
    final AggregateRequest requestArg = validateArgAndGetPB(scan, ci, false);
    final NavigableMap<byte[], List<S>> map = new TreeMap<>(Bytes.BYTES_COMPARATOR);
    class StdCallback implements Batch.Callback<List<S>> {

        S sumVal = null, sumWeights = null;

        public synchronized Pair<NavigableMap<byte[], List<S>>, List<S>> getMedianParams() {
            List<S> l = new ArrayList<>(2);
            l.add(sumVal);
            l.add(sumWeights);
            Pair<NavigableMap<byte[], List<S>>, List<S>> p = new Pair<>(map, l);
            return p;
        }

        @Override
        public synchronized void update(byte[] region, byte[] row, List<S> result) {
            map.put(row, result);
            sumVal = ci.add(sumVal, result.get(0));
            sumWeights = ci.add(sumWeights, result.get(1));
        }
    }
    StdCallback stdCallback = new StdCallback();
    table.coprocessorService(AggregateService.class, scan.getStartRow(), scan.getStopRow(), new Batch.Call<AggregateService, List<S>>() {

        @Override
        public List<S> call(AggregateService instance) throws IOException {
            RpcController controller = new AggregationClientRpcController();
            CoprocessorRpcUtils.BlockingRpcCallback<AggregateResponse> rpcCallback = new CoprocessorRpcUtils.BlockingRpcCallback<>();
            instance.getMedian(controller, requestArg, rpcCallback);
            AggregateResponse response = rpcCallback.get();
            if (controller.failed()) {
                throw new IOException(controller.errorText());
            }
            List<S> list = new ArrayList<>();
            for (int i = 0; i < response.getFirstPartCount(); i++) {
                ByteString b = response.getFirstPart(i);
                T t = getParsedGenericInstance(ci.getClass(), 4, b);
                S s = ci.getPromotedValueFromProto(t);
                list.add(s);
            }
            return list;
        }
    }, stdCallback);
    return stdCallback.getMedianParams();
}
Also used : AggregateRequest(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateRequest) NavigableMap(java.util.NavigableMap) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) AggregateResponse(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateResponse) ArrayList(java.util.ArrayList) List(java.util.List) Pair(org.apache.hadoop.hbase.util.Pair) IOException(java.io.IOException) TreeMap(java.util.TreeMap) RpcController(com.google.protobuf.RpcController) RpcCallback(com.google.protobuf.RpcCallback) CoprocessorRpcUtils(org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils) AggregateService(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateService)

Aggregations

IOException (java.io.IOException)14 AggregateRequest (org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateRequest)14 AggregateResponse (org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateResponse)14 RpcCallback (com.google.protobuf.RpcCallback)7 RpcController (com.google.protobuf.RpcController)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)7 CoprocessorRpcUtils (org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils)7 AggregateService (org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateService)7 ByteString (com.google.protobuf.ByteString)6 ByteBuffer (java.nio.ByteBuffer)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Pair (org.apache.hadoop.hbase.util.Pair)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 NavigableMap (java.util.NavigableMap)2 TreeMap (java.util.TreeMap)1