Java Code Examples for org.apache.hadoop.hbase.client.Result#advance()

The following examples show how to use org.apache.hadoop.hbase.client.Result#advance() . These examples are extracted from open source projects. 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 Project: hbase   File: BackupSystemTable.java    License: Apache License 2.0 6 votes vote down vote up
Map<byte[], String> readBulkLoadedFiles(String backupId) throws IOException {
  Scan scan = BackupSystemTable.createScanForBulkLoadedFiles(backupId);
  try (Table table = connection.getTable(bulkLoadTableName);
      ResultScanner scanner = table.getScanner(scan)) {
    Result res = null;
    Map<byte[], String> map = new TreeMap<>(Bytes.BYTES_COMPARATOR);
    while ((res = scanner.next()) != null) {
      res.advance();
      byte[] row = CellUtil.cloneRow(res.listCells().get(0));
      for (Cell cell : res.listCells()) {
        if (CellUtil.compareQualifiers(cell, BackupSystemTable.PATH_COL, 0,
          BackupSystemTable.PATH_COL.length) == 0) {
          map.put(row, Bytes.toString(CellUtil.cloneValue(cell)));
        }
      }
    }
    return map;
  }
}
 
Example 2
Source Project: hbase   File: BackupSystemTable.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Get the Region Servers log information after the last log roll from backup system table.
 * @param backupRoot root directory path to backup
 * @return RS log info
 * @throws IOException exception
 */
public HashMap<String, Long> readRegionServerLastLogRollResult(String backupRoot)
    throws IOException {
  LOG.trace("read region server last roll log result to backup system table");

  Scan scan = createScanForReadRegionServerLastLogRollResult(backupRoot);

  try (Table table = connection.getTable(tableName);
      ResultScanner scanner = table.getScanner(scan)) {
    Result res;
    HashMap<String, Long> rsTimestampMap = new HashMap<>();
    while ((res = scanner.next()) != null) {
      res.advance();
      Cell cell = res.current();
      byte[] row = CellUtil.cloneRow(cell);
      String server = getServerNameForReadRegionServerLastLogRollResult(row);
      byte[] data = CellUtil.cloneValue(cell);
      rsTimestampMap.put(server, Bytes.toLong(data));
    }
    return rsTimestampMap;
  }
}
 
Example 3
Source Project: hbase   File: BackupSystemTable.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Get all backup sessions with a given state (in descending order by time)
 * @param state backup session state
 * @return history info of backup info objects
 * @throws IOException exception
 */
public ArrayList<BackupInfo> getBackupInfos(BackupState state) throws IOException {
  LOG.trace("get backup infos from backup system table");

  Scan scan = createScanForBackupHistory();
  ArrayList<BackupInfo> list = new ArrayList<>();

  try (Table table = connection.getTable(tableName);
      ResultScanner scanner = table.getScanner(scan)) {
    Result res;
    while ((res = scanner.next()) != null) {
      res.advance();
      BackupInfo context = cellToBackupInfo(res.current());
      if (state != BackupState.ANY && context.getState() != state) {
        continue;
      }
      list.add(context);
    }
    return list;
  }
}
 
Example 4
Source Project: hbase   File: BackupSystemTable.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Get backup set list
 * @return backup set list
 * @throws IOException if a table or scanner operation fails
 */
public List<String> listBackupSets() throws IOException {
  LOG.trace("Backup set list");

  List<String> list = new ArrayList<>();
  try (Table table = connection.getTable(tableName)) {
    Scan scan = createScanForBackupSetList();
    scan.readVersions(1);
    try (ResultScanner scanner = table.getScanner(scan)) {
      Result res;
      while ((res = scanner.next()) != null) {
        res.advance();
        list.add(cellKeyToBackupSetName(res.current()));
      }
      return list;
    }
  }
}
 
Example 5
Source Project: hbase   File: BackupSystemTable.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Get backup set description (list of tables)
 * @param name set's name
 * @return list of tables in a backup set
 * @throws IOException if a table operation fails
 */
