Java Code Examples for org.apache.hadoop.hbase.client.Scan#withStopRow()

The following examples show how to use org.apache.hadoop.hbase.client.Scan#withStopRow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientMetaTableAccessor.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Performs a scan of META table for given table.
 * @param metaTable
 * @param startRow Where to start the scan
 * @param stopRow Where to stop the scan
 * @param type scanned part of meta
 * @param maxRows maximum rows to return
 * @param visitor Visitor invoked against each row
 */
private static CompletableFuture<Void> scanMeta(AsyncTable<AdvancedScanResultConsumer> metaTable,
  byte[] startRow, byte[] stopRow, QueryType type, int maxRows, final Visitor visitor) {
  int rowUpperLimit = maxRows > 0 ? maxRows : Integer.MAX_VALUE;
  Scan scan = getMetaScan(metaTable, rowUpperLimit);
  for (byte[] family : type.getFamilies()) {
    scan.addFamily(family);
  }
  if (startRow != null) {
    scan.withStartRow(startRow);
  }
  if (stopRow != null) {
    scan.withStopRow(stopRow);
  }

  if (LOG.isDebugEnabled()) {
    LOG.debug("Scanning META" + " starting at row=" + Bytes.toStringBinary(scan.getStartRow()) +
      " stopping at row=" + Bytes.toStringBinary(scan.getStopRow()) + " for max=" +
      rowUpperLimit + " with caching=" + scan.getCaching());
  }

  CompletableFuture<Void> future = new CompletableFuture<Void>();
  metaTable.scan(scan, new MetaTableScanResultConsumer(rowUpperLimit, visitor, future));
  return future;
}
 
Example 2
Source File: HashTable.java    From hbase with Apache License 2.0 6 votes vote down vote up
Scan initScan() throws IOException {
  Scan scan = new Scan();
  scan.setCacheBlocks(false);
  if (startTime != 0 || endTime != 0) {
    scan.setTimeRange(startTime, endTime == 0 ? HConstants.LATEST_TIMESTAMP : endTime);
  }
  if (scanBatch > 0) {
    scan.setBatch(scanBatch);
  }
  if (versions >= 0) {
    scan.readVersions(versions);
  }
  if (!isTableStartRow(startRow)) {
    scan.withStartRow(startRow);
  }
  if (!isTableEndRow(stopRow)) {
    scan.withStopRow(stopRow);
  }
  if(families != null) {
    for(String fam : families.split(",")) {
      scan.addFamily(Bytes.toBytes(fam));
    }
  }
  return scan;
}
 
Example 3
Source File: IndexVerificationResultRepository.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public IndexToolVerificationResult getVerificationResult(Table htable, long ts)
    throws IOException {
    byte[] startRowKey = Bytes.toBytes(Long.toString(ts));
    byte[] stopRowKey = ByteUtil.calculateTheClosestNextRowKeyForPrefix(startRowKey);
    IndexToolVerificationResult verificationResult = new IndexToolVerificationResult(ts);
    Scan scan = new Scan();
    scan.withStartRow(startRowKey);
    scan.withStopRow(stopRowKey);
    ResultScanner scanner = htable.getScanner(scan);
    for (Result result = scanner.next(); result != null; result = scanner.next()) {
        boolean isFirst = true;
        for (Cell cell : result.rawCells()) {
            if (isFirst){
                byte[][] rowKeyParts = ByteUtil.splitArrayBySeparator(result.getRow(),
                    ROW_KEY_SEPARATOR_BYTE[0]);
                verificationResult.setStartRow(rowKeyParts[3]);
                verificationResult.setStopRow(rowKeyParts[4]);
                isFirst = false;
            }
            verificationResult.update(cell);
        }
    }
    return verificationResult;
}
 
