Java Code Examples for org.apache.hadoop.hbase.regionserver.InternalScanner#close()

The following examples show how to use org.apache.hadoop.hbase.regionserver.InternalScanner#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestFilterFromRegionSide.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testFirstKeyOnlyFilterAndBatch() throws IOException {
  Scan scan = new Scan();
  scan.setFilter(new FirstKeyOnlyFilter());
  scan.setBatch(1);
  InternalScanner scanner = REGION.getScanner(scan);
  List<Cell> results = new ArrayList<>();
  for (int i = 0; i < NUM_ROWS; i++) {
    results.clear();
    scanner.next(results);
    assertEquals(1, results.size());
    Cell cell = results.get(0);
    assertArrayEquals(ROWS[i],
        Bytes.copy(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
  }
  assertFalse(scanner.next(results));
  scanner.close();
}
 
Example 2
Source File: HBaseTestingUtility.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Do a small get/scan against one store. This is required because store
 * has no actual methods of querying itself, and relies on StoreScanner.
 */
public static List<Cell> getFromStoreFile(HStore store,
                                              Get get) throws IOException {
  Scan scan = new Scan(get);
  InternalScanner scanner = (InternalScanner) store.getScanner(scan,
      scan.getFamilyMap().get(store.getColumnFamilyDescriptor().getName()),
      // originally MultiVersionConcurrencyControl.resetThreadReadPoint() was called to set
      // readpoint 0.
      0);

  List<Cell> result = new ArrayList<>();
  scanner.next(result);
  if (!result.isEmpty()) {
    // verify that we are on the row we want:
    Cell kv = result.get(0);
    if (!CellUtil.matchingRows(kv, get.getRow())) {
      result.clear();
    }
  }
  scanner.close();
  return result;
}
 
Example 3
Source File: TestFilterFromRegionSide.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testFirstSeveralCellsFilterAndBatch() throws IOException {
  Scan scan = new Scan();
  scan.setFilter(new FirstSeveralCellsFilter());
  scan.setBatch(NUM_COLS);
  InternalScanner scanner = REGION.getScanner(scan);
  List<Cell> results = new ArrayList<>();
  for (int i = 0; i < NUM_ROWS; i++) {
    results.clear();
    scanner.next(results);
    assertEquals(NUM_COLS, results.size());
    Cell cell = results.get(0);
    assertArrayEquals(ROWS[i],
        Bytes.copy(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
    assertArrayEquals(FAMILIES[0],
        Bytes.copy(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()));
    assertArrayEquals(QUALIFIERS[0], Bytes.copy(cell.getQualifierArray(),
        cell.getQualifierOffset(), cell.getQualifierLength()));
  }
  assertFalse(scanner.next(results));
  scanner.close();
}
 
Example 4
Source File: TestMobStoreCompaction.java    From hbase with Apache License 2.0 6 votes vote down vote up
private int countMobRows() throws IOException {
  Scan scan = new Scan();
  // Do not retrieve the mob data when scanning
  scan.setAttribute(MobConstants.MOB_SCAN_RAW, Bytes.toBytes(Boolean.TRUE));
  InternalScanner scanner = region.getScanner(scan);

  int scannedCount = 0;
  List<Cell> results = new ArrayList<>();
  boolean hasMore = true;
  while (hasMore) {
    hasMore = scanner.next(results);
    for (Cell c : results) {
      if (MobUtils.isMobReferenceCell(c)) {
        scannedCount++;
      }
    }
    results.clear();
  }
  scanner.close();

  return scannedCount;
}
 
Example 5
Source File: TestFilter.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testWhileMatchFilterWithFilterRowKeyWithReverseScan()
    throws Exception {
  Scan s = new Scan();
  String prefix = "testRowOne";
  WhileMatchFilter filter = new WhileMatchFilter(new PrefixFilter(
      Bytes.toBytes(prefix)));
  s.setFilter(filter);
  s.setReversed(true);

  InternalScanner scanner = this.region.getScanner(s);
  while (true) {
    ArrayList<Cell> values = new ArrayList<>();
    boolean isMoreResults = scanner.next(values);
    if (!isMoreResults
        || !Bytes.toString(CellUtil.cloneRow(values.get(0))).startsWith(prefix)) {
      Assert.assertTrue(
          "The WhileMatchFilter should now filter all remaining",
          filter.filterAllRemaining());
    }
    if (!isMoreResults) {
      break;
    }
  }
  scanner.close();
}
 
Example 6
Source File: RowCountEndpoint.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a count of the rows in the region where this coprocessor is loaded.
 */
@Override
public void getRowCount(RpcController controller, CountRequest request,
                        RpcCallback<CountResponse> done) {
  Scan scan = new Scan();
  scan.setFilter(new FirstKeyOnlyFilter());
  CountResponse response = null;
  InternalScanner scanner = null;
  try {
    scanner = env.getRegion().getScanner(scan);
    List<Cell> results = new ArrayList<>();
    boolean hasMore = false;
    byte[] lastRow = null;
    long count = 0;
    do {
      hasMore = scanner.next(results);
      for (Cell kv : results) {
        byte[] currentRow = CellUtil.cloneRow(kv);
        if (lastRow == null || !Bytes.equals(lastRow, currentRow)) {
          lastRow = currentRow;
          count++;
        }
      }
      results.clear();
    } while (hasMore);

    response = CountResponse.newBuilder()
        .setCount(count).build();
  } catch (IOException ioe) {
    CoprocessorRpcUtils.setControllerException(controller, ioe);
  } finally {
    if (scanner != null) {
      try {
        scanner.close();
      } catch (IOException ignored) {}
    }
  }
  done.run(response);
}
 
Example 7
Source File: TestRowCountEndPoint.java    From BigData-In-Practice with Apache License 2.0 5 votes vote down vote up
@Override
public void getRowCount(RpcController controller,
                        getRowCountRequest request,
                        RpcCallback<getRowCountResponse> done) {
    // 单个region上的计算结果值
    int result = 0;

    // 定义返回response
    getRowCountResponse.Builder responseBuilder = getRowCountResponse.newBuilder();
    // 进行行数统计
    InternalScanner scanner = null;
    try {
        Scan scan = new Scan();
        scanner = this.envi.getRegion().getScanner(scan);
        List<Cell> results = new ArrayList<Cell>();
        boolean hasMore = false;

        do {
            hasMore = scanner.next(results);
            result++;
        } while (hasMore);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        if (scanner != null) {
            try {
                scanner.close();
            } catch (IOException ignored) {
                // nothing to do
            }
        }
    }

    responseBuilder.setRowCount(result);
    done.run(responseBuilder.build());
    return;

}
 
Example 8
Source File: Export.java    From hbase with Apache License 2.0 5 votes vote down vote up
void checkScannerClose(final InternalScanner s) throws IOException {
  if (s == null) {
    return;
  }
  if (region.getCoprocessorHost() == null) {
    s.close();
    return;
  }
  region.getCoprocessorHost().preScannerClose(s);
  try {
    s.close();
  } finally {
    region.getCoprocessorHost().postScannerClose(s);
  }
}
 
Example 9
Source File: TestFilter.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testWhileMatchFilterWithFilterRowWithReverseScan()
    throws Exception {
  final int pageSize = 4;

  Scan s = new Scan();
  s.setReversed(true);
  WhileMatchFilter filter = new WhileMatchFilter(new PageFilter(pageSize));
  s.setFilter(filter);

  InternalScanner scanner = this.region.getScanner(s);
  int scannerCounter = 0;
  while (true) {
    boolean isMoreResults = scanner.next(new ArrayList<>());
    scannerCounter++;

    if (scannerCounter >= pageSize) {
      Assert.assertTrue(
          "The WhileMatchFilter should now filter all remaining",
          filter.filterAllRemaining());
    }
    if (!isMoreResults) {
      break;
    }
  }
  scanner.close();
  Assert.assertEquals("The page filter returned more rows than expected",
      pageSize, scannerCounter);
}
 
Example 10
Source File: TestMobStoreCompaction.java    From hbase with Apache License 2.0 5 votes vote down vote up
private int countReferencedMobFiles() throws IOException {
  Scan scan = new Scan();
  // Do not retrieve the mob data when scanning
  scan.setAttribute(MobConstants.MOB_SCAN_RAW, Bytes.toBytes(Boolean.TRUE));
  InternalScanner scanner = region.getScanner(scan);

  List<Cell> kvs = new ArrayList<>();
  boolean hasMore = true;
  String fileName;
  Set<String> files = new HashSet<>();
  do {
    kvs.clear();
    hasMore = scanner.next(kvs);
    for (Cell kv : kvs) {
      if (!MobUtils.isMobReferenceCell(kv)) {
        continue;
      }
      if (!MobUtils.hasValidMobRefCellValue(kv)) {
        continue;
      }
      int size = MobUtils.getMobValueLength(kv);
      if (size <= mobCellThreshold) {
        continue;
      }
      fileName = MobUtils.getMobFileName(kv);
      if (fileName.isEmpty()) {
        continue;
      }
      files.add(fileName);
      Path familyPath = MobUtils.getMobFamilyPath(conf, tableDescriptor.getTableName(),
          familyDescriptor.getNameAsString());
      assertTrue(fs.exists(new Path(familyPath, fileName)));
    }
  } while (hasMore);

  scanner.close();

  return files.size();
}
 
Example 11
Source File: TestRowProcessorEndpoint.java    From hbase with Apache License 2.0 5 votes vote down vote up
public static void doScan(HRegion region, Scan scan, List<Cell> result) throws IOException {
  InternalScanner scanner = null;
  try {
    scan.setIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
    scanner = region.getScanner(scan);
    result.clear();
    scanner.next(result);
  } finally {
    if (scanner != null) {
      scanner.close();
    }
  }
}
 
Example 12
Source File: TestScannerSelectionUsingKeyRange.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testScannerSelection() throws IOException {
  Configuration conf = TEST_UTIL.getConfiguration();
  conf.setInt("hbase.hstore.compactionThreshold", 10000);
  ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor familyDescriptor =
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY_BYTES)
      .setBlockCacheEnabled(true)
      .setBloomFilterType(bloomType);
  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(TABLE);

  tableDescriptor.setColumnFamily(familyDescriptor);
  RegionInfo info = RegionInfoBuilder.newBuilder(TABLE).build();
  HRegion region = HBaseTestingUtility.createRegionAndWAL(info, TEST_UTIL.getDataTestDir(), conf,
    tableDescriptor);

  for (int iFile = 0; iFile < NUM_FILES; ++iFile) {
    for (int iRow = 0; iRow < NUM_ROWS; ++iRow) {
      Put put = new Put(Bytes.toBytes("row" + iRow));
      for (int iCol = 0; iCol < NUM_COLS_PER_ROW; ++iCol) {
        put.addColumn(FAMILY_BYTES, Bytes.toBytes("col" + iCol),
                Bytes.toBytes("value" + iFile + "_" + iRow + "_" + iCol));
      }
      region.put(put);
    }
    region.flush(true);
  }

  Scan scan = new Scan().withStartRow(Bytes.toBytes("aaa")).withStopRow(Bytes.toBytes("aaz"));
  BlockCache cache = BlockCacheFactory.createBlockCache(conf);
  InternalScanner scanner = region.getScanner(scan);
  List<Cell> results = new ArrayList<>();
  while (scanner.next(results)) {
  }
  scanner.close();
  assertEquals(0, results.size());
  if (cache instanceof LruBlockCache) {
    Set<String> accessedFiles = ((LruBlockCache)cache).getCachedFileNamesForTest();
    assertEquals(expectedCount, accessedFiles.size());
  }
  HBaseTestingUtility.closeRegionAndWAL(region);
}
 
Example 13
Source File: PermissionStorage.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Loads all of the permission grants stored in a region of the {@code _acl_}
 * table.
 *
 * @param aclRegion the acl region
 * @return a map of the permissions for this table.
 * @throws IOException if an error occurs
 */
static Map<byte[], ListMultimap<String, UserPermission>> loadAll(Region aclRegion)
    throws IOException {
  if (!isAclRegion(aclRegion)) {
    throw new IOException("Can only load permissions from "+ACL_TABLE_NAME);
  }

  Map<byte[], ListMultimap<String, UserPermission>> allPerms =
    new TreeMap<>(Bytes.BYTES_RAWCOMPARATOR);

  // do a full scan of _acl_ table

  Scan scan = new Scan();
  scan.addFamily(ACL_LIST_FAMILY);

  InternalScanner iScanner = null;
  try {
    iScanner = aclRegion.getScanner(scan);

    while (true) {
      List<Cell> row = new ArrayList<>();

      boolean hasNext = iScanner.next(row);
      ListMultimap<String, UserPermission> perms = ArrayListMultimap.create();
      byte[] entry = null;
      for (Cell kv : row) {
        if (entry == null) {
          entry = CellUtil.cloneRow(kv);
        }
        Pair<String, Permission> permissionsOfUserOnTable =
            parsePermissionRecord(entry, kv, null, null, false, null);
        if (permissionsOfUserOnTable != null) {
          String username = permissionsOfUserOnTable.getFirst();
          Permission permission = permissionsOfUserOnTable.getSecond();
          perms.put(username, new UserPermission(username, permission));
        }
      }
      if (entry != null) {
        allPerms.put(entry, perms);
      }
      if (!hasNext) {
        break;
      }
    }
  } finally {
    if (iScanner != null) {
      iScanner.close();
    }
  }

  return allPerms;
}
 
Example 14
Source File: AggregateImplementation.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Gives a List containing sum of values and sum of weights.
 * It is computed for the combination of column
 * family and column qualifier(s) in the given row range as defined in the
 * Scan object. In its current implementation, it takes one column family and
 * two column qualifiers. The first qualifier is for values column and
 * the second qualifier (optional) is for weight column.
 */
@Override
public void getMedian(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, sumWeights = null, tempVal = null, tempWeight = null;
    Scan scan = ProtobufUtil.toScan(request.getScan());
    scanner = env.getRegion().getScanner(scan);
    byte[] colFamily = scan.getFamilies()[0];
    NavigableSet<byte[]> qualifiers = scan.getFamilyMap().get(colFamily);
    byte[] valQualifier = null, weightQualifier = null;
    if (qualifiers != null && !qualifiers.isEmpty()) {
      valQualifier = qualifiers.pollFirst();
      // if weighted median is requested, get qualifier for the weight column
      weightQualifier = qualifiers.pollLast();
    }
    List<Cell> results = new ArrayList<>();

    boolean hasMoreRows = false;

    do {
      tempVal = null;
      tempWeight = null;
      hasMoreRows = scanner.next(results);
      int listSize = results.size();
      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)));
        }
      }
      results.clear();
      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);
    pair.addFirstPart(first_sumWeights);
    response = pair.build();
  } catch (IOException e) {
    CoprocessorRpcUtils.setControllerException(controller, e);
  } finally {
    if (scanner != null) {
      try {
        scanner.close();
      } catch (IOException ignored) {}
    }
  }
  done.run(response);
}
 
