Search in sources :

Example 6 with AggregateRequest

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

the class AggregationClient method getStdArgs.

/**
   * It computes a global standard deviation for a given column and its value.
   * Standard deviation is square root of (average of squares -
   * average*average). From individual regions, it obtains sum, square sum and
   * number of rows. With these, the above values are computed to get the global
   * std.
   * @param table
   * @param scan
   * @return standard deviations
   * @throws Throwable
   */
private <R, S, P extends Message, Q extends Message, T extends Message> Pair<List<S>, Long> getStdArgs(final Table table, final ColumnInterpreter<R, S, P, Q, T> ci, final Scan scan) throws Throwable {
    final AggregateRequest requestArg = validateArgAndGetPB(scan, ci, false);
    class StdCallback implements Batch.Callback<Pair<List<S>, Long>> {

        long rowCountVal = 0l;

        S sumVal = null, sumSqVal = null;

        public synchronized Pair<List<S>, Long> getStdParams() {
            List<S> l = new ArrayList<>(2);
            l.add(sumVal);
            l.add(sumSqVal);
            Pair<List<S>, Long> p = new Pair<>(l, rowCountVal);
            return p;
        }

        @Override
        public synchronized void update(byte[] region, byte[] row, Pair<List<S>, Long> result) {
            if (result.getFirst().size() > 0) {
                sumVal = ci.add(sumVal, result.getFirst().get(0));
                sumSqVal = ci.add(sumSqVal, result.getFirst().get(1));
                rowCountVal += result.getSecond();
            }
        }
    }
    StdCallback stdCallback = new StdCallback();
    table.coprocessorService(AggregateService.class, scan.getStartRow(), scan.getStopRow(), new Batch.Call<AggregateService, Pair<List<S>, Long>>() {

        @Override
        public Pair<List<S>, Long> call(AggregateService instance) throws IOException {
            RpcController controller = new AggregationClientRpcController();
            CoprocessorRpcUtils.BlockingRpcCallback<AggregateResponse> rpcCallback = new CoprocessorRpcUtils.BlockingRpcCallback<>();
            instance.getStd(controller, requestArg, rpcCallback);
            AggregateResponse response = rpcCallback.get();
            if (controller.failed()) {
                throw new IOException(controller.errorText());
            }
            Pair<List<S>, Long> pair = new Pair<>(new ArrayList<>(), 0L);
            if (response.getFirstPartCount() == 0) {
                return pair;
            }
            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);
            }
            pair.setFirst(list);
            ByteBuffer bb = ByteBuffer.allocate(8).put(getBytesFromResponse(response.getSecondPart()));
            bb.rewind();
            pair.setSecond(bb.getLong());
            return pair;
        }
    }, stdCallback);
    return stdCallback.getStdParams();
}
Also used : AggregateRequest(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateRequest) 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) ByteBuffer(java.nio.ByteBuffer) RpcController(com.google.protobuf.RpcController) RpcCallback(com.google.protobuf.RpcCallback) CoprocessorRpcUtils(org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils) AtomicLong(java.util.concurrent.atomic.AtomicLong) AggregateService(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateService)

Example 7 with AggregateRequest

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

the class AggregationClient method sum.

/**
   * It sums up the value returned from various regions. In case qualifier is
   * null, summation of all the column qualifiers in the given family is done.
   * @param table
   * @param ci
   * @param scan
   * @return sum &lt;S&gt;
   * @throws Throwable
   */
public <R, S, P extends Message, Q extends Message, T extends Message> S sum(final Table table, final ColumnInterpreter<R, S, P, Q, T> ci, final Scan scan) throws Throwable {
    final AggregateRequest requestArg = validateArgAndGetPB(scan, ci, false);
    class SumCallBack implements Batch.Callback<S> {

        S sumVal = null;

        public S getSumResult() {
            return sumVal;
        }

        @Override
        public synchronized void update(byte[] region, byte[] row, S result) {
            sumVal = ci.add(sumVal, result);
        }
    }
    SumCallBack sumCallBack = new SumCallBack();
    table.coprocessorService(AggregateService.class, scan.getStartRow(), scan.getStopRow(), new Batch.Call<AggregateService, S>() {

        @Override
        public S call(AggregateService instance) throws IOException {
            RpcController controller = new AggregationClientRpcController();
            // Not sure what is going on here why I have to do these casts. TODO.
            CoprocessorRpcUtils.BlockingRpcCallback<AggregateResponse> rpcCallback = new CoprocessorRpcUtils.BlockingRpcCallback<>();
            instance.getSum(controller, requestArg, rpcCallback);
            AggregateResponse response = rpcCallback.get();
            if (controller.failed()) {
                throw new IOException(controller.errorText());
            }
            if (response.getFirstPartCount() == 0) {
                return null;
            }
            ByteString b = response.getFirstPart(0);
            T t = getParsedGenericInstance(ci.getClass(), 4, b);
            S s = ci.getPromotedValueFromProto(t);
            return s;
        }
    }, sumCallBack);
    return sumCallBack.getSumResult();
}
Also used : AggregateRequest(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateRequest) ByteString(com.google.protobuf.ByteString) AggregateResponse(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateResponse) IOException(java.io.IOException) 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)