Example 4
Source File: TestFilterListOrOperatorWithBlkCnt.java    From hbase with Apache License 2.0 6 votes vote down vote up
private List<Cell> getScanResult(byte[] startRow, byte[] stopRow, Table ht) throws IOException {
  Scan scan = new Scan();
  scan.readAllVersions();
  if(!Bytes.toString(startRow).isEmpty()) {
    scan.withStartRow(startRow);
  }
  if(!Bytes.toString(stopRow).isEmpty()) {
    scan.withStopRow(stopRow);
  }
  ResultScanner scanner = ht.getScanner(scan);
  List<Cell> kvList = new ArrayList<>();
  Result r;
  while ((r = scanner.next()) != null) {
    for (Cell kv : r.listCells()) {
      kvList.add(kv);
    }
  }
  return kvList;
}
 
Example 5
Source File: BackupSystemTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Creates Scan to load table-> { RS -> ts} map of maps
 * @return scan operation
 */
private Scan createScanForReadLogTimestampMap(String backupRoot) {
  Scan scan = new Scan();
  byte[] startRow = rowkey(TABLE_RS_LOG_MAP_PREFIX, backupRoot);
  byte[] stopRow = Arrays.copyOf(startRow, startRow.length);
  stopRow[stopRow.length - 1] = (byte) (stopRow[stopRow.length - 1] + 1);
  scan.withStartRow(startRow);
  scan.withStopRow(stopRow);
  scan.addFamily(BackupSystemTable.META_FAMILY);

  return scan;
}
 
Example 6
Source File: BackupSystemTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Creates Scan operation to load last RS log roll results
 * @return scan operation
 */
private Scan createScanForReadRegionServerLastLogRollResult(String backupRoot) {
  Scan scan = new Scan();
  byte[] startRow = rowkey(RS_LOG_TS_PREFIX, backupRoot);
  byte[] stopRow = Arrays.copyOf(startRow, startRow.length);
  stopRow[stopRow.length - 1] = (byte) (stopRow[stopRow.length - 1] + 1);
  scan.withStartRow(startRow);
  scan.withStopRow(stopRow);
  scan.addFamily(BackupSystemTable.META_FAMILY);
  scan.readVersions(1);

  return scan;
}
 
Example 7
Source File: BackupSystemTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
static Scan createScanForOrigBulkLoadedFiles(TableName table) {
  Scan scan = new Scan();
  byte[] startRow = rowkey(BULK_LOAD_PREFIX, table.toString(), BLK_LD_DELIM);
  byte[] stopRow = Arrays.copyOf(startRow, startRow.length);
  stopRow[stopRow.length - 1] = (byte) (stopRow[stopRow.length - 1] + 1);
  scan.withStartRow(startRow);
  scan.withStopRow(stopRow);
  scan.addFamily(BackupSystemTable.META_FAMILY);
  scan.readVersions(1);
  return scan;
}
 
Example 8
Source File: BackupSystemTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Creates Scan operation to load backup history
 * @return scan operation
 */
private Scan createScanForBackupHistory() {
  Scan scan = new Scan();
  byte[] startRow = Bytes.toBytes(BACKUP_INFO_PREFIX);
  byte[] stopRow = Arrays.copyOf(startRow, startRow.length);
  stopRow[stopRow.length - 1] = (byte) (stopRow[stopRow.length - 1] + 1);
  scan.withStartRow(startRow);
  scan.withStopRow(stopRow);
  scan.addFamily(BackupSystemTable.SESSIONS_FAMILY);
  scan.readVersions(1);
  return scan;
}
 
Example 9
Source File: BackupSystemTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Creates Scan operation to load WALs
 * @param backupRoot path to backup destination
 * @return scan operation
 */
private Scan createScanForGetWALs(String backupRoot) {
  // TODO: support for backupRoot
  Scan scan = new Scan();
  byte[] startRow = Bytes.toBytes(WALS_PREFIX);
  byte[] stopRow = Arrays.copyOf(startRow, startRow.length);
  stopRow[stopRow.length - 1] = (byte) (stopRow[stopRow.length - 1] + 1);
  scan.withStartRow(startRow);
  scan.withStopRow(stopRow);
  scan.addFamily(BackupSystemTable.META_FAMILY);
  return scan;
}
 
