Search in sources :

Example 16 with AggregateResponse

use of org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateResponse 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 17 with AggregateResponse

use of org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateResponse 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 18 with AggregateResponse

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

the class AggregateImplementation method getAvg.

/**
   * Gives a Pair with first object as Sum and second object as row count,
   * computed for a given combination of column qualifier and column family in
   * the given row range as defined in the Scan object. In its current
   * implementation, it takes one column family and one column qualifier (if
   * provided). In case of null column qualifier, an aggregate sum over all the
   * entire column family will be returned.
   * <p>
   * The average is computed in
   * AggregationClient#avg(byte[], ColumnInterpreter, Scan) by
   * processing results from all regions, so its "ok" to pass sum and a Long
   * type.
   */
@Override
public void getAvg(RpcController controller, AggregateRequest request, RpcCallback<AggregateResponse> done) {
    AggregateResponse response = null;
    InternalScanner scanner = null;
    try {
        ColumnInterpreter<T, S, P, Q, R> ci = constructColumnInterpreterFromRequest(request);
        S sumVal = null;
        Long rowCountVal = 0l;
        Scan scan = ProtobufUtil.toScan(request.getScan());
        scanner = env.getRegion().getScanner(scan);
        byte[] colFamily = scan.getFamilies()[0];
        NavigableSet<byte[]> qualifiers = scan.getFamilyMap().get(colFamily);
        byte[] qualifier = null;
        if (qualifiers != null && !qualifiers.isEmpty()) {
            qualifier = qualifiers.pollFirst();
        }
        List<Cell> results = new ArrayList<>();
        boolean hasMoreRows = false;
        do {
            results.clear();
            hasMoreRows = scanner.next(results);
            int listSize = results.size();
            for (int i = 0; i < listSize; i++) {
                sumVal = ci.add(sumVal, ci.castToReturnType(ci.getValue(colFamily, qualifier, results.get(i))));
            }
            rowCountVal++;
        } while (hasMoreRows);
        if (sumVal != null) {
            ByteString first = ci.getProtoForPromotedType(sumVal).toByteString();
            AggregateResponse.Builder pair = AggregateResponse.newBuilder();
            pair.addFirstPart(first);
            ByteBuffer bb = ByteBuffer.allocate(8).putLong(rowCountVal);
            bb.rewind();
            pair.setSecondPart(ByteString.copyFrom(bb));
            response = pair.build();
        }
    } catch (IOException e) {
        CoprocessorRpcUtils.setControllerException(controller, e);
    } finally {
        if (scanner != null) {
            try {
                scanner.close();
            } catch (IOException ignored) {
            }
        }
    }
    done.run(response);
}
Also used : InternalScanner(org.apache.hadoop.hbase.regionserver.InternalScanner) ByteString(com.google.protobuf.ByteString) AggregateResponse(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateResponse) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Scan(org.apache.hadoop.hbase.client.Scan) Cell(org.apache.hadoop.hbase.Cell)

Example 19 with AggregateResponse

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

the class AggregateImplementation method getSum.

/**
   * Gives the sum for a given combination of column qualifier and column
   * family, in the given row range as defined in the Scan object. In its
   * current implementation, it takes one column family and one column qualifier
   * (if provided). In case of null column qualifier, sum for the entire column
   * family will be returned.
   */
