private List<Cell> getScanResult(byte[] startRow, byte[] stopRow, Table ht) throws IOException { Scan scan = new Scan(); scan.setMaxVersions(); if(!Bytes.toString(startRow).isEmpty()) { scan.setStartRow(startRow); } if(!Bytes.toString(stopRow).isEmpty()) { scan.setStopRow(stopRow); } ResultScanner scanner = ht.getScanner(scan); List<Cell> kvList = new ArrayList<>(); Result r; while ((r = scanner.next()) != null) { for (Cell kv : r.listCells()) { kvList.add(kv); } } scanner.close(); return kvList; }
public HBaseScanBasedIterator(Table table) { try { Scan scan = new Scan(); scan.setCaching(1000); ResultScanner scanner = table.getScanner(HBaseLookupRowEncoder.CF); scannerIterator = scanner.iterator(); } catch (IOException e) { logger.error("error when scan HBase", e); } }
private void scan0(Scan scan, ScanResultConsumer consumer) { try (ResultScanner scanner = getScanner(scan)) { consumer.onScanMetricsCreated(scanner.getScanMetrics()); for (Result result; (result = scanner.next()) != null;) { if (!consumer.onNext(result)) { break; } } consumer.onComplete(); } catch (IOException e) { consumer.onError(e); } }
private Result getSingleScanResult(Table ht, Scan scan) throws IOException { ResultScanner scanner = ht.getScanner(scan); Result result = scanner.next(); scanner.close(); return result; }
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(); } } }
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(); } }
@Override boolean testRow(final int i) throws IOException { Pair<byte[], byte[]> startAndStopRow = getStartAndStopRow(); Scan scan = new Scan().withStartRow(startAndStopRow.getFirst()) .withStopRow(startAndStopRow.getSecond()).setCaching(opts.caching) .setCacheBlocks(opts.cacheBlocks).setAsyncPrefetch(opts.asyncPrefetch) .setReadType(opts.scanReadType).setScanMetricsEnabled(true); for (int family = 0; family < opts.families; family++) { byte[] familyName = Bytes.toBytes(FAMILY_NAME_BASE + family); if (opts.addColumns) { for (int column = 0; column < opts.columns; column++) { byte [] qualifier = column == 0? COLUMN_ZERO: Bytes.toBytes("" + column); scan.addColumn(familyName, qualifier); ResultScanner s = this.table.getScanner(scan); try { for (; (r = s.next()) != null;) { updateValueSize(r); count++; Bytes.toString(startAndStopRow.getFirst()), Bytes.toString(startAndStopRow.getSecond()), count)); updateScanMetrics(s.getScanMetrics()); s.close();
@Test public void testBatchAllowPartial() throws IOException { int limit = 5; Scan scan = new Scan().setFilter(new ColumnCountOnRowFilter(3)).setBatch(2).setMaxResultSize(1) .setAllowPartialResults(true).setLimit(limit); try (Table table = UTIL.getConnection().getTable(TABLE_NAME); ResultScanner scanner = table.getScanner(scan)) { for (int i = 0; i < 3 * limit; i++) { int key = i / 3; Result result = scanner.next(); assertEquals(key, Bytes.toInt(result.getRow())); assertEquals(1, result.size()); assertTrue(result.mayHaveMoreCellsInRow()); int cqIndex = i % 3; assertEquals(key * (cqIndex + 1), Bytes.toInt(result.getValue(FAMILY, CQS[cqIndex]))); } assertNull(scanner.next()); } }
@Test public void testJira6912() throws Exception { final TableName tableName = TableName.valueOf(name.getMethodName()); Table foo = TEST_UTIL.createTable(tableName, new byte[][] {FAMILY}, 10); List<Put> puts = new ArrayList<Put>(); for (int i=0;i !=100; i++){ Put put = new Put(Bytes.toBytes(i)); put.addColumn(FAMILY, FAMILY, Bytes.toBytes(i)); puts.add(put); } foo.put(puts); // If i comment this out it works TEST_UTIL.flush(); Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(1)); scan.setStopRow(Bytes.toBytes(3)); scan.addColumn(FAMILY, FAMILY); scan.setFilter(new RowFilter(CompareOperator.NOT_EQUAL, new BinaryComparator(Bytes.toBytes(1)))); ResultScanner scanner = foo.getScanner(scan); Result[] bar = scanner.next(100); assertEquals(1, bar.length); }
private void assertResult(TableName tableName, int expectedRowCount) throws IOException { try (Table table = TEST_UTIL.getConnection().getTable(tableName)) { Scan scan = new Scan(); try (ResultScanner scanner = table.getScanner(scan)) { Result result; int rowCount = 0; while ((result = scanner.next()) != null) { assertArrayEquals(result.getRow(), Bytes.toBytes(rowCount)); assertArrayEquals(result.getValue(Bytes.toBytes("f1"), Bytes.toBytes("cq")), Bytes.toBytes(rowCount)); rowCount += 1; } assertEquals(rowCount, expectedRowCount); } } }
@Test public void testReadExpiredDataForRawScan() throws IOException { TableName tableName = TableName.valueOf(name.getMethodName()); long ts = System.currentTimeMillis() - 10000; byte[] value = Bytes.toBytes("expired"); try (Table table = TEST_UTIL.createTable(tableName, FAMILY)) { table.put(new Put(ROW).addColumn(FAMILY, QUALIFIER, ts, value)); assertArrayEquals(value, table.get(new Get(ROW)).getValue(FAMILY, QUALIFIER)); TEST_UTIL.getAdmin().modifyColumnFamily(tableName, new HColumnDescriptor(FAMILY).setTimeToLive(5)); try (ResultScanner scanner = table.getScanner(FAMILY)) { assertNull(scanner.next()); } try (ResultScanner scanner = table.getScanner(new Scan().setRaw(true))) { assertArrayEquals(value, scanner.next().getValue(FAMILY, QUALIFIER)); assertNull(scanner.next()); } } }
@Test public void testLabelsWithCheckAndPut() throws Throwable { TableName tableName = TableName.valueOf(TEST_NAME.getMethodName()); try (Table table = TEST_UTIL.createTable(tableName, fam)) { byte[] row1 = Bytes.toBytes("row1"); Put put = new Put(row1); put.addColumn(fam, qual, HConstants.LATEST_TIMESTAMP, value); put.setCellVisibility(new CellVisibility(SECRET + " & " + CONFIDENTIAL)); table.checkAndMutate(row1, fam).qualifier(qual).ifNotExists().thenPut(put); byte[] row2 = Bytes.toBytes("row2"); put = new Put(row2); put.addColumn(fam, qual, HConstants.LATEST_TIMESTAMP, value); put.setCellVisibility(new CellVisibility(SECRET)); table.checkAndMutate(row2, fam).qualifier(qual).ifNotExists().thenPut(put); Scan scan = new Scan(); scan.setAuthorizations(new Authorizations(SECRET)); ResultScanner scanner = table.getScanner(scan); Result result = scanner.next(); assertTrue(!result.isEmpty()); assertTrue(Bytes.equals(row2, result.getRow())); result = scanner.next(); assertNull(result); } }
@Test public void testCrossRowAtomicInRegion() throws IOException, InterruptedException { put(Bytes.toBytes("row1"), CQ1, Bytes.toBytes(1)); put(Bytes.toBytes("row2"), CQ1, Bytes.toBytes(2)); try (Table table = CONN.getTable(tableName); ResultScanner scanner = table.getScanner(new Scan().setCaching(1))) { Result result = scanner.next(); assertArrayEquals(Bytes.toBytes("row1"), result.getRow()); assertEquals(1, Bytes.toInt(result.getValue(CF, CQ1))); move(); put(Bytes.toBytes("row3"), CQ1, Bytes.toBytes(3)); result = scanner.next(); assertArrayEquals(Bytes.toBytes("row2"), result.getRow()); assertEquals(2, Bytes.toInt(result.getValue(CF, CQ1))); assertNull(scanner.next()); } } }
static private void removeTablePermissions(TableName tableName, byte[] column, Table table, boolean closeTable) throws IOException { Scan scan = new Scan(); scan.addFamily(ACL_LIST_FAMILY); String columnName = Bytes.toString(column); scan.setFilter(new QualifierFilter(CompareOperator.EQUAL, new RegexStringComparator( String.format("(%s%s%s)|(%s%s)$", ACL_KEY_DELIMITER, columnName, ACL_KEY_DELIMITER, ACL_KEY_DELIMITER, columnName)))); Set<byte[]> qualifierSet = new TreeSet<>(Bytes.BYTES_COMPARATOR); ResultScanner scanner = null; try { scanner = table.getScanner(scan); for (Result res : scanner) { for (byte[] q : res.getFamilyMap(ACL_LIST_FAMILY).navigableKeySet()) { qualifierSet.add(q); } } if (qualifierSet.size() > 0) { Delete d = new Delete(tableName.getName()); for (byte[] qualifier : qualifierSet) { d.addColumns(ACL_LIST_FAMILY, qualifier); } table.delete(d); } } finally { if (scanner != null) scanner.close(); if (closeTable) table.close(); } }
@Test public void test() throws IOException { try (Table t = UTIL.getConnection().getTable(NAME)) { writeData(t); Scan s = new Scan(); s.addColumn(FAMILY, EXPLICIT_QUAL); try (ResultScanner scanner = t.getScanner(s)) { for (int i = 0; i < NUM_ROWS; i++) { Result result = scanner.next(); assertNotNull("The " + (i + 1) + "th result was unexpectedly null", result); assertEquals(2, result.getFamilyMap(FAMILY).size()); assertArrayEquals(Bytes.toBytes(i + 1), result.getRow()); assertArrayEquals(EXPLICIT_VAL, result.getValue(FAMILY, EXPLICIT_QUAL)); assertArrayEquals(IMPLICIT_VAL, result.getValue(FAMILY, IMPLICIT_QUAL)); } assertNull(scanner.next()); } } } }
@Test public void testSimpleVisibilityLabels() throws Exception { TableName tableName = TableName.valueOf(TEST_NAME.getMethodName()); try (Table table = createTableAndWriteDataWithLabels(tableName, SECRET + "|" + CONFIDENTIAL, PRIVATE + "|" + CONFIDENTIAL)) { Scan s = new Scan(); s.setAuthorizations(new Authorizations(SECRET, CONFIDENTIAL, PRIVATE)); ResultScanner scanner = table.getScanner(s); Result[] next = scanner.next(3); assertTrue(next.length == 2); CellScanner cellScanner = next[0].cellScanner(); cellScanner.advance(); Cell current = cellScanner.current(); assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(), row1, 0, row1.length)); cellScanner = next[1].cellScanner(); cellScanner.advance(); current = cellScanner.current(); assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(), row2, 0, row2.length)); } }
private void testSmallReversedScanInternal(String[] inputRowKeys) throws IOException { int rowCount = inputRowKeys.length; for (int i = 0; i < rowCount; i++) { Put put = new Put(Bytes.toBytes(inputRowKeys[i])); put.addColumn(COLUMN_FAMILY, null, Bytes.toBytes(i)); htable.put(put); } Scan scan = new Scan(); scan.setReversed(true); scan.setSmall(true); ResultScanner scanner = htable.getScanner(scan); Result r; int value = rowCount; while ((r = scanner.next()) != null) { Assert.assertArrayEquals(r.getValue(COLUMN_FAMILY, null), Bytes.toBytes(--value)); Assert.assertArrayEquals(r.getRow(), Bytes.toBytes(inputRowKeys[value])); } Assert.assertEquals(0, value); }
@Test public void testFiltersWithOR() throws Exception { TableName tn = TableName.valueOf(name.getMethodName()); Table table = TEST_UTIL.createTable(tn, new String[] { "cf1", "cf2" }); byte[] CF1 = Bytes.toBytes("cf1"); byte[] CF2 = Bytes.toBytes("cf2"); Put put1 = new Put(Bytes.toBytes("0")); put1.addColumn(CF1, Bytes.toBytes("col_a"), Bytes.toBytes(0)); table.put(put1); Put put2 = new Put(Bytes.toBytes("0")); put2.addColumn(CF2, Bytes.toBytes("col_b"), Bytes.toBytes(0)); table.put(put2); FamilyFilter filterCF1 = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(CF1)); FamilyFilter filterCF2 = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(CF2)); FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE); filterList.addFilter(filterCF1); filterList.addFilter(filterCF2); Scan scan = new Scan(); scan.setFilter(filterList); ResultScanner scanner = table.getScanner(scan); LOG.info("Filter list: " + filterList); for (Result rr = scanner.next(); rr != null; rr = scanner.next()) { Assert.assertEquals(2, rr.size()); } }
@Test public void testRowAtomic() throws IOException, InterruptedException { byte[] row = Bytes.toBytes("row"); put(row, CQ1, Bytes.toBytes(1)); put(row, CQ2, Bytes.toBytes(2)); try (Table table = CONN.getTable(tableName); ResultScanner scanner = table.getScanner(new Scan().setBatch(1).setCaching(1))) { Result result = scanner.next(); assertEquals(1, result.rawCells().length); assertEquals(1, Bytes.toInt(result.getValue(CF, CQ1))); move(); put(row, CQ3, Bytes.toBytes(3)); result = scanner.next(); assertEquals(1, result.rawCells().length); assertEquals(2, Bytes.toInt(result.getValue(CF, CQ2))); assertNull(scanner.next()); } }
@Override public Void run() throws Exception { Scan s = new Scan(); s.setAuthorizations(new Authorizations(SECRET, CONFIDENTIAL)); try (Connection connection = ConnectionFactory.createConnection(conf); Table t = connection.getTable(table.getName())) { ResultScanner scanner = t.getScanner(s); Result result = scanner.next(); assertTrue(!result.isEmpty()); assertTrue(Bytes.equals(Bytes.toBytes("row2"), result.getRow())); result = scanner.next(); assertNull(result); } return null; } };