for (int i = 0; i < listSize; i++) { Cell kv = results.get(i); tempVal = ci.add(tempVal, ci.castToReturnType(ci.getValue(colFamily, valQualifier, kv))); if (weightQualifier != null) { tempWeight = ci.add(tempWeight, ci.castToReturnType(ci.getValue(colFamily, weightQualifier, kv))); sumVal = ci.add(sumVal, tempVal); sumWeights = ci.add(sumWeights, tempWeight); } while (hasMoreRows); ByteString first_sumVal = ci.getProtoForPromotedType(sumVal).toByteString(); S s = sumWeights == null ? ci.castToReturnType(ci.getMinValue()) : sumWeights; ByteString first_sumWeights = ci.getProtoForPromotedType(s).toByteString(); AggregateResponse.Builder pair = AggregateResponse.newBuilder(); pair.addFirstPart(first_sumVal);
int listSize = results.size(); for (int i = 0; i < listSize; i++) { temp = ci.getValue(colFamily, qualifier, results.get(i)); min = (min == null || (temp != null && ci.compare(temp, min) < 0)) ? temp : min; if (min != null) { response = AggregateResponse.newBuilder().addFirstPart( ci.getProtoForCellType(min).toByteString()).build();
@Override public void onNext(Result[] results, ScanController controller) { try { for (Result result : results) { Cell weightCell = result.getColumnLatestCell(family, weightQualifier); R weight = ci.getValue(family, weightQualifier, weightCell); sum = ci.add(sum, ci.castToReturnType(weight)); if (ci.divideForAvg(sum, 1L) > halfSum) { if (value != null) { future.complete(value); } else { future.completeExceptionally(new NoSuchElementException()); } controller.terminate(); return; } Cell valueCell = result.getColumnLatestCell(family, valueQualifier); value = ci.getValue(family, valueQualifier, valueCell); } } catch (IOException e) { future.completeExceptionally(e); controller.terminate(); } }
private static <R, S, P extends Message, Q extends Message, T extends Message> void findMedian( CompletableFuture<R> future, AsyncTable<AdvancedScanResultConsumer> table, ColumnInterpreter<R, S, P, Q, T> ci, Scan scan, NavigableMap<byte[], S> sumByRegion) { double halfSum = ci.divideForAvg(sumByRegion.values().stream().reduce(ci::add).get(), 2L); S movingSum = null; byte[] startRow = null; for (Map.Entry<byte[], S> entry : sumByRegion.entrySet()) { startRow = entry.getKey(); S newMovingSum = ci.add(movingSum, entry.getValue()); if (ci.divideForAvg(newMovingSum, 1L) > halfSum) { break;
S sumVal = p.getSecond().get(0); S sumWeights = p.getSecond().get(1); double halfSumVal = ci.divideForAvg(sumVal, 2L); double movingSumVal = 0; boolean weighted = false; if (quals.size() > 1) { weighted = true; halfSumVal = ci.divideForAvg(sumWeights, 2L); double newSumVal = movingSumVal + ci.divideForAvg(s, 1L); if (newSumVal > halfSumVal) { R newValue = ci.getValue(colFamily, weightQualifier, kv); S s = ci.castToReturnType(newValue); double newSumVal = movingSumVal + ci.divideForAvg(s, 1L); value = ci.getValue(colFamily, qualifier, kv);
@Override public <T, S> Pair<S, Long> getAvg(ColumnInterpreter<T, S> ci, Scan scan) throws IOException { S sumVal = null; Long rowCountVal = 0l; InternalScanner scanner = ((RegionCoprocessorEnvironment) getEnvironment()) .getRegion().getScanner(scan); byte[] colFamily = scan.getFamilies()[0]; byte[] qualifier = scan.getFamilyMap().get(colFamily).pollFirst(); List<KeyValue> results = new ArrayList<KeyValue>(); boolean hasMoreRows = false; try { do { results.clear(); hasMoreRows = scanner.next(results); for (KeyValue kv : results) { sumVal = ci.add(sumVal, ci.castToReturnType(ci.getValue(colFamily, qualifier, kv))); } rowCountVal++; } while (hasMoreRows); } finally { scanner.close(); } Pair<S, Long> pair = new Pair<S, Long>(sumVal, rowCountVal); return pair; }
@Override protected Double getFinalResult() { return ci.divideForAvg(sum, count); } };
@Override protected void aggregate(RegionInfo region, AggregateResponse resp) throws IOException { if (resp.getFirstPartCount() > 0) { S s = getPromotedValueFromProto(ci, resp, 0); sum = ci.add(sum, s); } }
@Override public <T, S> T getMax(ColumnInterpreter<T, S> ci, Scan scan) throws IOException { T temp; T max = null; InternalScanner scanner = ((RegionCoprocessorEnvironment) getEnvironment()) .getRegion().getScanner(scan); List<KeyValue> results = new ArrayList<KeyValue>(); byte[] colFamily = scan.getFamilies()[0]; byte[] qualifier = scan.getFamilyMap().get(colFamily).pollFirst(); // qualifier can be null. try { boolean hasMoreRows = false; do { hasMoreRows = scanner.next(results); for (KeyValue kv : results) { temp = ci.getValue(colFamily, qualifier, kv); max = (max == null || (temp != null && ci.compare(temp, max) > 0)) ? temp : max; } results.clear(); } while (hasMoreRows); } finally { scanner.close(); } log.info("Maximum from this region is " + ((RegionCoprocessorEnvironment) getEnvironment()).getRegion() .getRegionNameAsString() + ": " + max); return max; }
@Override protected void aggregate(RegionInfo 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; } } }
static <R, S, P extends Message, Q extends Message, T extends Message> AggregateRequest validateArgAndGetPB(Scan scan, ColumnInterpreter<R, S, P, Q, T> ci, boolean canFamilyBeAbsent) throws IOException { validateParameters(scan, canFamilyBeAbsent); final AggregateRequest.Builder requestBuilder = AggregateRequest.newBuilder(); requestBuilder.setInterpreterClassName(ci.getClass().getCanonicalName()); P columnInterpreterSpecificData = ci.getRequestData(); if (columnInterpreterSpecificData != null) { requestBuilder.setInterpreterSpecificBytes(columnInterpreterSpecificData.toByteString()); } requestBuilder.setScan(ProtobufUtil.toScan(scan)); return requestBuilder.build(); }
S sumVal = p.getSecond().get(0); S sumWeights = p.getSecond().get(1); double halfSumVal = ci.divideForAvg(sumVal, 2L); double movingSumVal = 0; boolean weighted = false; if (quals.size() > 1) { weighted = true; halfSumVal = ci.divideForAvg(sumWeights, 2L); double newSumVal = movingSumVal + ci.divideForAvg(s, 1L); if (newSumVal > halfSumVal) break; // we found the region with the median movingSumVal = newSumVal; R newValue = ci.getValue(colFamily, weightQualifier, kv); S s = ci.castToReturnType(newValue); double newSumVal = movingSumVal + ci.divideForAvg(s, 1L); value = ci.getValue(colFamily, qualifier, kv);
@Override public <T, S> S getSum(ColumnInterpreter<T, S> ci, Scan scan) throws IOException { long sum = 0l; S sumVal = null; T temp; InternalScanner scanner = ((RegionCoprocessorEnvironment) getEnvironment()) .getRegion().getScanner(scan); byte[] colFamily = scan.getFamilies()[0]; byte[] qualifier = scan.getFamilyMap().get(colFamily).pollFirst(); List<KeyValue> results = new ArrayList<KeyValue>(); try { boolean hasMoreRows = false; do { hasMoreRows = scanner.next(results); for (KeyValue kv : results) { temp = ci.getValue(colFamily, qualifier, kv); if (temp != null) sumVal = ci.add(sumVal, ci.castToReturnType(temp)); } results.clear(); } while (hasMoreRows); } finally { scanner.close(); } log.debug("Sum from this region is " + ((RegionCoprocessorEnvironment) getEnvironment()).getRegion() .getRegionNameAsString() + ": " + sum); return sumVal; }
@Override protected Double getFinalResult() { double avg = ci.divideForAvg(sum, count); double avgSq = ci.divideForAvg(sumSq, count); return Math.sqrt(avgSq - avg * avg); } };
private static <R, S, P extends Message, Q extends Message, T extends Message> void findMedian( CompletableFuture<R> future, AsyncTable<AdvancedScanResultConsumer> table, ColumnInterpreter<R, S, P, Q, T> ci, Scan scan, NavigableMap<byte[], S> sumByRegion) { double halfSum = ci.divideForAvg(sumByRegion.values().stream().reduce(ci::add).get(), 2L); S movingSum = null; byte[] startRow = null; for (Map.Entry<byte[], S> entry : sumByRegion.entrySet()) { startRow = entry.getKey(); S newMovingSum = ci.add(movingSum, entry.getValue()); if (ci.divideForAvg(newMovingSum, 1L) > halfSum) { break;
@Override protected void aggregate(RegionInfo region, AggregateResponse resp) throws IOException { if (resp.getFirstPartCount() > 0) { sum = ci.add(sum, getPromotedValueFromProto(ci, resp, 0)); sumSq = ci.add(sumSq, getPromotedValueFromProto(ci, resp, 1)); count += resp.getSecondPart().asReadOnlyByteBuffer().getLong(); } }
@Override public <T, S> T getMin(ColumnInterpreter<T, S> ci, Scan scan) throws IOException { T min = null; T temp; InternalScanner scanner = ((RegionCoprocessorEnvironment) getEnvironment()) .getRegion().getScanner(scan); List<KeyValue> results = new ArrayList<KeyValue>(); byte[] colFamily = scan.getFamilies()[0]; byte[] qualifier = scan.getFamilyMap().get(colFamily).pollFirst(); try { boolean hasMoreRows = false; do { hasMoreRows = scanner.next(results); for (KeyValue kv : results) { temp = ci.getValue(colFamily, qualifier, kv); min = (min == null || (temp != null && ci.compare(temp, min) < 0)) ? temp : min; } results.clear(); } while (hasMoreRows); } finally { scanner.close(); } log.info("Minimum from this region is " + ((RegionCoprocessorEnvironment) getEnvironment()).getRegion() .getRegionNameAsString() + ": " + min); return min; }
@Override protected void aggregate(RegionInfo 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; } } }
static <R, S, P extends Message, Q extends Message, T extends Message> AggregateRequest validateArgAndGetPB(Scan scan, ColumnInterpreter<R, S, P, Q, T> ci, boolean canFamilyBeAbsent) throws IOException { validateParameters(scan, canFamilyBeAbsent); final AggregateRequest.Builder requestBuilder = AggregateRequest.newBuilder(); requestBuilder.setInterpreterClassName(ci.getClass().getCanonicalName()); P columnInterpreterSpecificData = null; if ((columnInterpreterSpecificData = ci.getRequestData()) != null) { requestBuilder.setInterpreterSpecificBytes(columnInterpreterSpecificData.toByteString()); } requestBuilder.setScan(ProtobufUtil.toScan(scan)); return requestBuilder.build(); }
int listSize = results.size(); for (int i = 0; i < listSize; i++) { tempVal = ci.add(tempVal, ci.castToReturnType(ci.getValue(colFamily, qualifier, results.get(i)))); 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);