use of org.apache.hadoop.hbase.client.Result in project camel by apache.
the class HBaseConsumer method poll.
@Override
protected int poll() throws Exception {
try (Table table = endpoint.getTable()) {
shutdownRunningTask = null;
pendingExchanges = 0;
Queue<Exchange> queue = new LinkedList<>();
Scan scan = new Scan();
List<Filter> filters = new LinkedList<>();
if (endpoint.getFilters() != null) {
filters.addAll(endpoint.getFilters());
}
if (maxMessagesPerPoll > 0) {
filters.add(new PageFilter(maxMessagesPerPoll));
}
if (!filters.isEmpty()) {
Filter compoundFilter = new FilterList(filters);
scan.setFilter(compoundFilter);
}
if (rowModel != null && rowModel.getCells() != null) {
Set<HBaseCell> cellModels = rowModel.getCells();
for (HBaseCell cellModel : cellModels) {
scan.addColumn(HBaseHelper.getHBaseFieldAsBytes(cellModel.getFamily()), HBaseHelper.getHBaseFieldAsBytes(cellModel.getQualifier()));
}
}
ResultScanner scanner = table.getScanner(scan);
int exchangeCount = 0;
// The next three statements are used just to get a reference to the BodyCellMappingStrategy instance.
Exchange exchange = endpoint.createExchange();
exchange.getIn().setHeader(CellMappingStrategyFactory.STRATEGY, CellMappingStrategyFactory.BODY);
CellMappingStrategy mappingStrategy = endpoint.getCellMappingStrategyFactory().getStrategy(exchange.getIn());
for (Result result = scanner.next(); (exchangeCount < maxMessagesPerPoll || maxMessagesPerPoll <= 0) && result != null; result = scanner.next()) {
HBaseData data = new HBaseData();
HBaseRow resultRow = new HBaseRow();
resultRow.apply(rowModel);
byte[] row = result.getRow();
resultRow.setId(endpoint.getCamelContext().getTypeConverter().convertTo(rowModel.getRowType(), row));
List<Cell> cells = result.listCells();
if (cells != null) {
Set<HBaseCell> cellModels = rowModel.getCells();
if (cellModels.size() > 0) {
for (HBaseCell modelCell : cellModels) {
HBaseCell resultCell = new HBaseCell();
String family = modelCell.getFamily();
String column = modelCell.getQualifier();
resultCell.setValue(endpoint.getCamelContext().getTypeConverter().convertTo(modelCell.getValueType(), result.getValue(HBaseHelper.getHBaseFieldAsBytes(family), HBaseHelper.getHBaseFieldAsBytes(column))));
resultCell.setFamily(modelCell.getFamily());
resultCell.setQualifier(modelCell.getQualifier());
resultRow.getCells().add(resultCell);
}
} else {
// just need to put every key value into the result Cells
for (Cell cell : cells) {
String qualifier = new String(CellUtil.cloneQualifier(cell));
String family = new String(CellUtil.cloneFamily(cell));
HBaseCell resultCell = new HBaseCell();
resultCell.setFamily(family);
resultCell.setQualifier(qualifier);
resultCell.setValue(endpoint.getCamelContext().getTypeConverter().convertTo(String.class, CellUtil.cloneValue(cell)));
resultRow.getCells().add(resultCell);
}
}
data.getRows().add(resultRow);
exchange = endpoint.createExchange();
// Probably overkill but kept it here for consistency.
exchange.getIn().setHeader(CellMappingStrategyFactory.STRATEGY, CellMappingStrategyFactory.BODY);
mappingStrategy.applyScanResults(exchange.getIn(), data);
//Make sure that there is a header containing the marked row ids, so that they can be deleted.
exchange.getIn().setHeader(HBaseAttribute.HBASE_MARKED_ROW_ID.asHeader(), result.getRow());
queue.add(exchange);
exchangeCount++;
}
}
scanner.close();
return queue.isEmpty() ? 0 : processBatch(CastUtils.cast(queue));
}
}
use of org.apache.hadoop.hbase.client.Result in project camel by apache.
the class HBaseConvertionsTest method testPutMultiRows.
@Test
public void testPutMultiRows() throws Exception {
if (systemReady) {
ProducerTemplate template = context.createProducerTemplate();
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(HBaseAttribute.HBASE_ROW_ID.asHeader(), key[0]);
headers.put(HBaseAttribute.HBASE_FAMILY.asHeader(), INFO_FAMILY);
headers.put(HBaseAttribute.HBASE_QUALIFIER.asHeader(), column[0]);
headers.put(HBaseAttribute.HBASE_VALUE.asHeader(), body[0]);
headers.put(HBaseAttribute.HBASE_ROW_ID.asHeader(2), key[1]);
headers.put(HBaseAttribute.HBASE_FAMILY.asHeader(2), INFO_FAMILY);
headers.put(HBaseAttribute.HBASE_QUALIFIER.asHeader(2), column[0]);
headers.put(HBaseAttribute.HBASE_VALUE.asHeader(2), body[1]);
headers.put(HBaseAttribute.HBASE_ROW_ID.asHeader(3), key[2]);
headers.put(HBaseAttribute.HBASE_FAMILY.asHeader(3), INFO_FAMILY);
headers.put(HBaseAttribute.HBASE_QUALIFIER.asHeader(3), column[0]);
headers.put(HBaseAttribute.HBASE_VALUE.asHeader(3), body[2]);
headers.put(HBaseConstants.OPERATION, HBaseConstants.PUT);
template.sendBodyAndHeaders("direct:start", null, headers);
Configuration configuration = hbaseUtil.getHBaseAdmin().getConfiguration();
Connection conn = ConnectionFactory.createConnection(configuration);
Table bar = conn.getTable(TableName.valueOf(PERSON_TABLE));
Get get = new Get(Bytes.toBytes((Integer) key[0]));
//Check row 1
get.addColumn(INFO_FAMILY.getBytes(), column[0].getBytes());
Result result = bar.get(get);
byte[] resultValue = result.value();
assertArrayEquals(Bytes.toBytes((Long) body[0]), resultValue);
//Check row 2
get = new Get(Bytes.toBytes((String) key[1]));
get.addColumn(INFO_FAMILY.getBytes(), column[0].getBytes());
result = bar.get(get);
resultValue = result.value();
assertArrayEquals(Bytes.toBytes((Boolean) body[1]), resultValue);
//Check row 3
get = new Get(Bytes.toBytes((String) key[2]));
get.addColumn(INFO_FAMILY.getBytes(), column[0].getBytes());
result = bar.get(get);
resultValue = result.value();
assertArrayEquals(Bytes.toBytes((String) body[2]), resultValue);
IOHelper.close(bar);
}
}
use of org.apache.hadoop.hbase.client.Result in project hbase by apache.
the class MetaTableAccessor method scanMeta.
/**
* Performs a scan of META table.
* @param connection connection we're using
* @param startRow Where to start the scan. Pass null if want to begin scan
* at first row.
* @param stopRow Where to stop the scan. Pass null if want to scan all rows
* from the start one
* @param type scanned part of meta
* @param maxRows maximum rows to return
* @param visitor Visitor invoked against each row.
* @throws IOException
*/
public static void scanMeta(Connection connection, @Nullable final byte[] startRow, @Nullable final byte[] stopRow, QueryType type, int maxRows, final Visitor visitor) throws IOException {
int rowUpperLimit = maxRows > 0 ? maxRows : Integer.MAX_VALUE;
Scan scan = getMetaScan(connection, rowUpperLimit);
for (byte[] family : type.getFamilies()) {
scan.addFamily(family);
}
if (startRow != null)
scan.setStartRow(startRow);
if (stopRow != null)
scan.setStopRow(stopRow);
if (LOG.isTraceEnabled()) {
LOG.trace("Scanning META" + " starting at row=" + Bytes.toStringBinary(startRow) + " stopping at row=" + Bytes.toStringBinary(stopRow) + " for max=" + rowUpperLimit + " with caching=" + scan.getCaching());
}
int currentRow = 0;
try (Table metaTable = getMetaHTable(connection)) {
try (ResultScanner scanner = metaTable.getScanner(scan)) {
Result data;
while ((data = scanner.next()) != null) {
if (data.isEmpty())
continue;
// Break if visit returns false.
if (!visitor.visit(data))
break;
if (++currentRow >= rowUpperLimit)
break;
}
}
}
if (visitor != null && visitor instanceof Closeable) {
try {
((Closeable) visitor).close();
} catch (Throwable t) {
ExceptionUtil.rethrowIfInterrupt(t);
LOG.debug("Got exception in closing the meta scanner visitor", t);
}
}
}
use of org.apache.hadoop.hbase.client.Result in project hbase by apache.
the class MetaTableAccessor method getRegionsFromMergeQualifier.
/**
* Get regions from the merge qualifier of the specified merged region
* @return null if it doesn't contain merge qualifier, else two merge regions
* @throws IOException
*/
@Nullable
public static Pair<HRegionInfo, HRegionInfo> getRegionsFromMergeQualifier(Connection connection, byte[] regionName) throws IOException {
Result result = getRegionResult(connection, regionName);
HRegionInfo mergeA = getHRegionInfo(result, HConstants.MERGEA_QUALIFIER);
HRegionInfo mergeB = getHRegionInfo(result, HConstants.MERGEB_QUALIFIER);
if (mergeA == null && mergeB == null) {
return null;
}
return new Pair<>(mergeA, mergeB);
}
use of org.apache.hadoop.hbase.client.Result in project hbase by apache.
the class MetaTableAccessor method getReplicationPositionForAllPeer.
/**
* Get replication positions for all peers in a region.
* @param connection connection we're using
* @param encodedRegionName region's encoded name
* @return the map of positions for each peer
*/
public static Map<String, Long> getReplicationPositionForAllPeer(Connection connection, byte[] encodedRegionName) throws IOException {
Get get = new Get(encodedRegionName);
get.addFamily(HConstants.REPLICATION_POSITION_FAMILY);
Result r = get(getMetaHTable(connection), get);
Map<String, Long> map = new HashMap<>((int) (r.size() / 0.75 + 1));
for (Cell c : r.listCells()) {
map.put(Bytes.toString(c.getQualifierArray(), c.getQualifierOffset(), c.getQualifierLength()), Bytes.toLong(c.getValueArray(), c.getValueOffset(), c.getValueLength()));
}
return map;
}
Aggregations