Refine search
private void loadChunkFromHBase() { Map<byte[], byte[]> chunk = new TreeMap<>(UnsignedBytes.lexicographicalComparator()); try { ResultScanner resultScanner = hbaseClient.scan(cursorKey, endScanKey); Result[] results = resultScanner.next(chunkSize); for (Result result : results) { byte[] columnKey = extractStateKeyFromRowKey(result.getRow()); byte[] columnValue = result.getValue(columnFamily, STATE_QUALIFIER); chunk.put(columnKey, columnValue); } if (results.length > 0) { byte[] lastRow = results[results.length - 1].getRow(); cursorKey = advanceRow(lastRow); } cachedResultIterator = chunk.entrySet().iterator(); } catch (Exception e) { throw new RuntimeException("Fail to scan from HBase state storage.", e); } }
/** * Reads all quota snapshots from the quota table. * * @return The current "view" of space use by each table. */ public Map<TableName, SpaceQuotaSnapshot> fetchSnapshotsFromQuotaTable() throws IOException { try (Table quotaTable = getConnection().getTable(QuotaUtil.QUOTA_TABLE_NAME); ResultScanner scanner = quotaTable.getScanner(QuotaTableUtil.makeQuotaSnapshotScan())) { Map<TableName,SpaceQuotaSnapshot> snapshots = new HashMap<>(); for (Result result : scanner) { try { extractQuotaSnapshot(result, snapshots); } catch (IllegalArgumentException e) { final String msg = "Failed to parse result for row " + Bytes.toString(result.getRow()); LOG.error(msg, e); throw new IOException(msg, e); } } return snapshots; } }
/** * Load all permissions from the region server holding {@code _acl_}, * primarily intended for testing purposes. */ static Map<byte[], ListMultimap<String, UserPermission>> loadAll( Configuration conf) throws IOException { Map<byte[], ListMultimap<String, UserPermission>> allPerms = new TreeMap<>(Bytes.BYTES_RAWCOMPARATOR); // do a full scan of _acl_, filtering on only first table region rows Scan scan = new Scan(); scan.addFamily(ACL_LIST_FAMILY); ResultScanner scanner = null; // TODO: Pass in a Connection rather than create one each time. try (Connection connection = ConnectionFactory.createConnection(conf)) { try (Table table = connection.getTable(ACL_TABLE_NAME)) { scanner = table.getScanner(scan); try { for (Result row : scanner) { ListMultimap<String, UserPermission> resultPerms = parsePermissions(row.getRow(), row, null, null, null, false); allPerms.put(row.getRow(), resultPerms); } } finally { if (scanner != null) scanner.close(); } } } return allPerms; }
public void doScan(int n, boolean caching) throws IOException { Scan scan = new Scan(); if (caching) { scan.setCaching(n); } else { scan.setCaching(1); } ResultScanner scanner = table.getScanner(scan); for (int i = 0; i < n; i++) { Result res = scanner.next(); LOG.debug("Result row: " + Bytes.toString(res.getRow()) + ", value: " + Bytes.toString(res.getValue(cf, qualifier))); } }
@Test public void test() throws Throwable { RenewLeaseConsumer consumer = new RenewLeaseConsumer(); TABLE.scan(new Scan(), consumer); List<Result> results = consumer.get(); // should not see the newly added value assertEquals(10, results.size()); IntStream.range(0, 10).forEach(i -> { Result result = results.get(i); assertEquals(String.format("%02d", i), Bytes.toString(result.getRow())); assertEquals(i, Bytes.toInt(result.getValue(FAMILY, CQ))); }); // now we can see the newly added value List<Result> results2 = TABLE.scanAll(new Scan()).get(); assertEquals(11, results2.size()); IntStream.range(0, 11).forEach(i -> { Result result = results2.get(i); assertEquals(String.format("%02d", i), Bytes.toString(result.getRow())); assertEquals(i, Bytes.toInt(result.getValue(FAMILY, CQ))); }); } }
@Test public void testSizeLimit() throws IOException { try (ResultScanner scanner = TEST_UTIL.getConnection().getTable(TABLE_NAME).getScanner(createScanWithSizeLimit())) { int num = 0; Result r; while ((r = scanner.next()) != null) { if (num % (NUM_FAMILIES * NUM_QUALIFIERS) != (NUM_FAMILIES * NUM_QUALIFIERS) - 1) { assertTrue(r.isCursor()); assertArrayEquals(ROWS[num / NUM_FAMILIES / NUM_QUALIFIERS], r.getCursor().getRow()); } else { assertFalse(r.isCursor()); assertArrayEquals(ROWS[num / NUM_FAMILIES / NUM_QUALIFIERS], r.getRow()); } num++; } } } }
private boolean peerHasAllNormalRows() throws IOException { try (ResultScanner scanner = htable2.getScanner(new Scan())) { Result[] results = scanner.next(ROWS_COUNT); if (results.length != ROWS_COUNT) { return false; } for (int i = 0; i < results.length; i++) { Assert.assertArrayEquals(generateRowKey(i), results[i].getRow()); } return true; } }
Scan scan = new Scan(); int limit = 20; conn = ConnectionFactory.createConnection(hconf); table = conn.getTable(TableName.valueOf(hbaseTable)); scanner = table.getScanner(scan); int count = 0; for (Result r : scanner) { byte[] rowkey = r.getRow(); System.out.println(Bytes.toStringBinary(rowkey)); count++;
@Test public void testBatchDoNotAllowPartial() throws InterruptedException, ExecutionException { // we set batch to 2 and max result size to 1, then server will only returns one result per call // but we should get 2 + 1 for every row. List<Result> results = TABLE.scanAll(new Scan().setBatch(2).setMaxResultSize(1)).get(); assertEquals(2 * COUNT, results.size()); for (int i = 0; i < COUNT; i++) { Result firstTwo = results.get(2 * i); assertEquals(String.format("%02d", i), Bytes.toString(firstTwo.getRow())); assertEquals(2, firstTwo.size()); assertEquals(i, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[0]))); assertEquals(2 * i, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[1]))); Result secondOne = results.get(2 * i + 1); assertEquals(String.format("%02d", i), Bytes.toString(secondOne.getRow())); assertEquals(1, secondOne.size()); assertEquals(3 * i, Bytes.toInt(secondOne.getValue(FAMILY, CQS[2]))); } }
public T nextRecord(T reuse) throws IOException { if (resultScanner == null) { throw new IOException("No table result scanner provided!"); } try { Result res = resultScanner.next(); if (res != null) { scannedRows++; currentRow = res.getRow(); return mapResultToOutType(res); } } catch (Exception e) { resultScanner.close(); //workaround for timeout on scan LOG.warn("Error after scan of " + scannedRows + " rows. Retry with a new scanner...", e); scan.setStartRow(currentRow); resultScanner = table.getScanner(scan); Result res = resultScanner.next(); if (res != null) { scannedRows++; currentRow = res.getRow(); return mapResultToOutType(res); } } endReached = true; return null; }
Result[] results = scanner.next(1); assertNotNull(results); assertEquals(1, results.length); assertTrue(Bytes.equals(ROW_1, results[0].getRow())); Result result = scanner.next(); assertNotNull(result); assertTrue(Bytes.equals(ROW_2, result.getRow())); results = scanner.next(2); assertNotNull(results); assertEquals(2, results.length); assertTrue(Bytes.equals(ROW_3, results[0].getRow())); assertTrue(Bytes.equals(ROW_4, results[1].getRow())); assertNotNull(results); assertEquals(4, results.length); assertTrue(Bytes.equals(ROW_1, results[0].getRow())); assertTrue(Bytes.equals(ROW_2, results[1].getRow())); assertTrue(Bytes.equals(ROW_3, results[2].getRow())); assertTrue(Bytes.equals(ROW_4, results[3].getRow())); assertNotNull(results); assertEquals(4, results.length); assertTrue(Bytes.equals(ROW_1, results[0].getRow())); assertTrue(Bytes.equals(ROW_2, results[1].getRow())); assertTrue(Bytes.equals(ROW_3, results[2].getRow())); assertTrue(Bytes.equals(ROW_4, results[3].getRow())); scanner.close();
public void deleteStatsForRegion(Region region, StatisticsCollector tracker, ImmutableBytesPtr fam, List<Mutation> mutations) throws IOException { long timeStamp = clientTimeStamp == DefaultStatisticsCollector.NO_TIMESTAMP ? tracker.getMaxTimeStamp() : clientTimeStamp; byte[] startKey = region.getRegionInfo().getStartKey(); byte[] stopKey = region.getRegionInfo().getEndKey(); List<Result> statsForRegion = new ArrayList<Result>(); Scan s = MetaDataUtil.newTableRowsScan(getAdjustedKey(startKey, tableName, fam, false), getAdjustedKey(stopKey, tableName, fam, true), MetaDataProtocol.MIN_TABLE_TIMESTAMP, clientTimeStamp); s.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_BYTES); try (ResultScanner scanner = statsWriterTable.getScanner(s)) { Result result = null; while ((result = scanner.next()) != null) { statsForRegion.add(result); } } for (Result result : statsForRegion) { mutations.add(new Delete(result.getRow(), timeStamp - 1)); } }
/** * There may be items for this table still up in hbase:meta in the case where the * info:regioninfo column was empty because of some write error. Remove ALL rows from hbase:meta * that have to do with this table. See HBASE-12980. */ private static void cleanAnyRemainingRows(final MasterProcedureEnv env, final TableName tableName) throws IOException { Connection connection = env.getMasterServices().getConnection(); Scan tableScan = MetaTableAccessor.getScanForTableName(connection, tableName); try (Table metaTable = connection.getTable(TableName.META_TABLE_NAME)) { List<Delete> deletes = new ArrayList<>(); try (ResultScanner resScanner = metaTable.getScanner(tableScan)) { for (Result result : resScanner) { deletes.add(new Delete(result.getRow())); } } if (!deletes.isEmpty()) { LOG.warn("Deleting some vestigial " + deletes.size() + " rows of " + tableName + " from " + TableName.META_TABLE_NAME); metaTable.delete(deletes); } } }
return; Scan scan = new Scan(); scan.setAuthorizations(new Authorizations(VisibilityUtils.SYSTEM_LABEL)); scan.addColumn(LABELS_TABLE_FAMILY, LABEL_QUALIFIER); ResultScanner scanner = null; try { scanner = labelsTable.getScanner(scan); Result next = null; while ((next = scanner.next()) != null) { byte[] row = next.getRow(); byte[] value = next.getValue(LABELS_TABLE_FAMILY, LABEL_QUALIFIER); labels.put(Bytes.toString(value), Bytes.toInt(row));
/** * Return an md5 digest of the entire contents of a table. */ public String checksumRows(final Table table) throws Exception { Scan scan = new Scan(); ResultScanner results = table.getScanner(scan); MessageDigest digest = MessageDigest.getInstance("MD5"); for (Result res : results) { digest.update(res.getRow()); } results.close(); return digest.toString(); }
scan.addColumn(TABLE_FAMILY_BYTES, TABLE_TYPE_BYTES); List<TableInfo> tableInfoList = Lists.newArrayList(); try (ResultScanner scanner = systemCatalog.getScanner(scan)) { for (Result result = scanner.next(); (result != null); result = scanner.next()) { byte[][] rowKeyMetaData = new byte[5][]; byte[] viewTenantId = null; getVarChars(result.getRow(), 5, rowKeyMetaData); if (linkType==PTable.LinkType.PARENT_TABLE) { viewTenantId = result.getValue(TABLE_FAMILY_BYTES, PARENT_TENANT_ID_BYTES);
private SpaceQuotaSnapshot getSnapshotForTable( Connection conn, TableName tn) throws IOException { try (Table quotaTable = conn.getTable(QuotaUtil.QUOTA_TABLE_NAME); ResultScanner scanner = quotaTable.getScanner(QuotaTableUtil.makeQuotaSnapshotScan())) { Map<TableName,SpaceQuotaSnapshot> activeViolations = new HashMap<>(); for (Result result : scanner) { try { QuotaTableUtil.extractQuotaSnapshot(result, activeViolations); } catch (IllegalArgumentException e) { final String msg = "Failed to parse result for row " + Bytes.toString(result.getRow()); LOG.error(msg, e); throw new IOException(msg, e); } } return activeViolations.get(tn); } }