public static String scanToString(BigtableExtendedScan scan) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] table = scan.getAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME); DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(table.length); dos.write(table); scan.getRowSet().writeTo(dos); dos.flush(); return Base64.getEncoder().encodeToString(baos.toByteArray()); }
public static BigtableExtendedScan stringToScan(String encoded) throws IOException { DataInputStream dis = new DataInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(encoded))); int tableLength = dis.readInt(); byte[] table = new byte[tableLength]; dis.read(table, 0, tableLength); int available = dis.available(); byte[] rowsetbytes = new byte[available]; dis.readFully(rowsetbytes); RowSet rs = RowSet.parseFrom(rowsetbytes); BigtableExtendedScan scan = new BigtableExtendedScan(); rs.getRowRangesList().forEach(scan::addRange); scan.setAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME, table); return scan; } }
/** * Creates a {@link RowRange} based on a prefix. This is similar to {@link Scan#setRowPrefixFilter(byte[])}. * @param prefix */ public void addRangeWithPrefix(byte[] prefix) { addRange(prefix, RowKeyUtil.calculateTheClosestNextRowKeyForPrefix(prefix)); }
/** * Restart from survivable exceptions by creating a new scanner. * * @throws IOException When restarting fails. */ public void restart() throws IOException { scan.setScanMetricsEnabled(true); if (this.scanner != null) { if (logScannerActivity) { LOG.info("Closing the previously opened scanner object."); } this.scanner.close(); } this.scanner = this.htable.getScanner(scan); if (logScannerActivity) { LOG.info("Current scan=" + scan.toString()); timestamp = System.currentTimeMillis(); rowcount = 0; } }
@Override public int compareTo(BigtableExtendedScanSplit o) { // If The table name of the two splits is the same then compare start row // otherwise compare based on table names int tableNameComparison = name.compareTo(o.name); return tableNameComparison != 0 ? tableNameComparison : Bytes.compareTo( scan.getRowSet().getRowRanges(0).getStartKeyClosed().toByteArray(), o.scan.getRowSet().getRowRanges(0).getStartKeyClosed().toByteArray()); }
/** * If hbase runs on new version of mapreduce, RecordReader has access to * counters thus can update counters based on scanMetrics. * If hbase runs on old version of mapreduce, it won't be able to get * access to counters and TableRecorderReader can't update counter values. * @throws IOException */ private void updateCounters() throws IOException { ScanMetrics scanMetrics = this.scan.getScanMetrics(); if (scanMetrics == null) { return; } updateCounters(scanMetrics, getCounter, context, numStale); }
private RowSet getRowSet(Scan scan) { if (scan instanceof BigtableExtendedScan) { return ((BigtableExtendedScan) scan).getRowSet(); } else { RowSet.Builder rowSetBuilder = RowSet.newBuilder();
private RangeSet<RowKeyWrapper> getRangeSet(Scan scan) { if (scan instanceof BigtableExtendedScan) { RowSet rowSet = ((BigtableExtendedScan) scan).getRowSet(); return rowRangeAdapter.rowSetToRangeSet(rowSet); } else { RangeSet<RowKeyWrapper> rangeSet = TreeRangeSet.create(); final ByteString startRow = ByteString.copyFrom(scan.getStartRow()); final ByteString stopRow = ByteString.copyFrom(scan.getStopRow()); if (scan.isGetScan()) { rangeSet.add(Range.singleton(new RowKeyWrapper(startRow))); } else { final BoundType startBound = (!OPEN_CLOSED_AVAILABLE || scan.includeStartRow()) ? BoundType.CLOSED : BoundType.OPEN; final BoundType endBound = (!OPEN_CLOSED_AVAILABLE || !scan.includeStopRow()) ? BoundType.OPEN : BoundType.CLOSED; rangeSet.add(rowRangeAdapter.boundedRange(startBound, startRow, endBound, stopRow)); } return rangeSet; } }
/** * Creates a {@link RowRange} based on a prefix. This is similar to {@link Scan#setRowPrefixFilter(byte[])}. * @param prefix */ public void addRangeWithPrefix(byte[] prefix) { addRange(prefix, RowKeyUtil.calculateTheClosestNextRowKeyForPrefix(prefix)); }
/** * Adds a range to scan. This is similar to calling a combination of * {@link Scan#setStartRow(byte[])} and {@link Scan#setStopRow(byte[])}. Other ranges can be * constructed by creating a {@link RowRange} and calling {@link #addRange(RowRange)} * @param startRow * @param stopRow */ public void addRange(byte[] startRow, byte[] stopRow) { addRange(RowRange.newBuilder() .setStartKeyClosed(ByteStringer.wrap(startRow)) .setEndKeyOpen(ByteStringer.wrap(stopRow)) .build()); }
/** * Adds a range to scan. This is similar to calling a combination of * {@link Scan#setStartRow(byte[])} and {@link Scan#setStopRow(byte[])}. Other ranges can be * constructed by creating a {@link RowRange} and calling {@link #addRange(RowRange)} * @param startRow * @param stopRow */ public void addRange(byte[] startRow, byte[] stopRow) { addRange(RowRange.newBuilder() .setStartKeyClosed(ByteStringer.wrap(startRow)) .setEndKeyOpen(ByteStringer.wrap(stopRow)) .build()); }