Search in sources :

Example 1 with Cell

use of org.apache.hadoop.hbase.Cell in project hive by apache.

the class HBaseReadWrite method multiRead.

private void multiRead(String table, byte[] colFam, byte[] colName, byte[][] keys, ByteBuffer[] resultDest) throws IOException {
    assert keys.length == resultDest.length;
    @SuppressWarnings("deprecation") HTableInterface htab = conn.getHBaseTable(table);
    List<Get> gets = new ArrayList<>(keys.length);
    for (byte[] key : keys) {
        Get g = new Get(key);
        g.addColumn(colFam, colName);
        gets.add(g);
    }
    Result[] results = htab.get(gets);
    for (int i = 0; i < results.length; ++i) {
        Result r = results[i];
        if (r.isEmpty()) {
            resultDest[i] = null;
        } else {
            Cell cell = r.getColumnLatestCell(colFam, colName);
            resultDest[i] = ByteBuffer.wrap(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
        }
    }
}
Also used : Get(org.apache.hadoop.hbase.client.Get) ArrayList(java.util.ArrayList) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) Cell(org.apache.hadoop.hbase.Cell) Result(org.apache.hadoop.hbase.client.Result)

Example 2 with Cell

use of org.apache.hadoop.hbase.Cell in project hbase by apache.

the class TestClientScanner method testNoResultsHint.

@Test
@SuppressWarnings("unchecked")
public void testNoResultsHint() throws IOException {
    final Result[] results = new Result[1];
    KeyValue kv1 = new KeyValue("row".getBytes(), "cf".getBytes(), "cq".getBytes(), 1, Type.Maximum);
    results[0] = Result.create(new Cell[] { kv1 });
    RpcRetryingCaller<Result[]> caller = Mockito.mock(RpcRetryingCaller.class);
    Mockito.when(rpcFactory.<Result[]>newCaller()).thenReturn(caller);
    Mockito.when(caller.callWithoutRetries(Mockito.any(RetryingCallable.class), Mockito.anyInt())).thenAnswer(new Answer<Result[]>() {

        private int count = 0;

        @Override
        public Result[] answer(InvocationOnMock invocation) throws Throwable {
            ScannerCallableWithReplicas callable = invocation.getArgumentAt(0, ScannerCallableWithReplicas.class);
            switch(count) {
                case // initialize
                0:
                    count++;
                    callable.currentScannerCallable.setMoreResultsInRegion(MoreResults.UNKNOWN);
                    return results;
                // detect no more results
                case 1:
                case // close
                2:
                    count++;
                    return new Result[0];
                default:
                    throw new RuntimeException("Expected only 2 invocations");
            }
        }
    });
    // Set a much larger cache and buffer size than we'll provide
    scan.setCaching(100);
    scan.setMaxResultSize(1000 * 1000);
    try (MockClientScanner scanner = new MockClientScanner(conf, scan, TableName.valueOf(name.getMethodName()), clusterConn, rpcFactory, controllerFactory, pool, Integer.MAX_VALUE)) {
        scanner.setRpcFinished(true);
        InOrder inOrder = Mockito.inOrder(caller);
        scanner.loadCache();
        // One for fetching the results
        // One for fetching empty results and quit as we do not have moreResults hint.
        inOrder.verify(caller, Mockito.times(2)).callWithoutRetries(Mockito.any(RetryingCallable.class), Mockito.anyInt());
        assertEquals(1, scanner.cache.size());
        Result r = scanner.cache.poll();
        assertNotNull(r);
        CellScanner cs = r.cellScanner();
        assertTrue(cs.advance());
        assertEquals(kv1, cs.current());
        assertFalse(cs.advance());
    }
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) InOrder(org.mockito.InOrder) CellScanner(org.apache.hadoop.hbase.CellScanner) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Cell(org.apache.hadoop.hbase.Cell) Test(org.junit.Test)

Example 3 with Cell

use of org.apache.hadoop.hbase.Cell in project hbase by apache.

the class TestClientScanner method testNoMoreResults.

