hadoop之mr

📅 2026/7/28 15:12:50 👁️ 阅读次数 📝 编程学习
hadoop之mr

文章目录

    • 1.mr之CombineTextInputFormat
    • 2.split size
    • 2.按行分片NLineInputFormat
    • 4.split大小与block的关系:
    • 5.reduce数量大于分区数

1.mr之CombineTextInputFormat

  • 处理一个文件夹下的四个小文件,未使用CombineTextInputFormat.class
//默认走的是TextInputFormat//job.setInputFormatClass(CombineTextInputFormat.class);//6.设置输入输出路径FileInputFormat.setInputPaths(job,newPath(input));FileOutputFormat.setOutputPath(job,newPath(output));

输出:

INFO FileInputFormat:Total input paths to process:4INFO JobSubmitter:number of splits:4
  • 使用CombineTextInputFormat.class
//默认走的是TextInputFormatjob.setInputFormatClass(CombineTextInputFormat.class);//6.设置输入输出路径FileInputFormat.setInputPaths(job,newPath(input));FileOutputFormat.setOutputPath(job,newPath(output));

输出:

INFO FileInputFormat:Total input paths to process:4INFO CombineFileInputFormat:DEBUG:Terminated node allocation with:CompletedNodes:1,size left:57INFO JobSubmitter:number of splits:1INFO JobSubmitter:Submitting tokensforjob:job_local656404029_0001

2.split size

  • 将block设置成4兆


有三个split

  • 第一阶段

1.4 <4 1.4
4.2 >4 <42 2.1 2.1
2.8 <4 2.8
5.7 >4 <4
2 2.85 2.85

  • CombineTextInputFormat只能合文本

2.按行分片NLineInputFormat

  • 业务场景:
    一般: 100列 100行 5G
    特殊: 2列 ? 5G

eg:10w行数据
未设置NLineInputFormat.1个split

job.setInputFormatClass(NLineInputFormat.class);NLineInputFormat.setNumLinesPerSplit(30000);

设置NLineInputFormat后,3个splits

4.split大小与block的关系:

(1)block块的小于split分片的最小值,那split的值就是split分片的大小

(2)block块的小大介于split分片配置的最小值和最大值之间,block的大小就是split的大小。

(3)block块的大小大于split分片的最大值,split的大小就是split配置的最大值。但会增加map执行的并发度,但是会造成在节点之间拉取数据

也有公式可以计算split也就是map任务数,这里就不做讨论了。

  • 一个map对应一个split分片吗?
    经过上面的讨论,答案是显而易见的:
    map个数:由任务切片spilt决定的,默认情况下一个split的大小就是block
    由参与任务的文件个数决定的

5.reduce数量大于分区数

  • 后面的part文件是空的—小文件越多

  • reduce数量不可以小于分区数

  • reduce数量可以等于1,全写入一个文件

  • reduce增加,并行度增加,速度会快.但是,jvm开销会大,小文件也增加.离线处理对速度要求不太高.