Search in sources :

Example 61 with ResultScanner

use of org.apache.hadoop.hbase.client.ResultScanner in project hbase by apache.

the class TestSyncTable method assertEqualTables.

private void assertEqualTables(int expectedRows, TableName sourceTableName, TableName targetTableName) throws Exception {
    Table sourceTable = TEST_UTIL.getConnection().getTable(sourceTableName);
    Table targetTable = TEST_UTIL.getConnection().getTable(targetTableName);
    ResultScanner sourceScanner = sourceTable.getScanner(new Scan());
    ResultScanner targetScanner = targetTable.getScanner(new Scan());
    for (int i = 0; i < expectedRows; i++) {
        Result sourceRow = sourceScanner.next();
        Result targetRow = targetScanner.next();
        LOG.debug("SOURCE row: " + (sourceRow == null ? "null" : Bytes.toInt(sourceRow.getRow())) + " cells:" + sourceRow);
        LOG.debug("TARGET row: " + (targetRow == null ? "null" : Bytes.toInt(targetRow.getRow())) + " cells:" + targetRow);
        if (sourceRow == null) {
            Assert.fail("Expected " + expectedRows + " source rows but only found " + i);
        }
        if (targetRow == null) {
            Assert.fail("Expected " + expectedRows + " target rows but only found " + i);
        }
        Cell[] sourceCells = sourceRow.rawCells();
        Cell[] targetCells = targetRow.rawCells();
        if (sourceCells.length != targetCells.length) {
            LOG.debug("Source cells: " + Arrays.toString(sourceCells));
            LOG.debug("Target cells: " + Arrays.toString(targetCells));
            Assert.fail("Row " + Bytes.toInt(sourceRow.getRow()) + " has " + sourceCells.length + " cells in source table but " + targetCells.length + " cells in target table");
        }
        for (int j = 0; j < sourceCells.length; j++) {
            Cell sourceCell = sourceCells[j];
            Cell targetCell = targetCells[j];
            try {
                if (!CellUtil.matchingRow(sourceCell, targetCell)) {
                    Assert.fail("Rows don't match");
                }
                if (!CellUtil.matchingFamily(sourceCell, targetCell)) {
                    Assert.fail("Families don't match");
                }
                if (!CellUtil.matchingQualifier(sourceCell, targetCell)) {
                    Assert.fail("Qualifiers don't match");
                }
                if (!CellUtil.matchingTimestamp(sourceCell, targetCell)) {
                    Assert.fail("Timestamps don't match");
                }
                if (!CellUtil.matchingValue(sourceCell, targetCell)) {
                    Assert.fail("Values don't match");
                }
            } catch (Throwable t) {
                LOG.debug("Source cell: " + sourceCell + " target cell: " + targetCell);
                Throwables.propagate(t);
            }
        }
    }
    Result sourceRow = sourceScanner.next();
    if (sourceRow != null) {
        Assert.fail("Source table has more than " + expectedRows + " rows.  Next row: " + Bytes.toInt(sourceRow.getRow()));
    }
    Result targetRow = targetScanner.next();
    if (targetRow != null) {
        Assert.fail("Target table has more than " + expectedRows + " rows.  Next row: " + Bytes.toInt(targetRow.getRow()));
    }
    sourceScanner.close();
    targetScanner.close();
    sourceTable.close();
    targetTable.close();
}
Also used : Table(org.apache.hadoop.hbase.client.Table) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) Scan(org.apache.hadoop.hbase.client.Scan) Cell(org.apache.hadoop.hbase.Cell) Result(org.apache.hadoop.hbase.client.Result)

Example 62 with ResultScanner

use of org.apache.hadoop.hbase.client.ResultScanner in project hbase by apache.

the class TestTableInputFormat method createIOEScannerTable.

/**
   * Create a table that IOE's on first scanner next call
   *
   * @throws IOException
   */
