Search in sources :

Example 1 with CellMappingStrategy

use of org.apache.camel.component.hbase.mapping.CellMappingStrategy 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));
    }
}
Also used : Table(org.apache.hadoop.hbase.client.Table) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) CellMappingStrategy(org.apache.camel.component.hbase.mapping.CellMappingStrategy) FilterList(org.apache.hadoop.hbase.filter.FilterList) LinkedList(java.util.LinkedList) HBaseCell(org.apache.camel.component.hbase.model.HBaseCell) Result(org.apache.hadoop.hbase.client.Result) Exchange(org.apache.camel.Exchange) PageFilter(org.apache.hadoop.hbase.filter.PageFilter) Filter(org.apache.hadoop.hbase.filter.Filter) HBaseData(org.apache.camel.component.hbase.model.HBaseData) HBaseRow(org.apache.camel.component.hbase.model.HBaseRow) Scan(org.apache.hadoop.hbase.client.Scan) PageFilter(org.apache.hadoop.hbase.filter.PageFilter) HBaseCell(org.apache.camel.component.hbase.model.HBaseCell) Cell(org.apache.hadoop.hbase.Cell)

Example 2 with CellMappingStrategy

use of org.apache.camel.component.hbase.mapping.CellMappingStrategy in project camel by apache.

the class HBaseProducer method process.

public void process(Exchange exchange) throws Exception {
    try (Table table = endpoint.getTable()) {
        updateHeaders(exchange);
        String operation = (String) exchange.getIn().getHeader(HBaseConstants.OPERATION);
        Integer maxScanResult = exchange.getIn().getHeader(HBaseConstants.HBASE_MAX_SCAN_RESULTS, Integer.class);
        String fromRowId = (String) exchange.getIn().getHeader(HBaseConstants.FROM_ROW);
        String stopRowId = (String) exchange.getIn().getHeader(HBaseConstants.STOP_ROW);
        CellMappingStrategy mappingStrategy = endpoint.getCellMappingStrategyFactory().getStrategy(exchange.getIn());
        HBaseData data = mappingStrategy.resolveModel(exchange.getIn());
        List<Put> putOperations = new LinkedList<>();
        List<Delete> deleteOperations = new LinkedList<>();
        List<HBaseRow> getOperationResult = new LinkedList<>();
        List<HBaseRow> scanOperationResult = new LinkedList<>();
        for (HBaseRow hRow : data.getRows()) {
            hRow.apply(rowModel);
            if (HBaseConstants.PUT.equals(operation)) {
                putOperations.add(createPut(hRow));
            } else if (HBaseConstants.GET.equals(operation)) {
                HBaseRow getResultRow = getCells(table, hRow);
                getOperationResult.add(getResultRow);
            } else if (HBaseConstants.DELETE.equals(operation)) {
                deleteOperations.add(createDeleteRow(hRow));
            } else if (HBaseConstants.SCAN.equals(operation)) {
                scanOperationResult = scanCells(table, hRow, fromRowId, stopRowId, maxScanResult, endpoint.getFilters());
            }
        }
        //Check if we have something to add.
        if (!putOperations.isEmpty()) {
            table.put(putOperations);
        } else if (!deleteOperations.isEmpty()) {
            table.delete(deleteOperations);
        } else if (!getOperationResult.isEmpty()) {
            mappingStrategy.applyGetResults(exchange.getOut(), new HBaseData(getOperationResult));
        } else if (!scanOperationResult.isEmpty()) {
            mappingStrategy.applyScanResults(exchange.getOut(), new HBaseData(scanOperationResult));
        }
    }
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) Table(org.apache.hadoop.hbase.client.Table) CellMappingStrategy(org.apache.camel.component.hbase.mapping.CellMappingStrategy) HBaseData(org.apache.camel.component.hbase.model.HBaseData) HBaseRow(org.apache.camel.component.hbase.model.HBaseRow) LinkedList(java.util.LinkedList) Put(org.apache.hadoop.hbase.client.Put)

Aggregations

LinkedList (java.util.LinkedList)2 CellMappingStrategy (org.apache.camel.component.hbase.mapping.CellMappingStrategy)2 HBaseData (org.apache.camel.component.hbase.model.HBaseData)2 HBaseRow (org.apache.camel.component.hbase.model.HBaseRow)2 Table (org.apache.hadoop.hbase.client.Table)2 Exchange (org.apache.camel.Exchange)1 HBaseCell (org.apache.camel.component.hbase.model.HBaseCell)1 Cell (org.apache.hadoop.hbase.Cell)1 Delete (org.apache.hadoop.hbase.client.Delete)1 Put (org.apache.hadoop.hbase.client.Put)1 Result (org.apache.hadoop.hbase.client.Result)1 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)1 Scan (org.apache.hadoop.hbase.client.Scan)1 Filter (org.apache.hadoop.hbase.filter.Filter)1 FilterList (org.apache.hadoop.hbase.filter.FilterList)1 PageFilter (org.apache.hadoop.hbase.filter.PageFilter)1