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))); } }
@Override public String[] getRow(Array<String> key) { byte[] encodedKey = encoder.encodeRowKey(key.data); Get get = new Get(encodedKey); try { Result result = table.get(get); if (result.isEmpty()) { return null; } return encoder.decode(new HBaseRow(result.getRow(), result.getFamilyMap(HBaseLookupRowEncoder.CF))); } catch (IOException e) { throw new RuntimeException("error when get row from hBase", e); } }
private int cellsCount(Table table, Filter filter) throws IOException { Scan scan = new Scan().setFilter(filter).readAllVersions(); try (ResultScanner scanner = table.getScanner(scan)) { List<Cell> results = new ArrayList<>(); Result result; while ((result = scanner.next()) != null) { result.listCells().forEach(results::add); } return results.size(); } }
@Override public String[] next() { counter ++; if (counter % 100000 == 0) { logger.info("scanned {} rows from hBase", counter); } Result result = scannerIterator.next(); byte[] rowKey = result.getRow(); NavigableMap<byte[], byte[]> qualifierValMap = result.getFamilyMap(HBaseLookupRowEncoder.CF); return encoder.decode(new HBaseRow(rowKey, qualifierValMap)); }
@Test public void testCompleteResult() throws IOException { int limit = 5; Scan scan = new Scan().setFilter(new ColumnCountOnRowFilter(2)).setMaxResultSize(1).setLimit(limit); try (Table table = UTIL.getConnection().getTable(TABLE_NAME); ResultScanner scanner = table.getScanner(scan)) { for (int i = 0; i < limit; i++) { Result result = scanner.next(); assertEquals(i, Bytes.toInt(result.getRow())); assertEquals(2, result.size()); assertFalse(result.mayHaveMoreCellsInRow()); assertEquals(i, Bytes.toInt(result.getValue(FAMILY, CQS[0]))); assertEquals(2 * i, Bytes.toInt(result.getValue(FAMILY, CQS[1]))); } assertNull(scanner.next()); } }
@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 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()); } } }
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 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(); } }
private void getOneRowAndAssertAllExist(final Table table) throws IOException { Get get = new Get(ROWKEY); Result result = table.get(get); assertTrue("Column A value should be a", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("A"))).equals("a")); assertTrue("Column B value should be b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))).equals("b")); assertTrue("Column C value should be c", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("C"))).equals("c")); }
@Test public void testIncrementWithDeletes() throws Exception { LOG.info("Starting " + this.name.getMethodName()); final TableName TABLENAME = TableName.valueOf(filterStringSoTableNameSafe(this.name.getMethodName())); Table ht = TEST_UTIL.createTable(TABLENAME, FAMILY); final byte[] COLUMN = Bytes.toBytes("column"); ht.incrementColumnValue(ROW, FAMILY, COLUMN, 5); TEST_UTIL.flush(TABLENAME); Delete del = new Delete(ROW); ht.delete(del); ht.incrementColumnValue(ROW, FAMILY, COLUMN, 5); Get get = new Get(ROW); Result r = ht.get(get); assertEquals(1, r.size()); assertEquals(5, Bytes.toLong(r.getValue(FAMILY, COLUMN))); }
@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; } };
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(); }
@Test public void testScanWithColumnsAndFilterAndVersion() throws IOException { TableName tableName = TableName.valueOf(name.getMethodName()); try (Table table = TEST_UTIL.createTable(tableName, FAMILY, 4)) { for (int i = 0; i < 4; i++) { Put put = new Put(ROW); put.addColumn(FAMILY, QUALIFIER, VALUE); table.put(put); } Scan scan = new Scan(); scan.addColumn(FAMILY, QUALIFIER); scan.setFilter(new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator(QUALIFIER))); scan.readVersions(3); try (ResultScanner scanner = table.getScanner(scan)) { Result result = scanner.next(); assertEquals(3, result.size()); } } }
@Test public void testFlush() throws IOException { byte[] row = Bytes.toBytes("row"); byte[] cf = Bytes.toBytes("cf"); byte[] cq = Bytes.toBytes("cq"); byte[] value = Bytes.toBytes("value"); TableName name = TableName.valueOf(getClass().getSimpleName()); Table t = util.createTable(name, cf); t.put(new Put(row).addColumn(cf, cq, value)); util.getAdmin().flush(name); assertArrayEquals(value, t.get(new Get(row)).getValue(cf, cq)); } }