static Table createIOEScannerTable(byte[] name, final int failCnt) throws IOException {
    // build up a mock scanner stuff to fail the first time
    Answer<ResultScanner> a = new Answer<ResultScanner>() {

        int cnt = 0;

        @Override
        public ResultScanner answer(InvocationOnMock invocation) throws Throwable {
            // first invocation return the busted mock scanner
            if (cnt++ < failCnt) {
                // create mock ResultScanner that always fails.
                Scan scan = mock(Scan.class);
                // avoid npe
                doReturn("bogus".getBytes()).when(scan).getStartRow();
                ResultScanner scanner = mock(ResultScanner.class);
                // simulate TimeoutException / IOException
                doThrow(new IOException("Injected exception")).when(scanner).next();
                return scanner;
            }
            // otherwise return the real scanner.
            return (ResultScanner) invocation.callRealMethod();
        }
    };
    Table htable = spy(createTable(name));
    doAnswer(a).when(htable).getScanner((Scan) anyObject());
    return htable;
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) Table(org.apache.hadoop.hbase.client.Table) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Scan(org.apache.hadoop.hbase.client.Scan) IOException(java.io.IOException)

Example 63 with ResultScanner

use of org.apache.hadoop.hbase.client.ResultScanner in project hbase by apache.

the class MobTestUtil method assertCellsValue.

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);
}
Also used : ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) Cell(org.apache.hadoop.hbase.Cell) Result(org.apache.hadoop.hbase.client.Result)

Example 64 with ResultScanner

use of org.apache.hadoop.hbase.client.ResultScanner in project hbase by apache.

the class TestVisibilityLabels method testVisibilityLabelsOnRSRestart.

@Test(timeout = 60 * 1000)
public void testVisibilityLabelsOnRSRestart() throws Exception {
    final TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
    List<RegionServerThread> regionServerThreads = TEST_UTIL.getHBaseCluster().getRegionServerThreads();
    for (RegionServerThread rsThread : regionServerThreads) {
        rsThread.getRegionServer().abort("Aborting ");
    }
    // Start one new RS
    RegionServerThread rs = TEST_UTIL.getHBaseCluster().startRegionServer();
    waitForLabelsRegionAvailability(rs.getRegionServer());
    try (Table table = createTableAndWriteDataWithLabels(tableName, "(" + SECRET + "|" + CONFIDENTIAL + ")", PRIVATE)) {
        Scan s = new Scan();
        s.setAuthorizations(new Authorizations(SECRET));
        ResultScanner scanner = table.getScanner(s);
        Result[] next = scanner.next(3);
        assertTrue(next.length == 1);
    }
}
Also used : TableName(org.apache.hadoop.hbase.TableName) Table(org.apache.hadoop.hbase.client.Table) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) Scan(org.apache.hadoop.hbase.client.Scan) RegionServerThread(org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread) RegionActionResult(org.apache.hadoop.hbase.protobuf.generated.ClientProtos.RegionActionResult) Result(org.apache.hadoop.hbase.client.Result) Test(org.junit.Test)

Example 65 with ResultScanner

use of org.apache.hadoop.hbase.client.ResultScanner in project hbase by apache.

the class TestVisibilityLabels method testClearUserAuths.