Example 10
Source File: BackupSystemTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Creates Scan operation to load backup set list
 * @return scan operation
 */
private Scan createScanForBackupSetList() {
  Scan scan = new Scan();
  byte[] startRow = Bytes.toBytes(SET_KEY_PREFIX);
  byte[] stopRow = Arrays.copyOf(startRow, startRow.length);
  stopRow[stopRow.length - 1] = (byte) (stopRow[stopRow.length - 1] + 1);
  scan.withStartRow(startRow);
  scan.withStopRow(stopRow);
  scan.addFamily(BackupSystemTable.META_FAMILY);
  return scan;
}
 
Example 11
Source File: MetaTableAccessor.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * This method creates a Scan object that will only scan catalog rows that belong to the specified
 * table. It doesn't specify any columns. This is a better alternative to just using a start row
 * and scan until it hits a new table since that requires parsing the HRI to get the table name.
 * @param tableName bytes of table's name
 * @return configured Scan object
 * @deprecated This is internal so please remove it when we get a chance.
 */
@Deprecated
public static Scan getScanForTableName(Connection connection, TableName tableName) {
  // Start key is just the table name with delimiters
  byte[] startKey = ClientMetaTableAccessor.getTableStartRowForMeta(tableName, QueryType.REGION);
  // Stop key appends the smallest possible char to the table name
  byte[] stopKey = ClientMetaTableAccessor.getTableStopRowForMeta(tableName, QueryType.REGION);

  Scan scan = getMetaScan(connection, -1);
  scan.withStartRow(startKey);
  scan.withStopRow(stopKey);
  return scan;
}
 
Example 12
Source File: TestTableInputFormat.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Create table data and run tests on specified htable using the
 * o.a.h.hbase.mapreduce API.
 *
 * @param table
 * @throws IOException
 * @throws InterruptedException
 */
static void runTestMapreduce(Table table) throws IOException,
    InterruptedException {
  org.apache.hadoop.hbase.mapreduce.TableRecordReaderImpl trr =
      new org.apache.hadoop.hbase.mapreduce.TableRecordReaderImpl();
  Scan s = new Scan();
  s.withStartRow(Bytes.toBytes("aaa"));
  s.withStopRow(Bytes.toBytes("zzz"));
  s.addFamily(FAMILY);
  trr.setScan(s);
  trr.setHTable(table);

  trr.initialize(null, null);
  Result r = new Result();
  ImmutableBytesWritable key = new ImmutableBytesWritable();

  boolean more = trr.nextKeyValue();
  assertTrue(more);
  key = trr.getCurrentKey();
  r = trr.getCurrentValue();
  checkResult(r, key, Bytes.toBytes("aaa"), Bytes.toBytes("value aaa"));

  more = trr.nextKeyValue();
  assertTrue(more);
  key = trr.getCurrentKey();
  r = trr.getCurrentValue();
  checkResult(r, key, Bytes.toBytes("bbb"), Bytes.toBytes("value bbb"));

  // no more data
  more = trr.nextKeyValue();
  assertFalse(more);
}
 
Example 13
Source File: IndexVerificationOutputRepository.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public Iterator<IndexVerificationOutputRow> getOutputRowIterator(long ts, byte[] indexName)
    throws IOException {
    Scan scan = new Scan();
    byte[] partialKey = generatePartialOutputTableRowKey(ts, indexName);
    scan.withStartRow(partialKey);
    scan.withStopRow(ByteUtil.calculateTheClosestNextRowKeyForPrefix(partialKey));
    ResultScanner scanner = outputTable.getScanner(scan);
    return new IndexVerificationOutputRowIterator(scanner.iterator());
}
 
