控制Hive MAP個數詳解

來源:互聯網
上載者:User

Hive的MAP數或者說MAPREDUCE的MAP數是由誰來決定的呢?inputsplit size,那麼對於每一個inputsplit size是如何計算出來的,這是做MAP數調整的關鍵.
Hadoop給出了Inputformat介面用於描述輸入資料的格式,其中一個關鍵的方法就是getSplits,對輸入的資料進行分區.
Hive對InputFormat進行了封裝:

而具體採用的實現是由參數hive.input.format來決定的,主要使用2中類型HiveInputFormat和CombineHiveInputFormat.
對於HiveInputFormat來說:


 public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException {
    //掃描每一個分區
    for (Path dir : dirs) {
      PartitionDesc part = getPartitionDescFromPath(pathToPartitionInfo, dir);
    //擷取分區的輸入格式
      Class inputFormatClass = part.getInputFileFormatClass();
      InputFormat inputFormat = getInputFormatFromCache(inputFormatClass, job);
    //按照相應格式的分區演算法擷取分區
    //注意:這裡的Inputformat只是old version API:org.apache.hadoop.mapred而不是org.apache.hadoop.mapreduce,因此不能採用新的API,否則在查詢時會報異常:Input format must implement InputFormat.區別就是新的API的計算inputsplit size(Math.max(minSize, Math.min(maxSize, blockSize))和老的(Math.max(minSize, Math.min(goalSize, blockSize)))不一樣;
      InputSplit[] iss = inputFormat.getSplits(newjob, numSplits / dirs.length);
      for (InputSplit is : iss) {
    //封裝結果,返回
        result.add(new HiveInputSplit(is, inputFormatClass.getName()));
      }
    }
    return result.toArray(new HiveInputSplit[result.size()]);
}

 

對於CombineHiveInputFormat來說的計算就比較複雜了:


 public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException {
    //載入CombineFileInputFormatShim,這個類繼承了org.apache.hadoop.mapred.lib.CombineFileInputFormat
    CombineFileInputFormatShim combine = ShimLoader.getHadoopShims()
        .getCombineFileInputFormat();
if (combine == null) {
//若為空白則採用HiveInputFormat的方式,下同
      return super.getSplits(job, numSplits);
    }
    Path[] paths = combine.getInputPathsShim(job);
for (Path path : paths) {
//若是外部表格,則按照HiveInputFormat方式分區
      if ((tableDesc != null) && tableDesc.isNonNative()) {
        return super.getSplits(job, numSplits);
      }
      Class inputFormatClass = part.getInputFileFormatClass();
      String inputFormatClassName = inputFormatClass.getName();
      InputFormat inputFormat = getInputFormatFromCache(inputFormatClass, job);
      if (this.mrwork != null && !this.mrwork.getHadoopSupportsSplittable()) {
        if (inputFormat instanceof TextInputFormat) {
        if ((new CompressionCodecFactory(job)).getCodec(path) != null)
//在未開啟hive.hadoop.supports.splittable.combineinputformat(MAPREDUCE-1597)參數情況下,對於TextInputFormat並且為壓縮則採用HiveInputFormat分區演算法
                    return super.getSplits(job, numSplits);
        }
      }
    //對於串連式同上
      if (inputFormat instanceof SymlinkTextInputFormat) {
        return super.getSplits(job, numSplits);
      }
      CombineFilter f = null;
      boolean done = false;
Path filterPath = path;
//由參數hive.mapper.cannot.span.multiple.partitions控制,預設false;如果沒true,則對每一個partition建立一個pool,以下省略為true的處理;對於同一個表的同一個檔案格式的split建立一個pool為combine做準備;
      if (!mrwork.isMapperCannotSpanPartns()) {
        opList = HiveFileFormatUtils.doGetWorksFromPath(
                  pathToAliases, aliasToWork, filterPath);
        f = poolMap.get(new CombinePathInputFormat(opList, inputFormatClassName));
      }
      if (!done) {
        if (f == null) {
          f = new CombineFilter(filterPath);
          combine.createPool(job, f);
        } else {
          f.addPath(filterPath);
        }
      }
    }
if (!mrwork.isMapperCannotSpanPartns()) {
//到這裡才調用combine的分區演算法,繼承了org.apache.hadoop.mapred.lib.CombineFileInputFormat extends 新版本CombineFileInputformat
      iss = Arrays.asList(combine.getSplits(job, 1));
}
//對於sample查詢特殊處理
    if (mrwork.getNameToSplitSample() != null && !mrwork.getNameToSplitSample().isEmpty()) {
      iss = sampleSplits(iss);
}
//封裝結果返回
    for (InputSplitShim is : iss) {
      CombineHiveInputSplit csplit = new CombineHiveInputSplit(job, is);
      result.add(csplit);
    }
    return result.toArray(new CombineHiveInputSplit[result.size()]);
  }

更多詳情見請繼續閱讀下一頁的精彩內容:

Hive 的詳細介紹:請點這裡
Hive 的:請點這裡

基於Hadoop叢集的Hive安裝

Hive內表和外表的區別

Hadoop + Hive + Map +reduce 叢集安裝部署

Hive本地獨立模式安裝

Hive學習之WordCount單詞統計

  • 1
  • 2
  • 下一頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.