对Reduce阶段的数据根据某一个或几个字段进行分组。
分组排序步骤:
(1)自定义类继承WritableComparator
(2)重写compare()方法
@Override
public int compare(WritableComparable a, WritableComparable b) {
// 比较的业务逻辑
return result;
}
(3)创建一个构造将比较对象的类传给父类
protected OrderGroupingComparator() {
super(OrderBean.class, true);
}
GroupingComparator分组案例实操
1.需求
有如下订单数据
订单id 商品id 成交金额
0000001 Pdt_01 222.8
0000002 Pdt_05 722.4
0000001 Pdt_02 33.8
0000003 Pdt_06 232.8
0000003 Pdt_02 33.8
0000002 Pdt_03 522.8
0000002 Pdt_04 122.4
现在需要求出每一个订单中最贵的商品。
(1)输入数据
0000001 Pdt_01 222.8
0000002 Pdt_05 722.4
0000001 Pdt_02 33.8
0000003 Pdt_06 232.8
0000003 Pdt_02 33.8
0000002 Pdt_03 522.8
0000002 Pdt_04 122.4
(2)期望输出数据
1 222.8
2 722.4
3 232.8
2.需求分析
(1)利用“订单id和成交金额”作为key,可以将Map阶段读取到的所有订单数据按照id升序排序,如果id相同再按照金额降序排序,发送到Reduce。
(2)在Reduce端利用groupingComparator将订单id相同的kv聚合成组,然后取第一个即是该订单中最贵商品
3.代码实现
(1)定义订单信息OrderBean类
public class OrderBean implements WritableComparable<OrderBean> {
private int order_id;
private double price;
public OrderBean() { }
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;
}
@Override
public String toString() {
return order_id +
"\t" + price;
}
@Override
public int compareTo(OrderBean bean) {
//TODO 利用id升序,利用price降序
int result;
if(order_id > bean.getOrder_id()){
result = 1;
}else if(order_id < bean.getOrder_id()){
result = -1;
}else{
if(price > bean.getPrice()){
result = -1;
}else if(price < bean.getPrice()){
result = 1;
}else {
result = 0;
}
}
return result;
}
//TODO 序列化
@Override
public void write(DataOutput out) throws IOException {
out.writeInt( order_id );
out.writeDouble( price );
}
@Override
public void readFields(DataInput in) throws IOException {
order_id = in.readInt();
price = in.readDouble();
}
}
(2)编写OrderSortMapper类
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 {
//0000001 Pdt_01 222.8
String[] split = value.toString().split( "\t" );
k.setOrder_id( Integer.parseInt( split[0] ) );
k.setPrice( Double.parseDouble( split[2] ) );
context.write( k,NullWritable.get() );
}
}
(3)编写OrderSortGroupingComparator类
public class OrderGroupingComparator extends WritableComparator {
//TODO 减少重复 ,只求每个id的最大值
protected OrderGroupingComparator(){
super(OrderBean.class,true);
}
@Override
public int compare(WritableComparable a, WritableComparable b) {
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;
}
}
(4)编写OrderSortReducer类
public class OrderReduce extends Reducer<OrderBean, NullWritable, OrderBean, NullWritable> {
@Override
protected void reduce(OrderBean key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
context.write( key,NullWritable.get() );
}
}
(5)编写OrderSortDriver类
public class OrderDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//TODO 获取conf对象
Configuration conf = new Configuration();
//TODO 获取job对象
Job job = Job.getInstance( conf );
//TODO 获取jar包位置
job.setJarByClass( OrderDriver.class );
//TODO 获取map类和reduce类
job.setMapperClass( OrderMapper.class );
job.setReducerClass( OrderReduce.class );
//TODO 获取map输出类型
job.setMapOutputKeyClass( OrderBean.class );
job.setMapOutputValueClass( NullWritable.class );
//TODO 获取总输出类型
job.setOutputKeyClass( OrderBean.class );
job.setOutputValueClass( NullWritable.class );
//TODO 多加了一个reduce端的分组
job.setGroupingComparatorClass( OrderGroupingComparator.class );
//TODO 输入输出类型
FileInputFormat.setInputPaths( job,new Path( args[0] ) );
FileOutputFormat.setOutputPath( job,new Path( args[1] ) );
//TODO 提交
boolean result = job.waitForCompletion( true );
System.exit(result?0:1);
}
}