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());
}
}
}
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());
}
}
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());
}
}
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());
}
}
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);
}
Aggregations