zoukankan      html  css  js  c++  java
  • MR mapper数量怎么确定?

    看过MR的处理流程的人应该都知道,在MR处理的时候有个split,这个split数量决定了mapper的数量,那split是怎么来的呢?我们在写MR代码的时候也没有接口可以定义split的数量,那split怎么来的? 有人说是block数量,真是是这样吗? 我们来看一下源码:

    public List<InputSplit> getSplits(JobContext job) throws IOException {
        StopWatch sw = new StopWatch().start();
        long minSize = Math.max(getFormatMinSplitSize(), getMinSplitSize(job));
        long maxSize = getMaxSplitSize(job);
    
        // generate splits
        List<InputSplit> splits = new ArrayList<InputSplit>();
        List<FileStatus> files = listStatus(job);
        for (FileStatus file: files) {
          Path path = file.getPath();
          long length = file.getLen();
          if (length != 0) {
            BlockLocation[] blkLocations;
            if (file instanceof LocatedFileStatus) {
              blkLocations = ((LocatedFileStatus) file).getBlockLocations();
            } else {
              FileSystem fs = path.getFileSystem(job.getConfiguration());
              blkLocations = fs.getFileBlockLocations(file, 0, length);
            }
            if (isSplitable(job, path)) {
              long blockSize = file.getBlockSize();
              long splitSize = computeSplitSize(blockSize, minSize, maxSize);//这里计算splitSize
    
              long bytesRemaining = length;
              while (((double) bytesRemaining)/splitSize > SPLIT_SLOP) {
                int blkIndex = getBlockIndex(blkLocations, length-bytesRemaining);
                splits.add(makeSplit(path, length-bytesRemaining, splitSize,
                            blkLocations[blkIndex].getHosts(),
                            blkLocations[blkIndex].getCachedHosts()));
                bytesRemaining -= splitSize;
              }
    
              if (bytesRemaining != 0) {
                int blkIndex = getBlockIndex(blkLocations, length-bytesRemaining);
                splits.add(makeSplit(path, length-bytesRemaining, bytesRemaining,
                           blkLocations[blkIndex].getHosts(),
                           blkLocations[blkIndex].getCachedHosts()));
              }
            } else { // not splitable
              splits.add(makeSplit(path, 0, length, blkLocations[0].getHosts(),
                          blkLocations[0].getCachedHosts()));
            }
          } else { 
            //Create empty hosts array for zero length files
            splits.add(makeSplit(path, 0, length, new String[0]));
          }
        }
        // Save the number of input files for metrics/loadgen
        job.getConfiguration().setLong(NUM_INPUT_FILES, files.size());
        sw.stop();
        if (LOG.isDebugEnabled()) {
          LOG.debug("Total # of splits generated by getSplits: " + splits.size()
              + ", TimeTaken: " + sw.now(TimeUnit.MILLISECONDS));
        }
        return splits;
      }
      protected long computeSplitSize(long blockSize, long minSize,
                                      long maxSize) {
        return Math.max(minSize, Math.min(maxSize, blockSize)); //获取splitSize
      }

    其中的getMinSplitSize和getMaxSplitSize方法分别用于获取最小InputSplit和最大InputSplit的值,对应的配置参数分别为mapreduce.input.fileinputformat.split.minsize,默认值为1L和mapreduce.input.fileinputformat.split.maxsize,默认值为Long.MAX_VALUE

    所以,hadoop中默认的block size为128M,所以split的size一般对应为block的大小,所以,Mapper的数量就是文件个数的数量;

    这样可以做到数据本地性,提示效率;

    1.如果根据特殊情况的需要非要自定义mapper的数量怎么办?

    那就只有修改块的大小、split的最小值和最大值来影响mapper的数量;

  • 相关阅读:
    Linux基础-3.用户、群组和权限
    Linux基础-2.目录文件的浏览、管理及维护
    Linux基础-1.Linux命令及获取帮助
    CentOS6.10安装详解
    有序字典
    根据公历计算农历
    常用模块
    人工智能_2_特征处理.py
    人工智能_1_初识_机器学习介绍_特征工程和文本特征提取
    python-matplotlib
  • 原文地址:https://www.cnblogs.com/dpains/p/8855313.html
Copyright © 2011-2022 走看看