Example 15
Source File: AggregateImplementation.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 16
Source File: AggregateImplementation.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 17
Source File: AggregateImplementation.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Gives the row count for the given column family and column qualifier, in
 * the given row range as defined in the Scan object.
 */
@Override
public void getRowNum(RpcController controller, AggregateRequest request,
        RpcCallback<AggregateResponse> done) {
  AggregateResponse response = null;
  long counter = 0L;
  List<Cell> results = new ArrayList<>();
  InternalScanner scanner = null;
  try {
    Scan scan = ProtobufUtil.toScan(request.getScan());
    byte[][] colFamilies = scan.getFamilies();
    byte[] colFamily = colFamilies != null ? colFamilies[0] : null;
    NavigableSet<byte[]> qualifiers = colFamilies != null ?
        scan.getFamilyMap().get(colFamily) : null;
    byte[] qualifier = null;
    if (qualifiers != null && !qualifiers.isEmpty()) {
      qualifier = qualifiers.pollFirst();
    }
    if (scan.getFilter() == null && qualifier == null) {
      scan.setFilter(new FirstKeyOnlyFilter());
    }
    scanner = env.getRegion().getScanner(scan);
    boolean hasMoreRows = false;
    do {
      hasMoreRows = scanner.next(results);
      if (results.size() > 0) {
        counter++;
      }
      results.clear();
    } while (hasMoreRows);
    ByteBuffer bb = ByteBuffer.allocate(8).putLong(counter);
    bb.rewind();
    response = AggregateResponse.newBuilder().addFirstPart(
        ByteString.copyFrom(bb)).build();
  } catch (IOException e) {
    CoprocessorRpcUtils.setControllerException(controller, e);
  } finally {
    if (scanner != null) {
      try {
        scanner.close();
      } catch (IOException ignored) {}
    }
  }
  log.info("Row counter from this region is "
      + env.getRegion().getRegionInfo().getRegionNameAsString() + ": " + counter);
  done.run(response);
}
 