Example 8 with AggregateRequest

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

the class AggregationClient method max.

/**
   * It gives the maximum value of a column for a given column family for the
   * given range. In case qualifier is null, a max of all values for the given
   * family is returned.
   * @param table
   * @param ci
   * @param scan
   * @return max val &lt;&gt;
   * @throws Throwable
   *           The caller is supposed to handle the exception as they are thrown
   *           &amp; propagated to it.
   */
public <R, S, P extends Message, Q extends Message, T extends Message> R max(final Table table, final ColumnInterpreter<R, S, P, Q, T> ci, final Scan scan) throws Throwable {
    final AggregateRequest requestArg = validateArgAndGetPB(scan, ci, false);
    class MaxCallBack implements Batch.Callback<R> {

        R max = null;

        R getMax() {
            return max;
        }

        @Override
        public synchronized void update(byte[] region, byte[] row, R result) {
            max = (max == null || (result != null && ci.compare(max, result) < 0)) ? result : max;
        }
    }
    MaxCallBack aMaxCallBack = new MaxCallBack();
    table.coprocessorService(AggregateService.class, scan.getStartRow(), scan.getStopRow(), new Batch.Call<AggregateService, R>() {

        @Override
        public R call(AggregateService instance) throws IOException {
            RpcController controller = new AggregationClientRpcController();
            CoprocessorRpcUtils.BlockingRpcCallback<AggregateResponse> rpcCallback = new CoprocessorRpcUtils.BlockingRpcCallback<>();
            instance.getMax(controller, requestArg, rpcCallback);
            AggregateResponse response = rpcCallback.get();
            if (controller.failed()) {
                throw new IOException(controller.errorText());
            }
            if (response.getFirstPartCount() > 0) {
                ByteString b = response.getFirstPart(0);
                Q q = getParsedGenericInstance(ci.getClass(), 3, b);
                return ci.getCellValueFromProto(q);
            }
            return null;
        }
    }, aMaxCallBack);
    return aMaxCallBack.getMax();
}
Also used : AggregateRequest(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateRequest) ByteString(com.google.protobuf.ByteString) AggregateResponse(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateResponse) IOException(java.io.IOException) 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)

Example 9 with AggregateRequest

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

the class AsyncAggregationClient method max.

public static <R, S, P extends Message, Q extends Message, T extends Message> CompletableFuture<R> max(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 max;

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

        @Override
        protected R getFinalResult() {
            return max;
        }
    };
    table.coprocessorService(channel -> AggregateService.newStub(channel), (stub, controller, rpcCallback) -> stub.getMax(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 10 with AggregateRequest

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

the class AsyncAggregationClient method sumByRegion.

// the map key is the startRow of the region
private static <R, S, P extends Message, Q extends Message, T extends Message> CompletableFuture<NavigableMap<byte[], S>> sumByRegion(RawAsyncTable table, ColumnInterpreter<R, S, P, Q, T> ci, Scan scan) {
    CompletableFuture<NavigableMap<byte[], S>> future = new CompletableFuture<NavigableMap<byte[], S>>();
    AggregateRequest req;
    try {
        req = validateArgAndGetPB(scan, ci, false);
    } catch (IOException e) {
        future.completeExceptionally(e);
        return future;
    }
    int firstPartIndex = scan.getFamilyMap().get(scan.getFamilies()[0]).size() - 1;
    AbstractAggregationCallback<NavigableMap<byte[], S>> callback = new AbstractAggregationCallback<NavigableMap<byte[], S>>(future) {

        private final NavigableMap<byte[], S> map = new TreeMap<>(Bytes.BYTES_COMPARATOR);

        @Override
        protected void aggregate(HRegionInfo region, AggregateResponse resp) throws IOException {
            if (resp.getFirstPartCount() > 0) {
                map.put(region.getStartKey(), getPromotedValueFromProto(ci, resp, firstPartIndex));
            }
        }

        @Override
        protected NavigableMap<byte[], S> getFinalResult() {
            return map;
        }
    };
    table.coprocessorService(channel -> AggregateService.newStub(channel), (stub, controller, rpcCallback) -> stub.getMedian(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) NavigableMap(java.util.NavigableMap) AggregateRequest(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateRequest) AggregateResponse(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateResponse) IOException(java.io.IOException)

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