private Result getSingleScanResult(Table ht, Scan scan) throws IOException { ResultScanner scanner = ht.getScanner(scan); Result result = scanner.next(); scanner.close(); return result; }
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; }
private void verifyRowCount(Table table, int expectedRegionNum) throws IOException { ResultScanner scanner = table.getScanner(new Scan()); int rowCount = 0; while (scanner.next() != null) { rowCount++; } assertEquals(expectedRegionNum, rowCount); scanner.close(); }
@Test public void testSerDeViolationPolicies() throws Exception { final TableName tn1 = getUniqueTableName(); final SpaceQuotaSnapshot snapshot1 = new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.DISABLE), 512L, 1024L); final TableName tn2 = getUniqueTableName(); final SpaceQuotaSnapshot snapshot2 = new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.NO_INSERTS), 512L, 1024L); final TableName tn3 = getUniqueTableName(); final SpaceQuotaSnapshot snapshot3 = new SpaceQuotaSnapshot( new SpaceQuotaStatus(SpaceViolationPolicy.NO_WRITES), 512L, 1024L); List<Put> puts = new ArrayList<>(); puts.add(QuotaTableUtil.createPutForSpaceSnapshot(tn1, snapshot1)); puts.add(QuotaTableUtil.createPutForSpaceSnapshot(tn2, snapshot2)); puts.add(QuotaTableUtil.createPutForSpaceSnapshot(tn3, snapshot3)); final Map<TableName,SpaceQuotaSnapshot> expectedPolicies = new HashMap<>(); expectedPolicies.put(tn1, snapshot1); expectedPolicies.put(tn2, snapshot2); expectedPolicies.put(tn3, snapshot3); final Map<TableName,SpaceQuotaSnapshot> actualPolicies = new HashMap<>(); try (Table quotaTable = connection.getTable(QuotaUtil.QUOTA_TABLE_NAME)) { quotaTable.put(puts); ResultScanner scanner = quotaTable.getScanner(QuotaTableUtil.makeQuotaSnapshotScan()); for (Result r : scanner) { QuotaTableUtil.extractQuotaSnapshot(r, actualPolicies); } scanner.close(); } assertEquals(expectedPolicies, actualPolicies); }
/** * 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(); }
private void verify(Scan scan) throws IOException { ResultScanner scanner = htable.getScanner(scan); Iterator<Result> it = scanner.iterator(); /* Then */ int count = 0; try { while (it.hasNext()) { it.next(); count++; } } finally { scanner.close(); } assertEquals(expected, count); } /**
private void scan(Table table) throws IOException { Scan scan = new Scan(); scan.setCaching(1); scan.setCacheBlocks(false); ResultScanner scanner = table.getScanner(scan); try { scanner.next(); } finally { scanner.close(); } } }
/** * Count the number of keyvalue in the table. Scans all possible versions * @param table table to scan * @return number of keyvalues over all rows in the table * @throws IOException */ private int getKeyValueCount(Table table) throws IOException { Scan scan = new Scan(); scan.setMaxVersions(Integer.MAX_VALUE - 1); ResultScanner results = table.getScanner(scan); int count = 0; for (Result res : results) { count += res.listCells().size(); System.out.println(count + ") " + res); } results.close(); return count; } }
int doScan(Table table, Scan scan, int expectedCount) throws IOException { ResultScanner results = table.getScanner(scan); results.next(); results.close(); return verifyCount(expectedCount); }
/** * Exhausts the scanner by calling next repetitively. Once completely exhausted, close scanner and * return total cell count * @param scanner the scanner to exhaust * @return the number of cells counted * @throws Exception if there is a problem retrieving cells from the scanner */ private int countCellsFromScanner(ResultScanner scanner) throws Exception { Result result = null; int numCells = 0; while ((result = scanner.next()) != null) { numCells += result.rawCells().length; } scanner.close(); return numCells; }
public static void assertCellsValue(Table table, Scan scan, byte[] expectedValue, int expectedCount) throws IOException { ResultScanner results = table.getScanner(scan); int count = 0; for (Result res : results) { List<Cell> cells = res.listCells(); for(Cell cell : cells) { // Verify the value Assert.assertArrayEquals(expectedValue, CellUtil.cloneValue(cell)); count++; } } results.close(); Assert.assertEquals(expectedCount, count); }
protected static void waitForReplication(int expectedRows, int retries) throws IOException, InterruptedException { Scan scan; for (int i = 0; i < retries; i++) { scan = new Scan(); if (i== retries -1) { fail("Waited too much time for normal batch replication"); } ResultScanner scanner = htable2.getScanner(scan); Result[] res = scanner.next(expectedRows); scanner.close(); if (res.length != expectedRows) { LOG.info("Only got " + res.length + " rows"); Thread.sleep(SLEEP_TIME); } else { break; } } }
/** * Count the number of keyvalues in the specified table with the given filter * @param table the table to scan * @return the number of keyvalues found * @throws IOException */ private int getCount(Table table, Filter filter) throws IOException { Scan scan = new Scan(); scan.setFilter(filter); ResultScanner results = table.getScanner(scan); int count = 0; for (Result res : results) { count += res.size(); } results.close(); return count; }
private int getResultsSize(Table ht, Scan scan) throws IOException { ResultScanner scanner = ht.getScanner(scan); List<Cell> results = new ArrayList<>(); Result r; while ((r = scanner.next()) != null) { for (Cell kv : r.listCells()) { results.add(kv); } } scanner.close(); return results.size(); } }
private void putDataAndVerify(Table table, String row, byte[] family, String value, int verifyNum) throws IOException { System.out.println("=========Putting data :" + row); Put put = new Put(Bytes.toBytes(row)); put.addColumn(family, Bytes.toBytes("q1"), Bytes.toBytes(value)); table.put(put); ResultScanner resultScanner = table.getScanner(new Scan()); List<Result> results = new ArrayList<>(); while (true) { Result r = resultScanner.next(); if (r == null) break; results.add(r); } resultScanner.close(); if (results.size() != verifyNum) { System.out.println(results); } assertEquals(verifyNum, results.size()); }
/** * Provide an existing table name to truncate. * Scans the table and issues a delete for each row read. * @param tableName existing table * @return HTable to that new table * @throws IOException */ public Table deleteTableData(TableName tableName) throws IOException { Table table = getConnection().getTable(tableName); Scan scan = new Scan(); ResultScanner resScan = table.getScanner(scan); for(Result res : resScan) { Delete del = new Delete(res.getRow()); table.delete(del); } resScan = table.getScanner(scan); resScan.close(); return table; }
@Override public List<TResult> getScannerResults(ByteBuffer table, TScan scan, int numRows) throws TIOError, TException { Table htable = getTable(table); List<TResult> results = null; ResultScanner scanner = null; try { scanner = htable.getScanner(scanFromThrift(scan)); results = resultsFromHBase(scanner.next(numRows)); } catch (IOException e) { throw getTIOError(e); } finally { if (scanner != null) { scanner.close(); } closeTable(htable); } return results; }
@Test public void testDontThrowUnknowScannerExceptionToClient() throws Exception { Table table = createTestTable(TableName.valueOf(name.getMethodName()), ROWS, FAMILIES, QUALIFIERS, VALUE); Scan scan = new Scan(); scan.setCaching(1); ResultScanner scanner = table.getScanner(scan); scanner.next(); Thread.sleep(timeout * 2); int count = 1; while (scanner.next() != null) { count++; } assertEquals(NUM_ROWS, count); scanner.close(); }
/** * Returns all rows from the hbase:meta table. * * @throws IOException When reading the rows fails. */ public List<byte[]> getMetaTableRows() throws IOException { // TODO: Redo using MetaTableAccessor class Table t = getConnection().getTable(TableName.META_TABLE_NAME); List<byte[]> rows = new ArrayList<>(); ResultScanner s = t.getScanner(new Scan()); for (Result result : s) { LOG.info("getMetaTableRows: row -> " + Bytes.toStringBinary(result.getRow())); rows.add(result.getRow()); } s.close(); t.close(); return rows; }
@Override void testRow(int i) throws IOException { byte[] value = generateValue(this.rand); Scan scan = constructScan(value); ResultScanner scanner = null; try { scanner = this.table.getScanner(scan); while (scanner.next() != null) { } } finally { if (scanner != null) scanner.close(); } }