use of org.apache.camel.component.hbase.model.HBaseCell 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.camel.component.hbase.model.HBaseCell in project camel by apache.
the class HBaseEndpoint method createRowModel.
/**
* Creates an {@link HBaseRow} model from the specified endpoint parameters.
*/
private HBaseRow createRowModel(Map<String, Object> parameters) {
HBaseRow rowModel = new HBaseRow();
if (parameters.containsKey(HBaseAttribute.HBASE_ROW_TYPE.asOption())) {
String rowType = String.valueOf(parameters.remove(HBaseAttribute.HBASE_ROW_TYPE.asOption()));
if (rowType != null && !rowType.isEmpty()) {
rowModel.setRowType(getCamelContext().getClassResolver().resolveClass(rowType));
}
}
for (int i = 1; parameters.get(HBaseAttribute.HBASE_FAMILY.asOption(i)) != null && parameters.get(HBaseAttribute.HBASE_QUALIFIER.asOption(i)) != null; i++) {
HBaseCell cellModel = new HBaseCell();
cellModel.setFamily(String.valueOf(parameters.remove(HBaseAttribute.HBASE_FAMILY.asOption(i))));
cellModel.setQualifier(String.valueOf(parameters.remove(HBaseAttribute.HBASE_QUALIFIER.asOption(i))));
cellModel.setValue(String.valueOf(parameters.remove(HBaseAttribute.HBASE_VALUE.asOption(i))));
if (parameters.containsKey(HBaseAttribute.HBASE_VALUE_TYPE.asOption(i))) {
String valueType = String.valueOf(parameters.remove(HBaseAttribute.HBASE_VALUE_TYPE.asOption(i)));
if (valueType != null && !valueType.isEmpty()) {
rowModel.setRowType(getCamelContext().getClassResolver().resolveClass(valueType));
}
}
rowModel.getCells().add(cellModel);
}
return rowModel;
}
use of org.apache.camel.component.hbase.model.HBaseCell in project camel by apache.
the class HeaderMappingStrategy method resolveRow.
/**
* Resolves the cell that the {@link Exchange} refers to.
*/
private HBaseRow resolveRow(Message message, int index) {
HBaseRow hRow = new HBaseRow();
HBaseCell hCell = new HBaseCell();
if (message != null) {
Object id = message.getHeader(HBaseAttribute.HBASE_ROW_ID.asHeader(index));
String rowClassName = message.getHeader(HBaseAttribute.HBASE_ROW_TYPE.asHeader(index), String.class);
Class<?> rowClass = rowClassName == null || rowClassName.isEmpty() ? String.class : message.getExchange().getContext().getClassResolver().resolveClass(rowClassName);
String columnFamily = (String) message.getHeader(HBaseAttribute.HBASE_FAMILY.asHeader(index));
String columnName = (String) message.getHeader(HBaseAttribute.HBASE_QUALIFIER.asHeader(index));
Object value = message.getHeader(HBaseAttribute.HBASE_VALUE.asHeader(index));
String valueClassName = message.getHeader(HBaseAttribute.HBASE_VALUE_TYPE.asHeader(index), String.class);
Class<?> valueClass = valueClassName == null || valueClassName.isEmpty() ? String.class : message.getExchange().getContext().getClassResolver().resolveClass(valueClassName);
//Id can be accepted as null when using get, scan etc.
if (id == null && columnFamily == null && columnName == null) {
return null;
}
hRow.setId(id);
hRow.setRowType(rowClass);
if (columnFamily != null && columnName != null) {
hCell.setQualifier(columnName);
hCell.setFamily(columnFamily);
hCell.setValue(value);
// String is the default value type
hCell.setValueType((valueClass != null) ? valueClass : String.class);
hRow.getCells().add(hCell);
}
}
return hRow;
}
use of org.apache.camel.component.hbase.model.HBaseCell in project camel by apache.
the class HBaseProducer method getCells.
/**
* Performs an HBase {@link Get} on a specific row, using a collection of values (family/column/value pairs).
* The result is <p>the most recent entry</p> for each column.
*/
private HBaseRow getCells(Table table, HBaseRow hRow) throws Exception {
HBaseRow resultRow = new HBaseRow();
List<HBaseCell> resultCells = new LinkedList<>();
ObjectHelper.notNull(hRow, "HBase row");
ObjectHelper.notNull(hRow.getId(), "HBase row id");
ObjectHelper.notNull(hRow.getCells(), "HBase cells");
resultRow.setId(hRow.getId());
Get get = new Get(endpoint.getCamelContext().getTypeConverter().convertTo(byte[].class, hRow.getId()));
Set<HBaseCell> cellModels = hRow.getCells();
for (HBaseCell cellModel : cellModels) {
String family = cellModel.getFamily();
String column = cellModel.getQualifier();
ObjectHelper.notNull(family, "HBase column family", cellModel);
ObjectHelper.notNull(column, "HBase column", cellModel);
get.addColumn(HBaseHelper.getHBaseFieldAsBytes(family), HBaseHelper.getHBaseFieldAsBytes(column));
}
Result result = table.get(get);
if (!result.isEmpty()) {
resultRow.setTimestamp(result.rawCells()[0].getTimestamp());
}
for (HBaseCell cellModel : cellModels) {
HBaseCell resultCell = new HBaseCell();
String family = cellModel.getFamily();
String column = cellModel.getQualifier();
resultCell.setFamily(family);
resultCell.setQualifier(column);
List<Cell> kvs = result.getColumnCells(HBaseHelper.getHBaseFieldAsBytes(family), HBaseHelper.getHBaseFieldAsBytes(column));
if (kvs != null && !kvs.isEmpty()) {
//Return the most recent entry.
resultCell.setValue(endpoint.getCamelContext().getTypeConverter().convertTo(cellModel.getValueType(), CellUtil.cloneValue(kvs.get(0))));
resultCell.setTimestamp(kvs.get(0).getTimestamp());
}
resultCells.add(resultCell);
resultRow.getCells().add(resultCell);
}
return resultRow;
}
use of org.apache.camel.component.hbase.model.HBaseCell in project camel by apache.
the class HBaseProducer method createPut.
/**
* Creates an HBase {@link Put} on a specific row, using a collection of values (family/column/value pairs).
*
* @param hRow
* @throws Exception
*/
private Put createPut(HBaseRow hRow) throws Exception {
ObjectHelper.notNull(hRow, "HBase row");
ObjectHelper.notNull(hRow.getId(), "HBase row id");
ObjectHelper.notNull(hRow.getCells(), "HBase cells");
Put put = new Put(endpoint.getCamelContext().getTypeConverter().convertTo(byte[].class, hRow.getId()));
Set<HBaseCell> cells = hRow.getCells();
for (HBaseCell cell : cells) {
String family = cell.getFamily();
String column = cell.getQualifier();
Object value = cell.getValue();
ObjectHelper.notNull(family, "HBase column family", cell);
ObjectHelper.notNull(column, "HBase column", cell);
put.addColumn(HBaseHelper.getHBaseFieldAsBytes(family), HBaseHelper.getHBaseFieldAsBytes(column), endpoint.getCamelContext().getTypeConverter().convertTo(byte[].class, value));
}
return put;
}
Aggregations