@Test
@SuppressWarnings("unchecked")
public void testNoMoreResults() throws IOException {
    final Result[] results = new Result[1];
    KeyValue kv1 = new KeyValue("row".getBytes(), "cf".getBytes(), "cq".getBytes(), 1, Type.Maximum);
    results[0] = Result.create(new Cell[] { kv1 });
    RpcRetryingCaller<Result[]> caller = Mockito.mock(RpcRetryingCaller.class);
    Mockito.when(rpcFactory.<Result[]>newCaller()).thenReturn(caller);
    Mockito.when(caller.callWithoutRetries(Mockito.any(RetryingCallable.class), Mockito.anyInt())).thenAnswer(new Answer<Result[]>() {

        private int count = 0;

        @Override
        public Result[] answer(InvocationOnMock invocation) throws Throwable {
            ScannerCallableWithReplicas callable = invocation.getArgumentAt(0, ScannerCallableWithReplicas.class);
            switch(count) {
                case // initialize
                0:
                    count++;
                    callable.currentScannerCallable.setMoreResultsInRegion(MoreResults.NO);
                    return results;
                case // close
                1:
                    count++;
                    return null;
                default:
                    throw new RuntimeException("Expected only 2 invocations");
            }
        }
    });
    Mockito.when(rpcFactory.<Result[]>newCaller()).thenReturn(caller);
    // Set a much larger cache and buffer size than we'll provide
    scan.setCaching(100);
    scan.setMaxResultSize(1000 * 1000);
    try (MockClientScanner scanner = new MockClientScanner(conf, scan, TableName.valueOf(name.getMethodName()), clusterConn, rpcFactory, controllerFactory, pool, Integer.MAX_VALUE)) {
        scanner.setRpcFinished(true);
        InOrder inOrder = Mockito.inOrder(caller);
        scanner.loadCache();
        inOrder.verify(caller, Mockito.times(1)).callWithoutRetries(Mockito.any(RetryingCallable.class), Mockito.anyInt());
        assertEquals(1, scanner.cache.size());
        Result r = scanner.cache.poll();
        assertNotNull(r);
        CellScanner cs = r.cellScanner();
        assertTrue(cs.advance());
        assertEquals(kv1, cs.current());
        assertFalse(cs.advance());
    }
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) InOrder(org.mockito.InOrder) CellScanner(org.apache.hadoop.hbase.CellScanner) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Cell(org.apache.hadoop.hbase.Cell) Test(org.junit.Test)

Example 4 with Cell

use of org.apache.hadoop.hbase.Cell in project hbase by apache.

the class TestClientScanner method testSizeLimit.

@Test
@SuppressWarnings("unchecked")
public void testSizeLimit() throws IOException {
    final Result[] results = new Result[1];
    KeyValue kv1 = new KeyValue("row".getBytes(), "cf".getBytes(), "cq".getBytes(), 1, Type.Maximum);
    results[0] = Result.create(new Cell[] { kv1 });
    RpcRetryingCaller<Result[]> caller = Mockito.mock(RpcRetryingCaller.class);
    Mockito.when(rpcFactory.<Result[]>newCaller()).thenReturn(caller);
    Mockito.when(caller.callWithoutRetries(Mockito.any(RetryingCallable.class), Mockito.anyInt())).thenAnswer(new Answer<Result[]>() {

        private int count = 0;

        @Override
        public Result[] answer(InvocationOnMock invocation) throws Throwable {
            ScannerCallableWithReplicas callable = invocation.getArgumentAt(0, ScannerCallableWithReplicas.class);
            switch(count) {
                case // initialize
                0:
                    count++;
                    // if we set no here the implementation will trigger a close
                    callable.currentScannerCallable.setMoreResultsInRegion(MoreResults.YES);
                    return results;
                case // close
                1:
                    count++;
                    return null;
                default:
                    throw new RuntimeException("Expected only 2 invocations");
            }
        }
    });
    Mockito.when(rpcFactory.<Result[]>newCaller()).thenReturn(caller);
    // Set a much larger cache
    scan.setCaching(100);
    // The single key-value will exit the loop
    scan.setMaxResultSize(1);
    try (MockClientScanner scanner = new MockClientScanner(conf, scan, TableName.valueOf(name.getMethodName()), clusterConn, rpcFactory, controllerFactory, pool, Integer.MAX_VALUE)) {
        InOrder inOrder = Mockito.inOrder(caller);
        scanner.loadCache();
        inOrder.verify(caller, Mockito.times(1)).callWithoutRetries(Mockito.any(RetryingCallable.class), Mockito.anyInt());
        assertEquals(1, scanner.cache.size());
        Result r = scanner.cache.poll();
        assertNotNull(r);
        CellScanner cs = r.cellScanner();
        assertTrue(cs.advance());
        assertEquals(kv1, cs.current());
        assertFalse(cs.advance());
    }
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) InOrder(org.mockito.InOrder) CellScanner(org.apache.hadoop.hbase.CellScanner) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Cell(org.apache.hadoop.hbase.Cell) Test(org.junit.Test)

