对Reduce阶段的数据根据某一个或几个字段进行分组。
分组排序步骤:
(1)自定义类继承WritableComparator
(2)重写compare()方法
package com.grouping;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
/**
* 按照订单编号对数据进行分组
*/
public class OrderComparator extends WritableComparator {
//建立两个OrderBean的空对象
protected OrderComparator() {
super(OrderBean.class,true);
}
/**
* 分组比较方法,按照相同订单进入一组进行比较
* @param a
* @param b
* @return
*/
@Override
public int compare(WritableComparable a, WritableComparable b) {
OrderBean a1 = (OrderBean) a;
OrderBean b1 = (OrderBean) b;
return a1.getOrderId().compareTo(b1.getOrderId());
}
}
GroupingComparator分组案例实操
订单id |
商品id |
成交金额 |
0000001 |
Pdt_01 |
222.8 |
Pdt_02 |
33.8 |
|
0000002 |
Pdt_03 |
522.8 |