public List<TableName> describeBackupSet(String name) throws IOException {
  if (LOG.isTraceEnabled()) {
    LOG.trace(" Backup set describe: " + name);
  }
  try (Table table = connection.getTable(tableName)) {
    Get get = createGetForBackupSet(name);
    Result res = table.get(get);
    if (res.isEmpty()) {
      return null;
    }
    res.advance();
    String[] tables = cellValueToBackupSet(res.current());
    return Arrays.asList(tables).stream().map(item -> TableName.valueOf(item))
        .collect(Collectors.toList());
  }
}
 
Example 6
Source Project: hbase   File: BackupSystemTable.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Add backup set (list of tables)
 * @param name set name
 * @param newTables list of tables, comma-separated
 * @throws IOException if a table operation fails
 */
public void addToBackupSet(String name, String[] newTables) throws IOException {
  if (LOG.isTraceEnabled()) {
    LOG.trace("Backup set add: " + name + " tables [" + StringUtils.join(newTables, " ") + "]");
  }
  String[] union = null;
  try (Table table = connection.getTable(tableName)) {
    Get get = createGetForBackupSet(name);
    Result res = table.get(get);
    if (res.isEmpty()) {
      union = newTables;
    } else {
      res.advance();
      String[] tables = cellValueToBackupSet(res.current());
      union = merge(tables, newTables);
    }
    Put put = createPutForBackupSet(name, union);
    table.put(put);
  }
}
 
Example 7
Source Project: hbase   File: BackupSystemTable.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Read the timestamp for each region server log after the last successful backup. Each table has
 * its own set of the timestamps. The info is stored for each table as a concatenated string of
 * rs->timestapmp
 * @param backupRoot root directory path to backup
 * @return the timestamp for each region server. key: tableName value:
 *         RegionServer,PreviousTimeStamp
 * @throws IOException exception
 */
public HashMap<TableName, HashMap<String, Long>> readLogTimestampMap(String backupRoot)
    throws IOException {
  if (LOG.isTraceEnabled()) {
    LOG.trace("read RS log ts from backup system table for root=" + backupRoot);
  }

  HashMap<TableName, HashMap<String, Long>> tableTimestampMap = new HashMap<>();

  Scan scan = createScanForReadLogTimestampMap(backupRoot);
  try (Table table = connection.getTable(tableName);
      ResultScanner scanner = table.getScanner(scan)) {
    Result res;
    while ((res = scanner.next()) != null) {
      res.advance();
      Cell cell = res.current();
      byte[] row = CellUtil.cloneRow(cell);
      String tabName = getTableNameForReadLogTimestampMap(row);
      TableName tn = TableName.valueOf(tabName);
      byte[] data = CellUtil.cloneValue(cell);
      if (data == null) {
        throw new IOException("Data of last backup data from backup system table "
            + "is empty. Create a backup first.");
      }
      if (data != null && data.length > 0) {
        HashMap<String, Long> lastBackup =
            fromTableServerTimestampProto(BackupProtos.TableServerTimestamp.parseFrom(data));
        tableTimestampMap.put(tn, lastBackup);
      }
    }
    return tableTimestampMap;
  }
}
 
Example 8
Source Project: hbase   File: BackupSystemTable.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Remove tables from backup set (list of tables)
 * @param name set name
 * @param toRemove list of tables
 * @throws IOException if a table operation or deleting the backup set fails
 */
