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);
}
}
}
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();
}
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();
}
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();
}
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++;
}
Aggregations