Example 14
Source File: IntegrationTestBigLinkedList.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int run(String[] args) throws Exception {
  Options options = new Options();
  options.addOption("s", "start", true, "start key");
  options.addOption("e", "end", true, "end key");
  options.addOption("l", "limit", true, "number to print");

  GnuParser parser = new GnuParser();
  CommandLine cmd = null;
  try {
    cmd = parser.parse(options, args);
    if (cmd.getArgs().length != 0) {
      throw new ParseException("Command takes no arguments");
    }
  } catch (ParseException e) {
    System.err.println("Failed to parse command line " + e.getMessage());
    System.err.println();
    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp(getClass().getSimpleName(), options);
    System.exit(-1);
  }

  Connection connection = ConnectionFactory.createConnection(getConf());
  Table table = connection.getTable(getTableName(getConf()));

  Scan scan = new Scan();
  scan.setBatch(10000);

  if (cmd.hasOption("s"))
    scan.withStartRow(Bytes.toBytesBinary(cmd.getOptionValue("s")));

  if (cmd.hasOption("e")) {
    scan.withStopRow(Bytes.toBytesBinary(cmd.getOptionValue("e")));
  }

  int limit = 0;
  if (cmd.hasOption("l"))
    limit = Integer.parseInt(cmd.getOptionValue("l"));
  else
    limit = 100;

  ResultScanner scanner = table.getScanner(scan);

  CINode node = new CINode();
  Result result = scanner.next();
  int count = 0;
  while (result != null && count++ < limit) {
    node = getCINode(result, node);
    System.out.printf("%s:%s:%012d:%s\n", Bytes.toStringBinary(node.key),
        Bytes.toStringBinary(node.prev), node.count, node.client);
    result = scanner.next();
  }
  scanner.close();
  table.close();
  connection.close();

  return 0;
}
 
Example 15
Source File: SyncTable.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Rescan the given range directly from the source and target tables.
 * Count and log differences, and if this is not a dry run, output Puts and Deletes
 * to make the target table match the source table for this range
 */
private void syncRange(Context context, ImmutableBytesWritable startRow,
    ImmutableBytesWritable stopRow) throws IOException, InterruptedException {
  Scan scan = sourceTableHash.initScan();
  scan.withStartRow(startRow.copyBytes());
  scan.withStopRow(stopRow.copyBytes());

  ResultScanner sourceScanner = sourceTable.getScanner(scan);
  CellScanner sourceCells = new CellScanner(sourceScanner.iterator());

  ResultScanner targetScanner = targetTable.getScanner(new Scan(scan));
  CellScanner targetCells = new CellScanner(targetScanner.iterator());

  boolean rangeMatched = true;
  byte[] nextSourceRow = sourceCells.nextRow();
  byte[] nextTargetRow = targetCells.nextRow();
  while(nextSourceRow != null || nextTargetRow != null) {
    boolean rowMatched;
    int rowComparison = compareRowKeys(nextSourceRow, nextTargetRow);
    if (rowComparison < 0) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Target missing row: " + Bytes.toString(nextSourceRow));
      }
      context.getCounter(Counter.TARGETMISSINGROWS).increment(1);

      rowMatched = syncRowCells(context, nextSourceRow, sourceCells, EMPTY_CELL_SCANNER);
      nextSourceRow = sourceCells.nextRow();  // advance only source to next row
    } else if (rowComparison > 0) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Source missing row: " + Bytes.toString(nextTargetRow));
      }
      context.getCounter(Counter.SOURCEMISSINGROWS).increment(1);

      rowMatched = syncRowCells(context, nextTargetRow, EMPTY_CELL_SCANNER, targetCells);
      nextTargetRow = targetCells.nextRow();  // advance only target to next row
    } else {
      // current row is the same on both sides, compare cell by cell
      rowMatched = syncRowCells(context, nextSourceRow, sourceCells, targetCells);
      nextSourceRow = sourceCells.nextRow();
      nextTargetRow = targetCells.nextRow();
    }

    if (!rowMatched) {
      rangeMatched = false;
    }
  }

  sourceScanner.close();
  targetScanner.close();

  context.getCounter(rangeMatched ? Counter.RANGESMATCHED : Counter.RANGESNOTMATCHED)
    .increment(1);
}
 