public void removeFromBackupSet(String name, String[] toRemove) throws IOException {
  if (LOG.isTraceEnabled()) {
    LOG.trace(
      " Backup set remove from : " + name + " tables [" + StringUtils.join(toRemove, " ") + "]");
  }
  String[] disjoint;
  String[] tables;
  try (Table table = connection.getTable(tableName)) {
    Get get = createGetForBackupSet(name);
    Result res = table.get(get);
    if (res.isEmpty()) {
      LOG.warn("Backup set '" + name + "' not found.");
      return;
    } else {
      res.advance();
      tables = cellValueToBackupSet(res.current());
      disjoint = disjoin(tables, toRemove);
    }
    if (disjoint.length > 0 && disjoint.length != tables.length) {
      Put put = createPutForBackupSet(name, disjoint);
      table.put(put);
    } else if (disjoint.length == tables.length) {
      LOG.warn("Backup set '" + name + "' does not contain tables ["
          + StringUtils.join(toRemove, " ") + "]");
    } else { // disjoint.length == 0 and tables.length >0
      // Delete backup set
      LOG.info("Backup set '" + name + "' is empty. Deleting.");
      deleteBackupSet(name);
    }
  }
}
 
Example 9
Source Project: hbase   File: FileArchiverNotifierImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Extracts the size component from a serialized {@link SpaceQuotaSnapshot} protobuf.
 *
 * @param r A Result containing one cell with a SpaceQuotaSnapshot protobuf
 * @return The size in bytes of the snapshot.
 */
long getSnapshotSizeFromResult(Result r) throws InvalidProtocolBufferException {
  // Per javadoc, Result should only be null if an exception was thrown. So, if we're here,
  // we should be non-null. If we can't advance to the first cell, same as "no cell".
  if (!r.isEmpty() && r.advance()) {
    return QuotaTableUtil.parseSnapshotSize(r.current());
  }
  return 0L;
}
 
Example 10
private long count(Table t) throws IOException {
  try (ResultScanner rs = t.getScanner(new Scan())) {
    long sum = 0;
    for (Result r : rs) {
      while (r.advance()) {
        sum++;
      }
    }
    return sum;
  }
}
 
Example 11
Source Project: hbase   File: QuotaTableUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Fetches the computed size of all snapshots against tables in a namespace for space quotas.
 */
static long getNamespaceSnapshotSize(
    Connection conn, String namespace) throws IOException {
  try (Table quotaTable = conn.getTable(QuotaTableUtil.QUOTA_TABLE_NAME)) {
    Result r = quotaTable.get(createGetNamespaceSnapshotSize(namespace));
    if (r.isEmpty()) {
      return 0L;
    }
    r.advance();
    return parseSnapshotSize(r.current());
  } catch (InvalidProtocolBufferException e) {
    throw new IOException("Could not parse snapshot size value for namespace " + namespace, e);
  }
}
 
Example 12
Source Project: hbase   File: BackupSystemTable.java    License: Apache License 2.0 4 votes vote down vote up
public Map<byte[], List<Path>>[] readBulkLoadedFiles(String backupId, List<TableName> sTableList)
    throws IOException {
  Scan scan = BackupSystemTable.createScanForBulkLoadedFiles(backupId);
  Map<byte[], List<Path>>[] mapForSrc = new Map[sTableList == null ? 1 : sTableList.size()];
  try (Table table = connection.getTable(bulkLoadTableName);
      ResultScanner scanner = table.getScanner(scan)) {
    Result res = null;
    while ((res = scanner.next()) != null) {
      res.advance();
      TableName tbl = null;
      byte[] fam = null;
      String path = null;
      for (Cell cell : res.listCells()) {
        if (CellUtil.compareQualifiers(cell, BackupSystemTable.TBL_COL, 0,
          BackupSystemTable.TBL_COL.length) == 0) {
          tbl = TableName.valueOf(CellUtil.cloneValue(cell));
        } else if (CellUtil.compareQualifiers(cell, BackupSystemTable.FAM_COL, 0,
          BackupSystemTable.FAM_COL.length) == 0) {
          fam = CellUtil.cloneValue(cell);
        } else if (CellUtil.compareQualifiers(cell, BackupSystemTable.PATH_COL, 0,
          BackupSystemTable.PATH_COL.length) == 0) {
          path = Bytes.toString(CellUtil.cloneValue(cell));
        }
      }
      int srcIdx = IncrementalTableBackupClient.getIndex(tbl, sTableList);
      if (srcIdx == -1) {
        // the table is not among the query
        continue;
      }
      if (mapForSrc[srcIdx] == null) {
        mapForSrc[srcIdx] = new TreeMap<>(Bytes.BYTES_COMPARATOR);
      }
      List<Path> files;
      if (!mapForSrc[srcIdx].containsKey(fam)) {
        files = new ArrayList<Path>();
        mapForSrc[srcIdx].put(fam, files);
      } else {
        files = mapForSrc[srcIdx].get(fam);
      }
      files.add(new Path(path));
      if (LOG.isDebugEnabled()) {
        LOG.debug("found bulk loaded file : " + tbl + " " + Bytes.toString(fam) + " " + path);
      }
    }

    return mapForSrc;
  }
}
 
