目录
四、MR编程—自定义InputFormat—处理小文件(重点+难点 )
一、MR编程—自定义分区—词频统计(重点 )
-
如果默认分区器不满足业务需求,可以自定义分区器
-
自定义分区器的类继承Partitioner类
-
覆写getPartition(),在方法中,定义自己的分区策略
-
在main()方法中调用job.setPartitionerClass()
-
main()中设置reduce任务数
-
1.1、分区原理
-
在map任务中,从环形缓冲区溢出写磁盘时,会先对kv对数据进行分区操作
-
分区操作是由MR中的分区器负责的
-
MapReduce有自带的默认分区器 HashPartitioner
-
关键方法getPartition返回当前键值对的分区索引(partition index
-
-
public class HashPartitioner<K2, V2> implements Partitioner<K2, V2> { public void configure(JobConf job) {} /** Use {@link Object#hashCode()} to partition. */ public int getPartition(K2 key, V2 value, int numReduceTasks) { return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks; } }
代码说明:
-
(1)环形缓冲区溢出写磁盘前,将每个kv对,作为getPartition()的参数传入;
-
(2)先对键值对中的key求hash值(int类型),与MAX_VALUE按位与;再模上reduce task个数。
-
假设reduce task个数设置为4(可在程序中使用job.setNumReduceTasks(4)指定reduce task个数为4)
-
那么map任务溢出文件有4个分区,分区index分别是0、1、2、3
-
getPartition()结果有四种:0、1、2、3
-
根据计算结果,决定当前kv对,落入哪个分区,如结果是0,则当前kv对落入溢出文件的0分区中
-
最终被相应的reduce task通过http获得
-
若是MR默认分区器,不满足需求;可根据业务逻辑,设计自定义分区器,比如实现图上的功能
1.2 默认分区
-
MR读取三个文件part1.txt、part2.txt、part3.txt;三个文件放到HDFS目录:/customParttitioner中
- part1.txt内容如下: ``` |
- part2.txt内容如下: ``` |
- part3.txt内容如下: ``` |
- 默认HashPartitioner分区时,查看结果(看代码)
-
运行参数: /customParttitioner /cp01
-
打jar包运行,结果如下:
只有part-r-00001、part-r-00003有数据;另外两个没有数据
HashPartitioner将Bear分到index=1的分区;将Car|Dear|River分到index=3分区
1.3、MR—自定义分区
-
使用combine时,首先考虑当前MR是否适合combine
-
总原则是不论使不使用combine不能影响最终的结果
-
在MR时,发生数据倾斜,且可以使用combine时,可以使用combine缓解数据倾斜
1.3.1 需求
-
自定义分区,使得文件中,分别以Dear、Bear、River、Car为键的键值对,分别落到index是0、1、2、3的分区中
1.3.2 分析
-
若要实现以上的分区策略,需要自定义分区类
-
此类实现Partitioner接口
-
在getPartition()中实现分区逻辑
-
-
main方法中
-
设定reduce个数为4
-
设置自定义的分区类,调用job.setPartitionerClass方法
-
1.3.3 MR代码
1、自定义分区器
2、main ,(map/reduce与以前一样)
3、完整代码
package com.kaikeba.hadoop.partitioner;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
import java.util.HashMap;
public class CustomPartitioner extends Partitioner<Text, IntWritable> {
public static HashMap<String, Integer> dict = new HashMap<String, Integer>();
//定义每个键对应的分区index,使用map数据结构完成
static{
dict.put("Dear", 0);
dict.put("Bear", 1);
dict.put("River", 2);
dict.put("Car", 3);
}
public int getPartition(Text text, IntWritable intWritable, int i) {
//
int partitionIndex = dict.get(text.toString());
return partitionIndex;
}
}
package com.kaikeba.hadoop.mapreduce.partitioner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
/**
*
* 在单词计数的例子的基础上,增加自定义分区的功能
*/
public class WordCountMain {
public static void main(String[] args) throws IOException,
ClassNotFoundException, InterruptedException {
if (args.length != 2 || args == null) {
System.out.println("please input Path!");
System.exit(0); }
// 1、配置
Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration, WordCountMain.class.getSimpleName());
job.setJarByClass(WordCountMain.class);
// 2、设置输入/输出路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
// 3、设置处理Map/Reduce阶段的类
job.setMapperClass(WordCountMap.class);
//job.setCombinerClass(WordCountReduce.class);//自定义combine类,还是用的是reduce
job.setReducerClass(WordCountReduce.class);
// 4、设置输出kv
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 5、======使用自定义的分区类进行分区
job.setPartitionerClass(CustomPartitioner.class);
// 6、==