Exemplo n.º 1
0
  /**
   * Returns statistics on this table as a tabular result set. Used for the SHOW TABLE STATS
   * statement. The schema of the returned TResultSet is set inside this method.
   */
  public TResultSet getTableStats() {
    TResultSet result = new TResultSet();
    TResultSetMetadata resultSchema = new TResultSetMetadata();
    result.setSchema(resultSchema);
    resultSchema.addToColumns(new TColumn("Region Location", ColumnType.STRING.toThrift()));
    resultSchema.addToColumns(new TColumn("Start RowKey", ColumnType.STRING.toThrift()));
    resultSchema.addToColumns(new TColumn("Est. #Rows", ColumnType.BIGINT.toThrift()));
    resultSchema.addToColumns(new TColumn("Size", ColumnType.STRING.toThrift()));

    // TODO: Consider fancier stats maintenance techniques for speeding up this process.
    // Currently, we list all regions and perform a mini-scan of each of them to
    // estimate the number of rows, the data size, etc., which is rather expensive.
    try {
      long totalNumRows = 0;
      long totalHdfsSize = 0;
      List<HRegionLocation> regions =
          HBaseTable.getRegionsInRange(
              hTable_, HConstants.EMPTY_END_ROW, HConstants.EMPTY_START_ROW);
      for (HRegionLocation region : regions) {
        TResultRowBuilder rowBuilder = new TResultRowBuilder();
        HRegionInfo regionInfo = region.getRegionInfo();
        Pair<Long, Long> estRowStats =
            getEstimatedRowStats(regionInfo.getStartKey(), regionInfo.getEndKey());

        long numRows = estRowStats.first.longValue();
        long hdfsSize = getHdfsSize(regionInfo);
        totalNumRows += numRows;
        totalHdfsSize += hdfsSize;

        // Add the region location, start rowkey, number of rows and raw Hdfs size.
        rowBuilder
            .add(String.valueOf(region.getHostname()))
            .add(Bytes.toString(regionInfo.getStartKey()))
            .add(numRows)
            .addBytes(hdfsSize);
        result.addToRows(rowBuilder.get());
      }

      // Total num rows and raw Hdfs size.
      if (regions.size() > 1) {
        TResultRowBuilder rowBuilder = new TResultRowBuilder();
        rowBuilder.add("Total").add("").add(totalNumRows).addBytes(totalHdfsSize);
        result.addToRows(rowBuilder.get());
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return result;
  }
Exemplo n.º 2
0
  private static Map<String, List<Row>> createRsRowMap(HTable htable, List<Row> rows)
      throws IOException {

    Map<String, List<Row>> rowMap = new HashMap<String, List<Row>>();
    for (Row row : rows) {
      HRegionLocation rl = htable.getRegionLocation(row.getRow());
      String hostname = rl.getHostname();
      List<Row> recs = rowMap.get(hostname);
      if (recs == null) {
        recs = new ArrayList<Row>(INITIAL_LIST_SIZE);
        rowMap.put(hostname, recs);
      }
      recs.add(row);
    }
    return rowMap;
  }
Exemplo n.º 3
0
  private static Map<String, List<Put>> createRsPutMap(HTable htable, List<Put> puts)
      throws IOException {

    Map<String, List<Put>> putMap = new HashMap<String, List<Put>>();
    for (Put put : puts) {
      HRegionLocation rl = htable.getRegionLocation(put.getRow());
      String hostname = rl.getHostname();
      List<Put> recs = putMap.get(hostname);
      if (recs == null) {
        recs = new ArrayList<Put>(INITIAL_LIST_SIZE);
        putMap.put(hostname, recs);
      }
      recs.add(put);
    }
    return putMap;
  }