@Override public void handle(byte[] row, ResultCell[] resultCells) { numRows += 1; for( final ResultCell resultCell : resultCells ){ lastResultBytes = Arrays.copyOfRange(resultCell.getValueArray(), resultCell.getValueOffset(), resultCell.getValueLength() + resultCell.getValueOffset()); } } public int numRows() {
public boolean contains(final ResultCell cell) { if (cell.getTimestamp() != latestTimestamp) { return false; } final byte[] row = Arrays.copyOfRange(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength() + cell.getRowOffset()); final String rowHash = new String(row, StandardCharsets.UTF_8); final Set<String> cellHashes = matchingCellHashes.get(rowHash); if (cellHashes == null) { return false; } final byte[] cellValue = Arrays.copyOfRange(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength() + cell.getValueOffset()); final String cellHash = new String(cellValue, StandardCharsets.UTF_8); return cellHashes.contains(cellHash); }
/** * @param cell the cell to get the family from * @param charset the charset that was used to encode the cell's family * @param base64encodeValues whether or not to base64 encode the returned string * * @return the String representation of the cell's family */ public static String getCellFamily(final ResultCell cell, final Charset charset, final boolean base64encodeValues) { if (base64encodeValues) { ByteBuffer cellFamilyBuffer = ByteBuffer.wrap(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()); ByteBuffer base64Buffer = Base64.getEncoder().encode(cellFamilyBuffer); return new String(base64Buffer.array(), StandardCharsets.UTF_8); } else { return new String(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength(), charset); } }
private ResultCell getResultCell(Cell cell) { final ResultCell resultCell = new ResultCell(); resultCell.setRowArray(cell.getRowArray()); resultCell.setRowOffset(cell.getRowOffset()); resultCell.setRowLength(cell.getRowLength()); resultCell.setFamilyArray(cell.getFamilyArray()); resultCell.setFamilyOffset(cell.getFamilyOffset()); resultCell.setFamilyLength(cell.getFamilyLength()); resultCell.setQualifierArray(cell.getQualifierArray()); resultCell.setQualifierOffset(cell.getQualifierOffset()); resultCell.setQualifierLength(cell.getQualifierLength()); resultCell.setTimestamp(cell.getTimestamp()); resultCell.setTypeByte(cell.getTypeByte()); resultCell.setSequenceId(cell.getSequenceId()); resultCell.setValueArray(cell.getValueArray()); resultCell.setValueOffset(cell.getValueOffset()); resultCell.setValueLength(cell.getValueLength()); resultCell.setTagsArray(cell.getTagsArray()); resultCell.setTagsOffset(cell.getTagsOffset()); resultCell.setTagsLength(cell.getTagsLength()); return resultCell; }
@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(); }
/** * @param cell the cell to get the qualifier from * @param charset the charset that was used to encode the cell's qualifier * @param base64encodeValues whether or not to base64 encode the returned string * * @return the String representation of the cell's qualifier */ public static String getCellQualifier(final ResultCell cell, final Charset charset, final boolean base64encodeValues) { if (base64encodeValues) { ByteBuffer cellQualifierBuffer = ByteBuffer.wrap(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()); ByteBuffer base64Buffer = Base64.getEncoder().encode(cellQualifierBuffer); return new String(base64Buffer.array(), StandardCharsets.UTF_8); } else { return new String(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength(), charset); } }
protected ResultScanner getResults(final Table table, final byte[] startRow, final byte[] endRow, final Collection<Column> columns, List<String> authorizations) throws IOException { final Scan scan = new Scan(); scan.setStartRow(startRow); scan.setStopRow(endRow); if (authorizations != null && authorizations.size() > 0) { scan.setAuthorizations(new Authorizations(authorizations)); } if (columns != null && columns.size() > 0) { for (Column col : columns) { if (col.getQualifier() == null) { scan.addFamily(col.getFamily()); } else { scan.addColumn(col.getFamily(), col.getQualifier()); } } } return table.getScanner(scan); }
private List<Column> getColumns(final String columnsValue) { final String[] columns = (columnsValue == null || columnsValue.isEmpty() ? new String[0] : columnsValue.split(",")); final List<Column> columnsList = new ArrayList<>(); for (final String column : columns) { if (column.contains(":")) { final String[] parts = column.trim().split(":"); final byte[] cf = parts[0].getBytes(StandardCharsets.UTF_8); final byte[] cq = parts[1].getBytes(StandardCharsets.UTF_8); columnsList.add(new Column(cf, cq)); } else { final byte[] cf = column.trim().getBytes(StandardCharsets.UTF_8); columnsList.add(new Column(cf, null)); } } return columnsList; }
@Override public void scan(final String tableName, final byte[] startRow, final byte[] endRow, final Collection<Column> columns, List<String> authorizations, final ResultHandler handler) throws IOException { try (final Table table = connection.getTable(TableName.valueOf(tableName)); final ResultScanner scanner = getResults(table, startRow, endRow, columns, authorizations)) { 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); } } }
private ResultCell getResultCell(Cell cell) { final ResultCell resultCell = new ResultCell(); resultCell.setRowArray(cell.getRowArray()); resultCell.setRowOffset(cell.getRowOffset()); resultCell.setRowLength(cell.getRowLength()); resultCell.setFamilyArray(cell.getFamilyArray()); resultCell.setFamilyOffset(cell.getFamilyOffset()); resultCell.setFamilyLength(cell.getFamilyLength()); resultCell.setQualifierArray(cell.getQualifierArray()); resultCell.setQualifierOffset(cell.getQualifierOffset()); resultCell.setQualifierLength(cell.getQualifierLength()); resultCell.setTimestamp(cell.getTimestamp()); resultCell.setTypeByte(cell.getTypeByte()); resultCell.setSequenceId(cell.getSequenceId()); resultCell.setValueArray(cell.getValueArray()); resultCell.setValueOffset(cell.getValueOffset()); resultCell.setValueLength(cell.getValueLength()); resultCell.setTagsArray(cell.getTagsArray()); resultCell.setTagsOffset(cell.getTagsOffset()); resultCell.setTagsLength(cell.getTagsLength()); return resultCell; }
@Override public void handle(byte[] row, ResultCell[] resultCells) { numRows += 1; for( final ResultCell resultCell : resultCells ){ lastResultBytes = Arrays.copyOfRange(resultCell.getValueArray(), resultCell.getValueOffset(), resultCell.getValueLength() + resultCell.getValueOffset()); } } public int numRows() {
protected ResultScanner getResults(final Table table, final byte[] startRow, final byte[] endRow, final Collection<Column> columns, List<String> authorizations) throws IOException { final Scan scan = new Scan(); scan.setStartRow(startRow); scan.setStopRow(endRow); if (authorizations != null && authorizations.size() > 0) { scan.setAuthorizations(new Authorizations(authorizations)); } if (columns != null && columns.size() > 0) { for (Column col : columns) { if (col.getQualifier() == null) { scan.addFamily(col.getFamily()); } else { scan.addColumn(col.getFamily(), col.getQualifier()); } } } return table.getScanner(scan); }
private List<Column> getColumns(final String columnsValue) { final String[] columns = (columnsValue == null || columnsValue.isEmpty() ? new String[0] : columnsValue.split(",")); final List<Column> columnsList = new ArrayList<>(); for (final String column : columns) { if (column.contains(":")) { final String[] parts = column.trim().split(":"); final byte[] cf = parts[0].getBytes(StandardCharsets.UTF_8); final byte[] cq = parts[1].getBytes(StandardCharsets.UTF_8); columnsList.add(new Column(cf, cq)); } else { final byte[] cf = column.trim().getBytes(StandardCharsets.UTF_8); columnsList.add(new Column(cf, null)); } } return columnsList; }
@Override public void scan(final String tableName, final byte[] startRow, final byte[] endRow, final Collection<Column> columns, List<String> authorizations, final ResultHandler handler) throws IOException { try (final Table table = connection.getTable(TableName.valueOf(tableName)); final ResultScanner scanner = getResults(table, startRow, endRow, columns, authorizations)) { 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); } } }
/** * @param cell the cell to get the value from * @param charset the charset that was used to encode the cell's value * @param base64encodeValues whether or not to base64 encode the returned string * * @return the String representation of the cell's value */ public static String getCellValue(final ResultCell cell, final Charset charset, final boolean base64encodeValues) { if (base64encodeValues) { ByteBuffer cellValueBuffer = ByteBuffer.wrap(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); ByteBuffer base64Buffer = Base64.getEncoder().encode(cellValueBuffer); return new String(base64Buffer.array(), StandardCharsets.UTF_8); } else { return new String(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(), charset); } }
protected ResultScanner getResults(final Table table, final Collection<Column> columns, final Filter filter, final long minTime, List<String> authorizations) throws IOException { // Create a new scan. We will set the min timerange as the latest timestamp that // we have seen so far. The minimum timestamp is inclusive, so we will get duplicates. // We will record any cells that have the latest timestamp, so that when we scan again, // we know to throw away those duplicates. final Scan scan = new Scan(); scan.setTimeRange(minTime, Long.MAX_VALUE); if (authorizations != null && authorizations.size() > 0) { scan.setAuthorizations(new Authorizations(authorizations)); } if (filter != null) { scan.setFilter(filter); } if (columns != null) { for (Column col : columns) { if (col.getQualifier() == null) { scan.addFamily(col.getFamily()); } else { scan.addColumn(col.getFamily(), col.getQualifier()); } } } return table.getScanner(scan); }
/** * @param columnsValue a String in the form colFam:colQual,colFam:colQual * @return a list of Columns based on parsing the given String */ private List<Column> getColumns(final String columnsValue) { final String[] columns = (columnsValue == null || columnsValue.isEmpty() ? new String[0] : columnsValue.split(",")); List<Column> columnsList = new ArrayList<>(columns.length); for (final String column : columns) { if (column.contains(":")) { final String[] parts = column.split(":"); final byte[] cf = parts[0].getBytes(StandardCharsets.UTF_8); final byte[] cq = parts[1].getBytes(StandardCharsets.UTF_8); columnsList.add(new Column(cf, cq)); } else { final byte[] cf = column.getBytes(StandardCharsets.UTF_8); columnsList.add(new Column(cf, null)); } } return columnsList; }
@Override public void scan(final String tableName, final String startRow, final String endRow, String filterExpression, final Long timerangeMin, final Long timerangeMax, final Integer limitRows, final Boolean isReversed, final Collection<Column> columns, List<String> visibilityLabels, final ResultHandler handler) throws IOException { try (final Table table = connection.getTable(TableName.valueOf(tableName)); final ResultScanner scanner = getResults(table, startRow, endRow, filterExpression, timerangeMin, timerangeMax, limitRows, isReversed, columns, visibilityLabels)) { int cnt = 0; final int lim = limitRows != null ? limitRows : 0; for (final Result result : scanner) { if (lim > 0 && ++cnt > lim){ break; } 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); } } }
protected ResultScanner getResults(final Table table, final Collection<Column> columns, final Filter filter, final long minTime, List<String> authorizations) throws IOException { // Create a new scan. We will set the min timerange as the latest timestamp that // we have seen so far. The minimum timestamp is inclusive, so we will get duplicates. // We will record any cells that have the latest timestamp, so that when we scan again, // we know to throw away those duplicates. final Scan scan = new Scan(); scan.setTimeRange(minTime, Long.MAX_VALUE); if (authorizations != null && authorizations.size() > 0) { scan.setAuthorizations(new Authorizations(authorizations)); } if (filter != null) { scan.setFilter(filter); } if (columns != null) { for (Column col : columns) { if (col.getQualifier() == null) { scan.addFamily(col.getFamily()); } else { scan.addColumn(col.getFamily(), col.getQualifier()); } } } return table.getScanner(scan); }
/** * @param columnsValue a String in the form colFam:colQual,colFam:colQual * @return a list of Columns based on parsing the given String */ private List<Column> getColumns(final String columnsValue) { final String[] columns = (columnsValue == null || columnsValue.isEmpty() ? new String[0] : columnsValue.split(",")); List<Column> columnsList = new ArrayList<>(columns.length); for (final String column : columns) { if (column.contains(":")) { final String[] parts = column.split(":"); final byte[] cf = parts[0].getBytes(StandardCharsets.UTF_8); final byte[] cq = parts[1].getBytes(StandardCharsets.UTF_8); columnsList.add(new Column(cf, cq)); } else { final byte[] cf = column.getBytes(StandardCharsets.UTF_8); columnsList.add(new Column(cf, null)); } } return columnsList; }