Search in sources :

Example 11 with ResultCell

use of org.apache.nifi.hbase.scan.ResultCell in project nifi by apache.

the class HBase_1_1_2_ClientService method scan.

@Override
public void scan(final String tableName, final Collection<Column> columns, final String filterExpression, final long minTime, final ResultHandler handler) throws IOException {
    Filter filter = null;
    if (!StringUtils.isBlank(filterExpression)) {
        ParseFilter parseFilter = new ParseFilter();
        filter = parseFilter.parseFilterString(filterExpression);
    }
    try (final Table table = connection.getTable(TableName.valueOf(tableName));
        final ResultScanner scanner = getResults(table, columns, filter, minTime)) {
        for (final Result result : scanner) {
            final byte[] rowKey = result.getRow();
            final Cell[] cells = result.rawCells();
            if (cells == null) {
                continue;
            }
            // convert HBase cells to NiFi cells
            final ResultCell[] resultCells = new ResultCell[cells.length];
            for (int i = 0; i < cells.length; i++) {
                final Cell cell = cells[i];
                final ResultCell resultCell = getResultCell(cell);
                resultCells[i] = resultCell;
            }
            // delegate to the handler
            handler.handle(rowKey, resultCells);
        }
    }
}
Also used : Table(org.apache.hadoop.hbase.client.Table) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) ParseFilter(org.apache.hadoop.hbase.filter.ParseFilter) ParseFilter(org.apache.hadoop.hbase.filter.ParseFilter) Filter(org.apache.hadoop.hbase.filter.Filter) ResultCell(org.apache.nifi.hbase.scan.ResultCell) Cell(org.apache.hadoop.hbase.Cell) ResultCell(org.apache.nifi.hbase.scan.ResultCell) Result(org.apache.hadoop.hbase.client.Result) ValidationResult(org.apache.nifi.components.ValidationResult)

Example 12 with ResultCell

use of org.apache.nifi.hbase.scan.ResultCell in project nifi by apache.

the class JsonFullRowSerializer method serialize.

@Override
public String serialize(byte[] rowKey, ResultCell[] cells) {
    final String rowId = RowSerializerUtil.getRowId(rowKey, decodeCharset, base64encode);
    final StringBuilder jsonBuilder = new StringBuilder();
    jsonBuilder.append("{");
    jsonBuilder.append("\"row\":");
    appendString(jsonBuilder, rowId, base64encode);
    jsonBuilder.append(", \"cells\": [");
    int i = 0;
    for (final ResultCell cell : cells) {
        final String cellFamily = RowSerializerUtil.getCellFamily(cell, decodeCharset, base64encode);
        final String cellQualifier = RowSerializerUtil.getCellQualifier(cell, decodeCharset, base64encode);
        final String cellValue = RowSerializerUtil.getCellValue(cell, decodeCharset, base64encode);
        if (i > 0) {
            jsonBuilder.append(", ");
        }
        // start cell
        jsonBuilder.append("{");
        jsonBuilder.append("\"fam\":");
        appendString(jsonBuilder, cellFamily, base64encode);
        jsonBuilder.append(",\"qual\":");
        appendString(jsonBuilder, cellQualifier, base64encode);
        jsonBuilder.append(",\"val\":");
        appendString(jsonBuilder, cellValue, base64encode);
        jsonBuilder.append(",\"ts\":");
        jsonBuilder.append(String.valueOf(cell.getTimestamp()));
        // end cell
        jsonBuilder.append("}");
        i++;
    }
    // end cell array
    jsonBuilder.append("]");
    // end overall document
    jsonBuilder.append("}");
    return jsonBuilder.toString();
}
Also used : ResultCell(org.apache.nifi.hbase.scan.ResultCell)

Example 13 with ResultCell

use of org.apache.nifi.hbase.scan.ResultCell in project nifi by apache.

the class JsonQualifierAndValueRowSerializer method serialize.