Example 16
Source File: VerifyReplication.java    From hbase with Apache License 2.0 4 votes vote down vote up
private static void setStartAndStopRows(Scan scan, byte[] startPrefixRow, byte[] lastPrefixRow) {
  scan.withStartRow(startPrefixRow);
  byte[] stopRow = Bytes.add(Bytes.head(lastPrefixRow, lastPrefixRow.length - 1),
      new byte[]{(byte) (lastPrefixRow[lastPrefixRow.length - 1] + 1)});
  scan.withStopRow(stopRow);
}
 
Example 17
Source File: TestCleanupCompactedFileOnRegionClose.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testCleanupOnClose() throws Exception {
  TableName tableName = TableName.valueOf("testCleanupOnClose");
  String familyName = "f";
  byte[] familyNameBytes = Bytes.toBytes(familyName);
  util.createTable(tableName, familyName);

  Admin hBaseAdmin = util.getAdmin();
  Table table = util.getConnection().getTable(tableName);

  HRegionServer rs = util.getRSForFirstRegionInTable(tableName);
  Region region = rs.getRegions(tableName).get(0);

  int refSFCount = 4;
  for (int i = 0; i < refSFCount; i++) {
    for (int j = 0; j < refSFCount; j++) {
      Put put = new Put(Bytes.toBytes(j));
      put.addColumn(familyNameBytes, Bytes.toBytes(i), Bytes.toBytes(j));
      table.put(put);
    }
    util.flush(tableName);
  }
  assertEquals(refSFCount, region.getStoreFileList(new byte[][]{familyNameBytes}).size());

  //add a delete, to test wether we end up with an inconsistency post region close
  Delete delete = new Delete(Bytes.toBytes(refSFCount-1));
  table.delete(delete);
  util.flush(tableName);
  assertFalse(table.exists(new Get(Bytes.toBytes(refSFCount-1))));

  //Create a scanner and keep it open to add references to StoreFileReaders
  Scan scan = new Scan();
  scan.withStopRow(Bytes.toBytes(refSFCount-2));
  scan.setCaching(1);
  ResultScanner scanner = table.getScanner(scan);
  Result res = scanner.next();
  assertNotNull(res);
  assertEquals(refSFCount, res.getFamilyMap(familyNameBytes).size());


  //Verify the references
  int count = 0;
  for (HStoreFile sf : (Collection<HStoreFile>)region.getStore(familyNameBytes).getStorefiles()) {
    synchronized (sf) {
      if (count < refSFCount) {
        assertTrue(sf.isReferencedInReads());
      } else {
        assertFalse(sf.isReferencedInReads());
      }
    }
    count++;
  }

  //Major compact to produce compacted storefiles that need to be cleaned up
  util.compact(tableName, true);
  assertEquals(1, region.getStoreFileList(new byte[][]{familyNameBytes}).size());
  assertEquals(refSFCount+1,
    ((HStore)region.getStore(familyNameBytes)).getStoreEngine().getStoreFileManager()
        .getCompactedfiles().size());

  //close then open the region to determine wether compacted storefiles get cleaned up on close
  hBaseAdmin.unassign(region.getRegionInfo().getRegionName(), false);
  hBaseAdmin.assign(region.getRegionInfo().getRegionName());
  util.waitUntilNoRegionsInTransition(10000);


  assertFalse("Deleted row should not exist",
      table.exists(new Get(Bytes.toBytes(refSFCount-1))));

  rs = util.getRSForFirstRegionInTable(tableName);
  region = rs.getRegions(tableName).get(0);
  assertEquals(1, region.getStoreFileList(new byte[][]{familyNameBytes}).size());
  assertEquals(0,
      ((HStore)region.getStore(familyNameBytes)).getStoreEngine().getStoreFileManager()
          .getCompactedfiles().size());
}
 
