自定义分区
需求说明
场景:实际生产中需求变化多端,默认分区规则往往不能满足需求,需要结合业务逻辑来灵活控制分区规则以及分区数量。
如何定制自己需要的分区规则?
具体步骤:
- 自定义类继承Partitioner,重写getPartition()方法
- 在Driver驱动中,指定使用自定义Partitioner
- 在Driver驱动中,要根据自定义Partitioner的逻辑设置相应数量的ReduceTask数量。
需求: 按照不同的appkey把记录输出到不同的分区中
原始日志格式
001 001577c3 kar 120.196.100.99 1116 954 200
日志id 设备id appkey(合作硬件厂商) 网络ip 自有内容时长(秒) 第三方内容时长(秒) 网络状态码
数据地址
链接:https://pan.baidu.com/s/170U3UIdfYAUdN8aFuGEAXg
提取码:h0ih
输出结果
根据appkey把不同厂商的日志数据分别输出到不同的文件中
需求分析
面对业务需求,结合mr的特点,来设计map输出的kv,以及reduce输出的kv数据
一个ReduceTask对应一个输出文件,因为在shuffle机制中每个reduceTask拉取的都是某一个分区的数据,一个分区对应一个输出文件。
结合appkey的前缀相同的特点,同时不能使用默认分区规则,而是使用自定义分区器,只要appkey前
缀相同则数据进入同个分区。
整体思路:
Mapper
- 读取一行文本,按照制表符切分
- 解析出appkey字段,其余数据封装为PartitionBean对象(实现序列化Writable接口)
- 设计map()输出的kv,key–>appkey(依靠该字段完成分区),PartitionBean对象作为Value输出
自定义分区代码
PartitionBean
关键点:
- 空参构造
- 序列化方法write
- 反序列化方法readFields 字段顺序与序列化方法一致
- 重写toString方法 (方便输出)
package com.my.mr.partition;
import lombok.Data;
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@Data
public class PartitionBean implements Writable {
//准备一个空参构造
public PartitionBean() {
}
public PartitionBean(String id, String deviceId, String appkey, String ip, Long selfDuration, Long thirdPartDuration, String status) {
this.id = id;
this.deviceId = deviceId;
this.appkey = appkey;
this.ip = ip;
this.selfDuration = selfDuration;
this.thirdPartDuration = thirdPartDuration;
this.status = status;
}
//序列化方法
@Override
public void write(DataOutput out) throws IOException {
out.writeUTF(id);
out.writeUTF(deviceId);
out.writeUTF(appkey);
out.writeUTF(ip);
out.writeLong(selfDuration);
out.writeLong(thirdPartDuration);
out.writeUTF(status);
}
//反序列化方法 要求序列化与反序列化字段顺序要保持一致
@Override
public void readFields(DataInput in) throws IOException {
this.id = in.readUTF();
this.deviceId = in.readUTF();
this.appkey = in.readUTF();
this.ip = in.readUTF();
this.selfDuration = in.readLong();
this.thirdPartDuration = in.readLong();
this.status = in.readUTF();
}
//定义属性
private String id;//日志id
private String deviceId;//设备id
private String appkey;//appkey厂商id
private String ip;//ip地址
private Long selfDuration;//自有内容播放时长
private Long thirdPartDuration;//第三方内容时长
private String status;//状态码
//方便文本中的数据易于观察,重写toString()方法
@Override
public String toString() {
return id + '\t' +
"\t" + deviceId + '\t' + appkey + '\t' +
ip + '\t' +
selfDuration +
"\t" + thirdPartDuration +
"\t" + status;
}
}
CustomPartitioner
重写getPartition方法,确定分区规则
package com.my.mr.partition;
import lombok.Data;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
//Partitioner分区器的泛型是map输出的kv类型
public class CustomPartitioner extends Partitioner<Text, PartitionBean> {
@Override
public int getPartition(Text text, PartitionBean partitionBean, int numPartitions) {
int partition = 0;
if (text.toString().equals("kar")) {
//只需要保证满足此if条件的数据获得同个分区编号集合
partition = 0;
} else if (text.toString().equals("pandora")) {
partition = 1;
} else {
partition = 2;
}
return partition;
}
}
PartitionMapper
package com.my.mr.partition;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/*
1. 读取一行文本,按照制表符切分
2. 解析出appkey字段,其余数据封装为PartitionBean对象(实现序列化Writable接口)
3. 设计map()输出的kv,key-->appkey(依靠该字段完成分区),PartitionBean对象作为Value输出
*/
public class PartitionMapper extends Mapper<LongWritable, Text, Text, PartitionBean> {
final PartitionBean bean = new PartitionBean();
final Text k = new Text();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
final String[] fields = value.toString().split("\t");
String appkey = fields[2];
bean.setId(fields[0]);
bean.setDeviceId(fields[1]);
bean.setAppkey(fields[2]);
bean.setIp(fields[3]);
bean.setSelfDuration(Long.parseLong(fields[4]));
bean.setThirdPartDuration(Long.parseLong(fields[5]));
bean.setStatus(fields[6]);
k.set(appkey);
context.write(k, bean); //shuffle开始时会根据k的hashcode值进行分区,但是结合我们自己的业务,默认hash分区方式不能满足需求
}
}
PartitionReducer
package com.my.mr.partition;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
//reduce输入类型:Text,PartitionBean,输出:Text,PartitionBean
public class PartitionReducer extends Reducer<Text, PartitionBean, Text, PartitionBean> {
@Override
protected void reduce(Text key, Iterable<PartitionBean> values, Context context) throws IOException, InterruptedException {
//无需聚合运算,只需要进行输出即可
for (PartitionBean bean : values) {
context.write(key, bean);
}
}
}
PartitionDriver
package com.my.mr.partition;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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 PartitionDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//1 获取配置文件
final Configuration conf = new Configuration();
//2 获取job实例
final Job job = Job.getInstance(conf);
//3 设置任务相关参数
job.setJarByClass(PartitionDriver.class);
job.setMapperClass(PartitionMapper.class);
job.setReducerClass(PartitionReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(PartitionBean.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(PartitionBean.class);
// 4 设置使用自定义分区器
job.setPartitionerClass(CustomPartitioner.class);
//5 指定reducetask的数量与分区数量保持一致,分区数量是3
job.setNumReduceTasks(3); //reducetask不设置默认是1个
// job.setNumReduceTasks(5);
// job.setNumReduceTasks(2);
// 6 指定输入和输出数据路径
FileInputFormat.setInputPaths(job, new Path("e:/speak.data"));
FileOutputFormat.setOutputPath(job, new Path("e:/parition/out"));
// 7 提交任务
final boolean flag = job.waitForCompletion(true);
System.exit(flag ? 0 : 1);
}
}
总结
- 自定义分区器时最好保证分区数量与reduceTask数量保持一致;
- 如果分区数量不止1个,但是reduceTask数量1个,此时只会输出一个文件。
- 如果reduceTask数量大于分区数量,但是输出多个空文件
- 如果reduceTask数量小于分区数量,有可能会报错。
分区发生在shuffle阶段,具体是map阶段环形缓冲区溢写磁盘,多个maptask的相同分区被同一个reduce拉去数据,作为reduce的数据源。通常分区数量与reducetask数量相同,
若reducetask的数量小于分区数, 则多余的分区将无reducetask拉取,可能会报错,
若reducetask的数量大学分区数,则多余的任务将无数据拉取,输出为空。