Example 5 with Cell

use of org.apache.hadoop.hbase.Cell in project hbase by apache.

the class RowCountEndpoint method getRowCount.

/**
   * Returns a count of the rows in the region where this coprocessor is loaded.
   */
@Override
public void getRowCount(RpcController controller, ExampleProtos.CountRequest request, RpcCallback<ExampleProtos.CountResponse> done) {
    Scan scan = new Scan();
    scan.setFilter(new FirstKeyOnlyFilter());
    ExampleProtos.CountResponse response = null;
    InternalScanner scanner = null;
    try {
        scanner = env.getRegion().getScanner(scan);
        List<Cell> results = new ArrayList<>();
        boolean hasMore = false;
        byte[] lastRow = null;
        long count = 0;
        do {
            hasMore = scanner.next(results);
            for (Cell kv : results) {
                byte[] currentRow = CellUtil.cloneRow(kv);
                if (lastRow == null || !Bytes.equals(lastRow, currentRow)) {
                    lastRow = currentRow;
                    count++;
                }
            }
            results.clear();
        } while (hasMore);
        response = ExampleProtos.CountResponse.newBuilder().setCount(count).build();
    } catch (IOException ioe) {
        CoprocessorRpcUtils.setControllerException(controller, ioe);
    } finally {
        if (scanner != null) {
            try {
                scanner.close();
            } catch (IOException ignored) {
            }
        }
    }
    done.run(response);
}
Also used : ExampleProtos(org.apache.hadoop.hbase.coprocessor.example.generated.ExampleProtos) InternalScanner(org.apache.hadoop.hbase.regionserver.InternalScanner) FirstKeyOnlyFilter(org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter) ArrayList(java.util.ArrayList) Scan(org.apache.hadoop.hbase.client.Scan) IOException(java.io.IOException) Cell(org.apache.hadoop.hbase.Cell)

Aggregations

Cell (org.apache.hadoop.hbase.Cell)1201 Test (org.junit.Test)491 ArrayList (java.util.ArrayList)430 Scan (org.apache.hadoop.hbase.client.Scan)341 Result (org.apache.hadoop.hbase.client.Result)290 KeyValue (org.apache.hadoop.hbase.KeyValue)260 Put (org.apache.hadoop.hbase.client.Put)202 IOException (java.io.IOException)193 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)150 Table (org.apache.hadoop.hbase.client.Table)139 TableName (org.apache.hadoop.hbase.TableName)132 Get (org.apache.hadoop.hbase.client.Get)121 List (java.util.List)110 Delete (org.apache.hadoop.hbase.client.Delete)104 Configuration (org.apache.hadoop.conf.Configuration)98 CellScanner (org.apache.hadoop.hbase.CellScanner)87 Path (org.apache.hadoop.fs.Path)73 Map (java.util.Map)64 RegionScanner (org.apache.hadoop.hbase.regionserver.RegionScanner)60 InterruptedIOException (java.io.InterruptedIOException)59