Example 18
Source File: AggregateImplementation.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 19
Source File: AggregateImplementation.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Gives the maximum 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, maximum value for the
 * entire column family will be returned.
 */
@Override
public void getMax(RpcController controller, AggregateRequest request,
        RpcCallback<AggregateResponse> done) {
  InternalScanner scanner = null;
  AggregateResponse response = null;
  T max = null;
  try {
    ColumnInterpreter<T, S, P, Q, R> ci = constructColumnInterpreterFromRequest(request);
    T temp;
    Scan scan = ProtobufUtil.toScan(request.getScan());
    scanner = env.getRegion().getScanner(scan);
    List<Cell> results = new ArrayList<>();
    byte[] colFamily = scan.getFamilies()[0];
    NavigableSet<byte[]> qualifiers = scan.getFamilyMap().get(colFamily);
    byte[] qualifier = null;
    if (qualifiers != null && !qualifiers.isEmpty()) {
      qualifier = qualifiers.pollFirst();
    }
    // qualifier can be null.
    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));
        max = (max == null || (temp != null && ci.compare(temp, max) > 0)) ? temp : max;
      }
      results.clear();
    } while (hasMoreRows);
    if (max != null) {
      AggregateResponse.Builder builder = AggregateResponse.newBuilder();
      builder.addFirstPart(ci.getProtoForCellType(max).toByteString());
      response = builder.build();
    }
  } catch (IOException e) {
    CoprocessorRpcUtils.setControllerException(controller, e);
  } finally {
    if (scanner != null) {
      try {
        scanner.close();
      } catch (IOException ignored) {}
    }
  }
  log.info("Maximum from this region is "
      + env.getRegion().getRegionInfo().getRegionNameAsString() + ": " + max);
  done.run(response);
}
 
