Search in sources :

Example 1 with ModelAwareFilter

use of org.apache.camel.component.hbase.filters.ModelAwareFilter in project camel by apache.

the class HBaseProducer method scanCells.

/**
     * 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 List<HBaseRow> scanCells(Table table, HBaseRow model, String start, String stop, Integer maxRowScan, List<Filter> filters) throws Exception {
    List<HBaseRow> rowSet = new LinkedList<>();
    HBaseRow startRow = new HBaseRow(model.getCells());
    startRow.setId(start);
    Scan scan;
    if (start != null) {
        scan = new Scan(Bytes.toBytes(start));
    } else {
        scan = new Scan();
    }
    if (ObjectHelper.isNotEmpty(stop)) {
        scan.setStopRow(Bytes.toBytes(stop));
    }
    if (filters != null && !filters.isEmpty()) {
        for (int i = 0; i < filters.size(); i++) {
            ((ModelAwareFilter<?>) filters.get(i)).apply(endpoint.getCamelContext(), model);
            scan.setFilter(new FilterList(FilterList.Operator.MUST_PASS_ALL, ((ModelAwareFilter<?>) filters.get(i)).getFilteredList()));
        }
    }
    Set<HBaseCell> cellModels = model.getCells();
    for (HBaseCell cellModel : cellModels) {
        String family = cellModel.getFamily();
        String column = cellModel.getQualifier();
        if (ObjectHelper.isNotEmpty(family) && ObjectHelper.isNotEmpty(column)) {
            scan.addColumn(HBaseHelper.getHBaseFieldAsBytes(family), HBaseHelper.getHBaseFieldAsBytes(column));
        }
    }
    ResultScanner resultScanner = table.getScanner(scan);
    int count = 0;
    Result result = resultScanner.next();
    while (result != null && count < maxRowScan) {
        HBaseRow resultRow = new HBaseRow();
        resultRow.setId(endpoint.getCamelContext().getTypeConverter().convertTo(model.getRowType(), result.getRow()));
        resultRow.setTimestamp(result.rawCells()[0].getTimestamp());
        cellModels = model.getCells();
        for (HBaseCell modelCell : cellModels) {
            HBaseCell resultCell = new HBaseCell();
            String family = modelCell.getFamily();
            String column = modelCell.getQualifier();
            resultRow.setId(endpoint.getCamelContext().getTypeConverter().convertTo(model.getRowType(), result.getRow()));
            resultCell.setValue(endpoint.getCamelContext().getTypeConverter().convertTo(modelCell.getValueType(), result.getValue(HBaseHelper.getHBaseFieldAsBytes(family), HBaseHelper.getHBaseFieldAsBytes(column))));
            resultCell.setFamily(modelCell.getFamily());
            resultCell.setQualifier(modelCell.getQualifier());
            Cell cell = result.getColumnLatestCell(HBaseHelper.getHBaseFieldAsBytes(family), HBaseHelper.getHBaseFieldAsBytes(column));
            if (cell != null) {
                resultCell.setTimestamp(cell.getTimestamp());
            }
            resultRow.getCells().add(resultCell);
        }
        rowSet.add(resultRow);
        count++;
        result = resultScanner.next();
    }
    return rowSet;
}
Also used : ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) 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) HBaseRow(org.apache.camel.component.hbase.model.HBaseRow) ModelAwareFilter(org.apache.camel.component.hbase.filters.ModelAwareFilter) Scan(org.apache.hadoop.hbase.client.Scan) HBaseCell(org.apache.camel.component.hbase.model.HBaseCell) Cell(org.apache.hadoop.hbase.Cell)

Aggregations

LinkedList (java.util.LinkedList)1 ModelAwareFilter (org.apache.camel.component.hbase.filters.ModelAwareFilter)1 HBaseCell (org.apache.camel.component.hbase.model.HBaseCell)1 HBaseRow (org.apache.camel.component.hbase.model.HBaseRow)1 Cell (org.apache.hadoop.hbase.Cell)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 FilterList (org.apache.hadoop.hbase.filter.FilterList)1