Example 13
Source Project: hbase   File: BackupSystemTable.java    License: Apache License 2.0 4 votes vote down vote up
public Pair<Map<TableName, Map<String, Map<String, List<Pair<String, Boolean>>>>>, List<byte[]>>
  readBulkloadRows(List<TableName> tableList) throws IOException {

  Map<TableName, Map<String, Map<String, List<Pair<String, Boolean>>>>> map = new HashMap<>();
  List<byte[]> rows = new ArrayList<>();
  for (TableName tTable : tableList) {
    Scan scan = BackupSystemTable.createScanForOrigBulkLoadedFiles(tTable);
    Map<String, Map<String, List<Pair<String, Boolean>>>> tblMap = map.get(tTable);
    try (Table table = connection.getTable(bulkLoadTableName);
        ResultScanner scanner = table.getScanner(scan)) {
      Result res = null;
      while ((res = scanner.next()) != null) {
        res.advance();
        String fam = null;
        String path = null;
        boolean raw = false;
        byte[] row;
        String region = null;
        for (Cell cell : res.listCells()) {
          row = CellUtil.cloneRow(cell);
          rows.add(row);
          String rowStr = Bytes.toString(row);
          region = BackupSystemTable.getRegionNameFromOrigBulkLoadRow(rowStr);
          if (CellUtil.compareQualifiers(cell, BackupSystemTable.FAM_COL, 0,
            BackupSystemTable.FAM_COL.length) == 0) {
            fam = Bytes.toString(CellUtil.cloneValue(cell));
          } else if (CellUtil.compareQualifiers(cell, BackupSystemTable.PATH_COL, 0,
            BackupSystemTable.PATH_COL.length) == 0) {
            path = Bytes.toString(CellUtil.cloneValue(cell));
          } else if (CellUtil.compareQualifiers(cell, BackupSystemTable.STATE_COL, 0,
            BackupSystemTable.STATE_COL.length) == 0) {
            byte[] state = CellUtil.cloneValue(cell);
            if (Bytes.equals(BackupSystemTable.BL_PREPARE, state)) {
              raw = true;
            } else {
              raw = false;
            }
          }
        }
        if (map.get(tTable) == null) {
          map.put(tTable, new HashMap<>());
          tblMap = map.get(tTable);
        }
        if (tblMap.get(region) == null) {
          tblMap.put(region, new HashMap<>());
        }
        Map<String, List<Pair<String, Boolean>>> famMap = tblMap.get(region);
        if (famMap.get(fam) == null) {
          famMap.put(fam, new ArrayList<>());
        }
        famMap.get(fam).add(new Pair<>(path, raw));
        LOG.debug("found orig " + path + " for " + fam + " of table " + region);
      }
    }
  }
  return new Pair<>(map, rows);
}
 
Example 14
Source Project: hbase   File: BackupSystemTable.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Converts Result to BackupInfo
 * @param res HBase result
 * @return backup info instance
 * @throws IOException exception
 */
private BackupInfo resultToBackupInfo(Result res) throws IOException {
  res.advance();
  Cell cell = res.current();
  return cellToBackupInfo(cell);
}