@Override
public String serialize(byte[] rowKey, ResultCell[] cells) {
    final StringBuilder jsonBuilder = new StringBuilder();
    jsonBuilder.append("{");
    int i = 0;
    for (final ResultCell cell : cells) {
        final String cellQualifier = RowSerializerUtil.getCellQualifier(cell, decodeCharset, base64encode);
        final String cellValue = RowSerializerUtil.getCellValue(cell, decodeCharset, base64encode);
        if (i > 0) {
            jsonBuilder.append(", ");
        }
        appendString(jsonBuilder, cellQualifier, base64encode);
        jsonBuilder.append(":");
        appendString(jsonBuilder, cellValue, base64encode);
        i++;
    }
    jsonBuilder.append("}");
    return jsonBuilder.toString();
}
Also used : ResultCell(org.apache.nifi.hbase.scan.ResultCell)

Example 14 with ResultCell

use of org.apache.nifi.hbase.scan.ResultCell in project nifi by apache.

the class JsonRowSerializer method serialize.

@Override
public String serialize(byte[] rowKey, ResultCell[] cells) {
    final StringBuilder jsonBuilder = new StringBuilder();
    jsonBuilder.append("{");
    final String row = new String(rowKey, charset);
    jsonBuilder.append("\"row\":").append("\"").append(StringEscapeUtils.escapeJson(row)).append("\"");
    jsonBuilder.append(", \"cells\": {");
    int i = 0;
    for (final ResultCell cell : cells) {
        final String cellFamily = new String(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength(), charset);
        final String cellQualifier = new String(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength(), charset);
        if (i > 0) {
            jsonBuilder.append(", ");
        }
        jsonBuilder.append("\"").append(StringEscapeUtils.escapeJson(cellFamily)).append(":").append(StringEscapeUtils.escapeJson(cellQualifier)).append("\":\"").append(StringEscapeUtils.escapeJson(new String(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(), charset))).append("\"");
        i++;
    }
    jsonBuilder.append("}}");
    return jsonBuilder.toString();
}
Also used : ResultCell(org.apache.nifi.hbase.scan.ResultCell)

Example 15 with ResultCell

use of org.apache.nifi.hbase.scan.ResultCell in project nifi by apache.

the class MockHBaseClientService method scan.

@Override
public void scan(String tableName, byte[] startRow, byte[] endRow, Collection<Column> columns, ResultHandler handler) throws IOException {
    if (throwException) {
        throw new IOException("exception");
    }
    for (final Map.Entry<String, ResultCell[]> entry : results.entrySet()) {
        List<ResultCell> matchedCells = new ArrayList<>();
        if (columns == null || columns.isEmpty()) {
            Arrays.stream(entry.getValue()).forEach(e -> matchedCells.add(e));
        } else {
            for (Column column : columns) {
                String colFam = new String(column.getFamily(), StandardCharsets.UTF_8);
                String colQual = new String(column.getQualifier(), StandardCharsets.UTF_8);
                for (ResultCell cell : entry.getValue()) {
                    String cellFam = new String(cell.getFamilyArray(), StandardCharsets.UTF_8);
                    String cellQual = new String(cell.getQualifierArray(), StandardCharsets.UTF_8);
                    if (colFam.equals(cellFam) && colQual.equals(cellQual)) {
                        matchedCells.add(cell);
                    }
                }
            }
        }
        handler.handle(entry.getKey().getBytes(StandardCharsets.UTF_8), matchedCells.toArray(new ResultCell[matchedCells.size()]));
    }
    numScans++;
}
Also used : PutColumn(org.apache.nifi.hbase.put.PutColumn) Column(org.apache.nifi.hbase.scan.Column) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ResultCell(org.apache.nifi.hbase.scan.ResultCell) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ResultCell (org.apache.nifi.hbase.scan.ResultCell)17 HashMap (java.util.HashMap)4 Table (org.apache.hadoop.hbase.client.Table)4 IOException (java.io.IOException)3 Cell (org.apache.hadoop.hbase.Cell)3 Result (org.apache.hadoop.hbase.client.Result)3 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)3 ValidationResult (org.apache.nifi.components.ValidationResult)3 Before (org.junit.Before)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 PutColumn (org.apache.nifi.hbase.put.PutColumn)2 Column (org.apache.nifi.hbase.scan.Column)2 OutputStream (java.io.OutputStream)1 Charset (java.nio.charset.Charset)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Set (java.util.Set)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Filter (org.apache.hadoop.hbase.filter.Filter)1