GroupingComparator分组(辅助排序)
对Reduce阶段的数据根据某一个或几个字段进行分组。
分组排序步骤:
(1)自定义类继承WritableComparator
(2)重写compare()方法
@Override
public int compare(WritableComparable a, WritableComparable b) {
// 比较的业务逻辑
return result;
}
(3)创建一个构造将比较对象的类传给父类
rotected OrderGroupingComparator() {
super(OrderBean.class, true);
}
案例实操
现在需要求出每一个订单中最贵的商品。
代码
OrderBean编写
package com.hadwinling.mapreduce.order;
import org.apache.hadoop.io.WritableComparable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
* @author :HadwinLing
* @version 1.0
* @description: TODO
* @date 2020/11/12 下午3:02
*/
public class OrderBean implements WritableComparable<OrderBean> {
private int order_id;
private double price;
public OrderBean() {
super();
}
public OrderBean(int order_id, double price) {
this.order_id = order_id;
this.price = price;
}
public int getOrder_id() {
return order_id;
}
public void setOrder_id(int order_id) {
this.order_id = order_id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
// 先按照定id升序排序,如果相同 按照价格降序排序
public int compareTo(OrderBean o) {
int result ;
if (order_id > o.getOrder_id()){
result = 1;
}else if (order_id < o.getOrder_id()){
result = -1;
}else {
if (price > o.getPrice()){
result = -1;
}else if(price < o.getPrice()){
result = 1;
}else{
result = 0;
}
}
return result;
}
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeInt(order_id);
dataOutput.writeDouble(price);
}
public void readFields(DataInput dataInput) throws IOException {
order_id = dataInput.readInt();
price = dataInput.readDouble();
}
@Override
public String toString() {
return "OrderBean{" +
"order_id=" + order_id +
", price=" + price +
'}';
}
}
OrderMapper编写
package com.hadwinling.mapreduce.order;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/**
* @author :HadwinLing
* @version 1.0
* @description: TODO
* @date 2020/11/12 下午3:09
*/
public class OrderMapper extends Mapper<LongWritable, Text, OrderBean,NullWritable> {
OrderBean k = new OrderBean();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] fields = line.split(" ");
k.setOrder_id(Integer.parseInt(fields[0]));
k.setPrice(Integer.parseInt(fields[2]));
context.write(k,NullWritable.get());
}
}
OrderReducer编写
package com.hadwinling.mapreduce.order;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
/**
* @author :HadwinLing
* @version 1.0
* @description: TODO
* @date 2020/11/12 下午3:18
*/
public class OrderReducer extends Reducer<OrderBean, NullWritable, OrderBean, NullWritable> {
@Override
protected void reduce(OrderBean key, Iterable<NullWritable> values, Context context)
throws IOException, InterruptedException {
for (NullWritable nullWritable : values) {
context.write(key, NullWritable.get());
}
}
}
OrderDriver编写
package com.hadwinling.mapreduce.order;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
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;
/**
* @author :HadwinLing
* @version 1.0
* @description: TODO
* @date 2020/11/12 下午3:22
*/
public class OrderDriver {
public static void main(String[] args) throws ClassNotFoundException, IOException, InterruptedException {
// 输入输出路径需要根据自己电脑上实际的输入输出路径设置
args = new String[] { };
// 1 获取配置信息
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
// 2 设置jar包加载路径
job.setJarByClass(OrderDriver.class);
// 3 加载map/reduce类
job.setMapperClass(OrderMapper.class);
job.setReducerClass(OrderReducer.class);
// 4 设置map输出数据key和value类型
job.setMapOutputKeyClass(OrderBean.class);
job.setMapOutputValueClass(NullWritable.class);
// 5 设置最终输出数据的key和value类型
job.setOutputKeyClass(OrderBean.class);
job.setOutputValueClass(NullWritable.class);
// 6 设置输入数据和输出数据路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
// 8 设置reduce端的分组
job.setGroupingComparatorClass(OrderGroupingComparator.class);
// 7 提交
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}
OrderGroupingComparator 编写
package com.hadwinling.mapreduce.order;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
/**
* @author :HadwinLing
* @version 1.0
* @description:辅助排序
* @date 2020/11/12 下午3:23
*/
public class OrderGroupingComparator extends WritableComparator {
protected OrderGroupingComparator(){
super(OrderBean.class, true);
}
@Override
public int compare(WritableComparable a, WritableComparable b) {
// 要求只要id相同,就认为是相同的key
OrderBean aBean = (OrderBean) a;
OrderBean bBean = (OrderBean) b;
int result;
if (aBean.getOrder_id() > bBean.getOrder_id()) {
result = 1;
}else if(aBean.getOrder_id() < bBean.getOrder_id()){
result = -1;
}else {
result = 0;
}
return result;
}
}