Пример #1
0
  private HTableDescriptor ensureTableExists(String tableName) throws StorageException {
    HBaseAdmin adm = getAdminInterface();

    HTableDescriptor desc;

    try { // Create our table, if necessary
      if (adm.tableExists(tableName)) {
        desc = adm.getTableDescriptor(tableName.getBytes());
      } else {
        desc = new HTableDescriptor(tableName);
        adm.createTable(desc);
      }
    } catch (IOException e) {
      throw new TemporaryStorageException(e);
    }

    return desc;
  }
Пример #2
0
  /**
   * Deletes the specified table with all its columns. ATTENTION: Invoking this method will delete
   * the table if it exists and therefore causes data loss.
   */
  @Override
  public void clearStorage() throws StorageException {
    HBaseAdmin adm = getAdminInterface();

    try { // first of all, check if table exists, if not - we are done
      if (!adm.tableExists(tableName)) {
        logger.debug("clearStorage() called before table {} was created, skipping.", tableName);
        return;
      }
    } catch (IOException e) {
      throw new TemporaryStorageException(e);
    }

    HTable table = null;

    try {
      table = new HTable(hconf, tableName);

      Scan scan = new Scan();
      scan.setBatch(100);
      scan.setCacheBlocks(false);
      scan.setCaching(2000);

      ResultScanner scanner = null;

      try {
        scanner = table.getScanner(scan);

        for (Result res : scanner) {
          table.delete(new Delete(res.getRow()));
        }
      } finally {
        IOUtils.closeQuietly(scanner);
      }
    } catch (IOException e) {
      throw new TemporaryStorageException(e);
    } finally {
      IOUtils.closeQuietly(table);
    }
  }