@Test
public void testClearUserAuths() throws Throwable {
    PrivilegedExceptionAction<Void> action = new PrivilegedExceptionAction<Void>() {

        public Void run() throws Exception {
            String[] auths = { SECRET, CONFIDENTIAL, PRIVATE };
            String user = "testUser";
            try (Connection conn = ConnectionFactory.createConnection(conf)) {
                VisibilityClient.setAuths(conn, auths, user);
            } catch (Throwable e) {
                fail("Should not have failed");
            }
            // Removing the auths for SECRET and CONFIDENTIAL for the user.
            // Passing a non existing auth also.
            auths = new String[] { SECRET, PUBLIC, CONFIDENTIAL };
            VisibilityLabelsResponse response = null;
            try (Connection conn = ConnectionFactory.createConnection(conf)) {
                response = VisibilityClient.clearAuths(conn, auths, user);
            } catch (Throwable e) {
                fail("Should not have failed");
            }
            List<RegionActionResult> resultList = response.getResultList();
            assertEquals(3, resultList.size());
            assertTrue(resultList.get(0).getException().getValue().isEmpty());
            assertEquals("org.apache.hadoop.hbase.DoNotRetryIOException", resultList.get(1).getException().getName());
            assertTrue(Bytes.toString(resultList.get(1).getException().getValue().toByteArray()).contains("org.apache.hadoop.hbase.security.visibility.InvalidLabelException: " + "Label 'public' is not set for the user testUser"));
            assertTrue(resultList.get(2).getException().getValue().isEmpty());
            try (Connection connection = ConnectionFactory.createConnection(conf);
                Table ht = connection.getTable(LABELS_TABLE_NAME)) {
                ResultScanner scanner = ht.getScanner(new Scan());
                Result result = null;
                List<Result> results = new ArrayList<>();
                while ((result = scanner.next()) != null) {
                    results.add(result);
                }
                List<String> curAuths = extractAuths(user, results);
                assertTrue(curAuths.contains(PRIVATE));
                assertEquals(1, curAuths.size());
            }
            GetAuthsResponse authsResponse = null;
            try (Connection conn = ConnectionFactory.createConnection(conf)) {
                authsResponse = VisibilityClient.getAuths(conn, user);
            } catch (Throwable e) {
                fail("Should not have failed");
            }
            List<String> authsList = new ArrayList<>(authsResponse.getAuthList().size());
            for (ByteString authBS : authsResponse.getAuthList()) {
                authsList.add(Bytes.toString(authBS.toByteArray()));
            }
            assertEquals(1, authsList.size());
            assertTrue(authsList.contains(PRIVATE));
            return null;
        }
    };
    SUPERUSER.runAs(action);
}
Also used : Table(org.apache.hadoop.hbase.client.Table) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) ByteString(com.google.protobuf.ByteString) Connection(org.apache.hadoop.hbase.client.Connection) ArrayList(java.util.ArrayList) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) ByteString(com.google.protobuf.ByteString) RegionActionResult(org.apache.hadoop.hbase.protobuf.generated.ClientProtos.RegionActionResult) RegionActionResult(org.apache.hadoop.hbase.protobuf.generated.ClientProtos.RegionActionResult) Result(org.apache.hadoop.hbase.client.Result) GetAuthsResponse(org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.GetAuthsResponse) Scan(org.apache.hadoop.hbase.client.Scan) VisibilityLabelsResponse(org.apache.hadoop.hbase.protobuf.generated.VisibilityLabelsProtos.VisibilityLabelsResponse) Test(org.junit.Test)

Aggregations

ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)342 Scan (org.apache.hadoop.hbase.client.Scan)303 Result (org.apache.hadoop.hbase.client.Result)302 Table (org.apache.hadoop.hbase.client.Table)164 Test (org.junit.Test)152 Cell (org.apache.hadoop.hbase.Cell)106 IOException (java.io.IOException)102 TableName (org.apache.hadoop.hbase.TableName)89 Delete (org.apache.hadoop.hbase.client.Delete)79 Connection (org.apache.hadoop.hbase.client.Connection)77 Put (org.apache.hadoop.hbase.client.Put)75 ArrayList (java.util.ArrayList)71 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)47 InterruptedIOException (java.io.InterruptedIOException)46 CellScanner (org.apache.hadoop.hbase.CellScanner)42 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)31 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)29 HTable (org.apache.hadoop.hbase.client.HTable)29 Admin (org.apache.hadoop.hbase.client.Admin)24 Get (org.apache.hadoop.hbase.client.Get)23