Example 20
Source File: QueryEndpoint.java    From yuzhouwan with Apache License 2.0 4 votes vote down vote up
@Override
public void queryByStartRowAndEndRow(RpcController controller, DataProtos.DataQueryRequest request, RpcCallback<DataQueryResponse> done) {

    DataProtos.DataQueryResponse response = null;
    InternalScanner scanner = null;
    try {
        String startRow = request.getStartRow();
        String endRow = request.getEndRow();
        String regionStartKey = Bytes.toString(this.env.getRegion().getRegionInfo().getStartKey());
        String regionEndKey = Bytes.toString(this.env.getRegion().getRegionInfo().getEndKey());

        if (request.getIsSalting()) {                       // 如果加盐过则在key前添加盐值
            String startSalt = null;
            String endSalt = null;
            if (StrUtils.isNotEmpty(regionStartKey)) {
                startSalt = regionStartKey.split("_")[0];   // 加盐的方式为盐值+"_",所以取_前面的
            }
            if (StrUtils.isNotEmpty(regionEndKey)) {
                endSalt = regionStartKey.split("_")[0];     //加盐的方式为盐值+"_",所以取_前面的
            }
            if (startSalt != null) {
                if (null != startRow) {
                    startRow = startSalt + "_" + startRow;
                    endRow = endSalt + "_" + endRow;
                }
            }
        }
        Scan scan = new Scan();
        if (null != startRow) {
            scan.setStartRow(Bytes.toBytes(startRow));
        }
        if (null != endRow) {
            if (request.getIncluedEnd()) {
                Filter filter = new InclusiveStopFilter(Bytes.toBytes(endRow));
                scan.setFilter(filter);
            } else {
                scan.setStopRow(Bytes.toBytes(endRow));
            }
        }
        scanner = this.env.getRegion().getScanner(scan);

        List<Cell> results = new ArrayList<>();
        boolean hasMore;
        DataProtos.DataQueryResponse.Builder responseBuilder = DataProtos.DataQueryResponse.newBuilder();
        do {
            hasMore = scanner.next(results);
            DataProtos.DataQueryResponse.Row.Builder rowBuilder = DataProtos.DataQueryResponse.Row.newBuilder();
            if (results.size() > 0) {
                rowBuilder.setRowKey(ByteString.copyFrom(results.get(0).getRow()));
                for (Cell kv : results) {
                    queryBuilder(rowBuilder, ByteString.copyFrom(kv.getFamily()), ByteString.copyFrom(kv.getQualifier()), ByteString.copyFrom(kv.getRow()), ByteString.copyFrom(kv.getValue()));
                }
            }
            responseBuilder.addRowList(rowBuilder);
            results.clear();
        } while (hasMore);
        response = responseBuilder.build();
    } catch (IOException ignored) {
        ResponseConverter.setControllerException(controller, ignored);
    } finally {
        if (scanner != null) {
            try {
                scanner.close();
            } catch (IOException e) {
                _log.error(ExceptionUtils.errorInfo(e));
            }
        }
    }
    done.run(response);
}