Example 18
Source File: ThriftHBaseServiceHandler.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int scannerOpenWithScan(ByteBuffer tableName, TScan tScan,
    Map<ByteBuffer, ByteBuffer> attributes)
    throws IOError {

  Table table = null;
  try {
    table = getTable(tableName);
    Scan scan = new Scan();
    addAttributes(scan, attributes);
    if (tScan.isSetStartRow()) {
      scan.withStartRow(tScan.getStartRow());
    }
    if (tScan.isSetStopRow()) {
      scan.withStopRow(tScan.getStopRow());
    }
    if (tScan.isSetTimestamp()) {
      scan.setTimeRange(0, tScan.getTimestamp());
    }
    if (tScan.isSetCaching()) {
      scan.setCaching(tScan.getCaching());
    }
    if (tScan.isSetBatchSize()) {
      scan.setBatch(tScan.getBatchSize());
    }
    if (tScan.isSetColumns() && !tScan.getColumns().isEmpty()) {
      for(ByteBuffer column : tScan.getColumns()) {
        byte [][] famQf = CellUtil.parseColumn(getBytes(column));
        if(famQf.length == 1) {
          scan.addFamily(famQf[0]);
        } else {
          scan.addColumn(famQf[0], famQf[1]);
        }
      }
    }
    if (tScan.isSetFilterString()) {
      ParseFilter parseFilter = new ParseFilter();
      scan.setFilter(
          parseFilter.parseFilterString(tScan.getFilterString()));
    }
    if (tScan.isSetReversed()) {
      scan.setReversed(tScan.isReversed());
    }
    if (tScan.isSetCacheBlocks()) {
      scan.setCacheBlocks(tScan.isCacheBlocks());
    }
    return addScanner(table.getScanner(scan), tScan.sortColumns);
  } catch (IOException e) {
    LOG.warn(e.getMessage(), e);
    throw getIOError(e);
  } finally{
    closeTable(table);
  }
}
 
Example 19
Source File: ThriftUtilities.java    From hbase with Apache License 2.0 4 votes vote down vote up
public static Scan scanFromThrift(TScan in) throws IOException {
  Scan out = new Scan();

  if (in.isSetStartRow()) {
    out.withStartRow(in.getStartRow());
  }
  if (in.isSetStopRow()) {
    out.withStopRow(in.getStopRow());
  }
  if (in.isSetCaching()) {
    out.setCaching(in.getCaching());
  }
  if (in.isSetMaxVersions()) {
    out.readVersions(in.getMaxVersions());
  }

  if (in.isSetColumns()) {
    for (TColumn column : in.getColumns()) {
      if (column.isSetQualifier()) {
        out.addColumn(column.getFamily(), column.getQualifier());
      } else {
        out.addFamily(column.getFamily());
      }
    }
  }

  TTimeRange timeRange = in.getTimeRange();
  if (timeRange != null &&
      timeRange.isSetMinStamp() && timeRange.isSetMaxStamp()) {
    out.setTimeRange(timeRange.getMinStamp(), timeRange.getMaxStamp());
  }

  if (in.isSetBatchSize()) {
    out.setBatch(in.getBatchSize());
  }

  if (in.isSetFilterString()) {
    ParseFilter parseFilter = new ParseFilter();
    out.setFilter(parseFilter.parseFilterString(in.getFilterString()));
  }

  if (in.isSetAttributes()) {
    addAttributes(out,in.getAttributes());
  }

  if (in.isSetAuthorizations()) {
    out.setAuthorizations(new Authorizations(in.getAuthorizations().getLabels()));
  }

  if (in.isSetReversed()) {
    out.setReversed(in.isReversed());
  }

  if (in.isSetCacheBlocks()) {
    out.setCacheBlocks(in.isCacheBlocks());
  }

  if (in.isSetColFamTimeRangeMap()) {
    Map<ByteBuffer, TTimeRange> colFamTimeRangeMap = in.getColFamTimeRangeMap();
    if (MapUtils.isNotEmpty(colFamTimeRangeMap)) {
      for (Map.Entry<ByteBuffer, TTimeRange> entry : colFamTimeRangeMap.entrySet()) {
        out.setColumnFamilyTimeRange(Bytes.toBytes(entry.getKey()),
            entry.getValue().getMinStamp(), entry.getValue().getMaxStamp());
      }
    }
  }

  if (in.isSetReadType()) {
    out.setReadType(readTypeFromThrift(in.getReadType()));
  }

  if (in.isSetLimit()) {
    out.setLimit(in.getLimit());
  }

  if (in.isSetConsistency()) {
    out.setConsistency(consistencyFromThrift(in.getConsistency()));
  }

  if (in.isSetTargetReplicaId()) {
    out.setReplicaId(in.getTargetReplicaId());
  }

  if (in.isSetFilterBytes()) {
    out.setFilter(filterFromThrift(in.getFilterBytes()));
  }

  return out;
}
 
