hadoop源码阅读(三)(InputFormat切片源码)

10 篇文章 0 订阅
6 篇文章 0 订阅
本文详细探讨了Hadoop的FileInputFormat类中关于切片(split)的源码实现,从getSplits方法入手,解释了max方法如何确定最小和最大切片大小。通过实例展示了一个350M和100M文件的切片过程,生成了四部分切片。后续将补充详细的XMind思维导图。
摘要由CSDN通过智能技术生成

切片源码

在这里插入图片描述

从FileInputFormat类进入,因为在FileInputFormat类中重写了父类InputFormat的getSplits方法,在long minSize = Math.max(getFormatMinSplitSize(), getMinSplitSize(job));处打断点,进入

/**
     * Returns the greater of two {@code int} values. That is, the
     * result is the argument closer to the value of
     * {@link Integer#MAX_VALUE}. If the arguments have the same value,
     * the result is that same value.
     *
     * @param   a   an argument.
     * @param   b   another argument.
     * @return  the larger of {@code a} and {@code b}.
     */
    @IntrinsicCandidate
    public static int max(int a, int b) {
        return (a >= b) ? a : b;
    }

    /**
     * Returns the greater of two {@code long} values. That is, the
     * result is the argument closer to the value of
     * {@link Long#MAX_VALUE}. If the arguments have the same value,
     * the result is that same value.
     *
     * @param   a   an argument.
     * @param   b   another argument.
     * @return  the larger of {@code a} and {@code b}.
     */
    public static long max(long a, long b) {
        return (a >= b) ? a : b;
    }

max方法返回getFormatMinSplitSize()或getMinSplitSize(job)参数中的最大值

//getFormatMinSplitSize()方法固定返回值为1
protected long getFormatMinSplitSize() {
    return 1;
  }

//getMinSplitSize(JobContext job)方法
//如果配置了mapreduce.input.fileinputformat.split.maxsize则返回配置值,否则返回默认值1L
public static long getMinSplitSize(JobContext job) {
    return job.getConfiguration().getLong(SPLIT_MINSIZE, 1L);
  }

public static final String SPLIT_MAXSIZE = 
    "mapreduce.input.fileinputformat.split.maxsize";

在这里插入图片描述

同理,在getMaxSplitSize(job)方法中

//如果未配置mapreduce.input.fileinputformat.split.maxsize则返回Long.MAX_VALUE的值,如果配置了就使用配置值
public static long getMaxSplitSize(JobContext context) {
    return context.getConfiguration().getLong(SPLIT_MAXSIZE, 
                                              Long.MAX_VALUE);
  }

//SPLIT_MAXSIZE
public static final String SPLIT_MAXSIZE = 
    "mapreduce.input.fileinputformat.split.maxsize";

//Long.MAX_VALUE值
@Native public static final long MAX_VALUE = 0x7fffffffffffffffL;

在这里插入图片描述

在这里插入图片描述

具体计算切片大小细节:

//如果未设置minSize默认为1,maxSize默认Long的最大值
//Math.min(maxSize,blockSize)返回的是blockSize,整个计算切片方法返回的是blockSize
//总结: 默认情况下 片大小 = 块大小
//要使片大小 > 块大小,则需要设置minSize>128m
//要使片大小 < 块大小,则需要设置maxSize<128m
protected long computeSplitSize(long blockSize, long minSize,
                                  long maxSize) {
    return Math.max(minSize, Math.min(maxSize, blockSize));
  }

在这里插入图片描述

案例:
输入文件
File1 350m
File2 100m

输出文件
File1.split1 0–128m
File2.split2 128m-256m
File3.split3 256m–350m
File4.split4 100m

//若上面那个除后得到的值没有大于1.1走这个方法,将最后一个文件一整个输出(不切)>>可以忽略跨节点问题
if (bytesRemaining != 0) {
            int blkIndex = getBlockIndex(blkLocations, length-bytesRemaining);
            splits.add(makeSplit(path, length-bytesRemaining, bytesRemaining,
                       blkLocations[blkIndex].getHosts(),
                       blkLocations[blkIndex].getCachedHosts()));
          }

走完最终将生成的切片放入到splits集合中.

XMind图后面补

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水花一直飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值