@Override
public void getSum(RpcController controller, AggregateRequest request, RpcCallback<AggregateResponse> done) {
    AggregateResponse response = null;
    InternalScanner scanner = null;
    long sum = 0l;
    try {
        ColumnInterpreter<T, S, P, Q, R> ci = constructColumnInterpreterFromRequest(request);
        S sumVal = null;
        T temp;
        Scan scan = ProtobufUtil.toScan(request.getScan());
        scanner = env.getRegion().getScanner(scan);
        byte[] colFamily = scan.getFamilies()[0];
        NavigableSet<byte[]> qualifiers = scan.getFamilyMap().get(colFamily);
        byte[] qualifier = null;
        if (qualifiers != null && !qualifiers.isEmpty()) {
            qualifier = qualifiers.pollFirst();
        }
        List<Cell> results = new ArrayList<>();
        boolean hasMoreRows = false;
        do {
            hasMoreRows = scanner.next(results);
            int listSize = results.size();
            for (int i = 0; i < listSize; i++) {
                temp = ci.getValue(colFamily, qualifier, results.get(i));
                if (temp != null)
                    sumVal = ci.add(sumVal, ci.castToReturnType(temp));
            }
            results.clear();
        } while (hasMoreRows);
        if (sumVal != null) {
            response = AggregateResponse.newBuilder().addFirstPart(ci.getProtoForPromotedType(sumVal).toByteString()).build();
        }
    } catch (IOException e) {
        CoprocessorRpcUtils.setControllerException(controller, e);
    } finally {
        if (scanner != null) {
            try {
                scanner.close();
            } catch (IOException ignored) {
            }
        }
    }
    log.debug("Sum from this region is " + env.getRegion().getRegionInfo().getRegionNameAsString() + ": " + sum);
    done.run(response);
}
Also used : InternalScanner(org.apache.hadoop.hbase.regionserver.InternalScanner) AggregateResponse(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateResponse) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Scan(org.apache.hadoop.hbase.client.Scan) Cell(org.apache.hadoop.hbase.Cell)

Example 20 with AggregateResponse

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

the class AggregateImplementation method getStd.

/**
   * Gives a Pair with first object a List containing Sum and sum of squares,
   * and the second object as row count. It is computed for a given combination of
   * column qualifier and column family in the given row range as defined in the
   * Scan object. In its current implementation, it takes one column family and
   * one column qualifier (if provided). The idea is get the value of variance first:
   * the average of the squares less the square of the average a standard
   * deviation is square root of variance.
   */
@Override
public void getStd(RpcController controller, AggregateRequest request, RpcCallback<AggregateResponse> done) {
    InternalScanner scanner = null;
    AggregateResponse response = null;
    try {
        ColumnInterpreter<T, S, P, Q, R> ci = constructColumnInterpreterFromRequest(request);
        S sumVal = null, sumSqVal = null, tempVal = null;
        long rowCountVal = 0l;
        Scan scan = ProtobufUtil.toScan(request.getScan());
        scanner = env.getRegion().getScanner(scan);
        byte[] colFamily = scan.getFamilies()[0];
        NavigableSet<byte[]> qualifiers = scan.getFamilyMap().get(colFamily);
        byte[] qualifier = null;
        if (qualifiers != null && !qualifiers.isEmpty()) {
            qualifier = qualifiers.pollFirst();
        }
        List<Cell> results = new ArrayList<>();
        boolean hasMoreRows = false;
        do {
            tempVal = null;
            hasMoreRows = scanner.next(results);
            int listSize = results.size();
            for (int i = 0; i < listSize; i++) {
                tempVal = ci.add(tempVal, ci.castToReturnType(ci.getValue(colFamily, qualifier, results.get(i))));
            }
            results.clear();
            sumVal = ci.add(sumVal, tempVal);
            sumSqVal = ci.add(sumSqVal, ci.multiply(tempVal, tempVal));
            rowCountVal++;
        } while (hasMoreRows);
        if (sumVal != null) {
            ByteString first_sumVal = ci.getProtoForPromotedType(sumVal).toByteString();
            ByteString first_sumSqVal = ci.getProtoForPromotedType(sumSqVal).toByteString();
            AggregateResponse.Builder pair = AggregateResponse.newBuilder();
            pair.addFirstPart(first_sumVal);
            pair.addFirstPart(first_sumSqVal);
            ByteBuffer bb = ByteBuffer.allocate(8).putLong(rowCountVal);
            bb.rewind();
            pair.setSecondPart(ByteString.copyFrom(bb));
            response = pair.build();
        }
    } catch (IOException e) {
        CoprocessorRpcUtils.setControllerException(controller, e);
    } finally {
        if (scanner != null) {
            try {
                scanner.close();
            } catch (IOException ignored) {
            }
        }
    }
    done.run(response);
}
Also used : InternalScanner(org.apache.hadoop.hbase.regionserver.InternalScanner) ByteString(com.google.protobuf.ByteString) AggregateResponse(org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateResponse) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Scan(org.apache.hadoop.hbase.client.Scan) Cell(org.apache.hadoop.hbase.Cell)

Aggregations

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