/** * Creates Scan operation to load backup set list * @return scan operation */ private Scan createScanForBackupSetList() { Scan scan = new Scan(); byte[] startRow = Bytes.toBytes(SET_KEY_PREFIX); byte[] stopRow = Arrays.copyOf(startRow, startRow.length); stopRow[stopRow.length - 1] = (byte) (stopRow[stopRow.length - 1] + 1); scan.setStartRow(startRow); scan.setStopRow(stopRow); scan.addFamily(BackupSystemTable.META_FAMILY); return scan; }
/** * Tries to scan a row from passed region */ private void isSuccessfulScan(RegionInfo region) throws IOException { Scan scan = new Scan().withStartRow(region.getStartKey()).setRaw(true).setOneRowLimit() .setMaxResultSize(1L).setCaching(1).setFilter(new FirstKeyOnlyFilter()) .setCacheBlocks(false); try (Table table = conn.getTable(region.getTable()); ResultScanner scanner = table.getScanner(scan)) { scanner.next(); } catch (IOException e) { LOG.error("Could not scan region:" + region.getEncodedName(), e); throw e; } }
/** * Create a Scan operation for the range of rows specified. * @param startRow row to start scanner at or after (inclusive) * @param stopRow row to stop scanner before (exclusive) * @deprecated use {@code new Scan().withStartRow(startRow).withStopRow(stopRow)} instead. */ @Deprecated public Scan(byte[] startRow, byte[] stopRow) { setStartRow(startRow); setStopRow(stopRow); }
@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()); } }
@Override boolean testRow(final int i) throws IOException { if (this.testScanner == null) { Scan scan = new Scan().withStartRow(format(opts.startRow)).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); } } else { scan.addFamily(familyName); } } if (opts.filterAll) { scan.setFilter(new FilterAllFilter()); } this.testScanner = table.getScanner(scan); } Result r = testScanner.next(); updateValueSize(r); return true; } }
@Test public void test() throws IOException, InterruptedException { try (Table t = UTIL.getConnection().getTable(NAME)) { writeData(t); // Flush the data UTIL.flush(NAME); // Issue a compaction UTIL.compact(NAME, true); Scan s = new Scan(); s.addColumn(FAMILY, QUALIFIER); 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(1, result.getFamilyMap(FAMILY).size()); assertArrayEquals(Bytes.toBytes(i + 1), result.getRow()); assertArrayEquals(Bytes.toBytes(replacedValue), result.getValue(FAMILY, QUALIFIER)); } 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 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); } } return kvList; }
@Test public void testMultiRowRangeFilterWithEmptyStopRow() throws IOException { tableName = TableName.valueOf(name.getMethodName()); Table ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE); generateRows(numRows, ht, family, qf, value); Scan scan = new Scan(); scan.setMaxVersions(); List<RowRange> ranges = new ArrayList<>(); ranges.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(""), false)); ranges.add(new RowRange(Bytes.toBytes(30), true, Bytes.toBytes(40), false)); MultiRowRangeFilter filter = new MultiRowRangeFilter(ranges); scan.setFilter(filter); int resultsSize = getResultsSize(ht, scan); List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(""), ht); assertEquals(results1.size(), resultsSize); ht.close(); }
@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); } }
Scan initScan() throws IOException { Scan scan = new Scan(); scan.setCacheBlocks(false); if (startTime != 0 || endTime != 0) { scan.setTimeRange(startTime, endTime == 0 ? HConstants.LATEST_TIMESTAMP : endTime); } if (scanBatch > 0) { scan.setBatch(scanBatch); } if (versions >= 0) { scan.setMaxVersions(versions); } if (!isTableStartRow(startRow)) { scan.setStartRow(startRow); } if (!isTableEndRow(stopRow)) { scan.setStopRow(stopRow); } if(families != null) { for(String fam : families.split(",")) { scan.addFamily(Bytes.toBytes(fam)); } } return scan; }
private void checkTableRows(final Table table, int numRows) throws Exception { Scan scan = new Scan(); scan.setCaching(1); scan.setCacheBlocks(false); ResultScanner scanner = table.getScanner(scan); try { int count = 0; for (int i = 0; i < numRows; ++i) { byte[] row = Bytes.toBytes(String.format("%09d", i)); Result result = scanner.next(); assertTrue(result != null); assertTrue(Bytes.equals(row, result.getRow())); count++; } while (true) { Result result = scanner.next(); if (result == null) break; count++; } assertEquals(numRows, count); } finally { scanner.close(); } } }
@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()); } } }
@Override public List<Cell> run() throws Exception { Scan scan = new Scan(); scan.setStartRow(TEST_ROW); scan.setStopRow(Bytes.add(TEST_ROW, new byte[]{ 0 } )); scan.addFamily(TEST_FAMILY); Connection connection = ConnectionFactory.createConnection(conf); Table t = connection.getTable(TEST_TABLE.getTableName()); try { ResultScanner scanner = t.getScanner(scan); Result result = null; do { result = scanner.next(); if (result != null) { scanResults.addAll(result.listCells()); } } while (result != null); } finally { t.close(); connection.close(); } return scanResults; } };
@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; } };
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))); } }
private void verify(final Table table) throws IOException { Scan scan = new Scan(); scan.addColumn(FAMILY_NAME, COLUMN_NAME); scan.setMaxVersions(1); ResultScanner scanner = table.getScanner(scan); for (Result r: scanner) { for (Cell kv : r.listCells()) { log.debug(Bytes.toString(r.getRow()) + "\t" + Bytes.toString(CellUtil.cloneFamily(kv)) + "\t" + Bytes.toString(CellUtil.cloneQualifier(kv)) + "\t" + kv.getTimestamp() + "\t" + Bytes.toBoolean(CellUtil.cloneValue(kv))); org.junit.Assert.assertEquals(TIMESTAMP.get(kv.getTimestamp()), Bytes.toBoolean(CellUtil.cloneValue(kv))); } } scanner.close(); }