Example 20
Source File: CopyTable.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Sets up the actual job.
 *
 * @param args  The command line parameters.
 * @return The newly created job.
 * @throws IOException When setting up the job fails.
 */
public Job createSubmittableJob(String[] args) throws IOException {
  if (!doCommandLine(args)) {
    return null;
  }

  String jobName = NAME + "_" + (tableName == null ? snapshot : tableName);
  Job job = Job.getInstance(getConf(), getConf().get(JOB_NAME_CONF_KEY, jobName));
  job.setJarByClass(CopyTable.class);
  Scan scan = new Scan();

  scan.setBatch(batch);
  scan.setCacheBlocks(false);

  if (cacheRow > 0) {
    scan.setCaching(cacheRow);
  } else {
    scan.setCaching(getConf().getInt(HConstants.HBASE_CLIENT_SCANNER_CACHING, 100));
  }

  scan.setTimeRange(startTime, endTime);

  if (allCells) {
    scan.setRaw(true);
  }
  if (shuffle) {
    job.getConfiguration().set(TableInputFormat.SHUFFLE_MAPS, "true");
  }
  if (versions >= 0) {
    scan.readVersions(versions);
  }

  if (startRow != null) {
    scan.withStartRow(Bytes.toBytesBinary(startRow));
  }

  if (stopRow != null) {
    scan.withStopRow(Bytes.toBytesBinary(stopRow));
  }

  if(families != null) {
    String[] fams = families.split(",");
    Map<String,String> cfRenameMap = new HashMap<>();
    for(String fam : fams) {
      String sourceCf;
      if(fam.contains(":")) {
          // fam looks like "sourceCfName:destCfName"
          String[] srcAndDest = fam.split(":", 2);
          sourceCf = srcAndDest[0];
          String destCf = srcAndDest[1];
          cfRenameMap.put(sourceCf, destCf);
      } else {
          // fam is just "sourceCf"
          sourceCf = fam;
      }
      scan.addFamily(Bytes.toBytes(sourceCf));
    }
    Import.configureCfRenaming(job.getConfiguration(), cfRenameMap);
  }
  job.setNumReduceTasks(0);

  if (bulkload) {
    initCopyTableMapperReducerJob(job, scan);

    // We need to split the inputs by destination tables so that output of Map can be bulk-loaded.
    TableInputFormat.configureSplitTable(job, TableName.valueOf(dstTableName));

    bulkloadDir = generateUniqTempDir(false);
    LOG.info("HFiles will be stored at " + this.bulkloadDir);
    HFileOutputFormat2.setOutputPath(job, bulkloadDir);
    try (Connection conn = ConnectionFactory.createConnection(getConf());
        Admin admin = conn.getAdmin()) {
      HFileOutputFormat2.configureIncrementalLoadMap(job,
        admin.getDescriptor((TableName.valueOf(dstTableName))));
    }
  } else {
    initCopyTableMapperReducerJob(job, scan);
    TableMapReduceUtil.initTableReducerJob(dstTableName, null, job, null, peerAddress, null,
      null);
  }

  return job;
}