MapReduce的GroupComparator

问题

有如下的订单数据,想要查询出每一个订单中的最贵的商品

Order_0000001   Pdt_01  222.8
Order_0000001   Pdt_01  222.8
Order_0000002   Pdt_03  522.8
Order_0000003   Pdt_01  222.8
Order_0000004   Pdt_01  222.8
Order_0000004   Pdt_05  25.8
Order_0000005   Pdt_03  522.8
Order_0000006   Pdt_04  122.4
Order_0000007   Pdt_05  722.4
Order_0000007   Pdt_01  222.8
Order_0000001   Pdt_05  25.8
Order_0000002   Pdt_04  122.4
Order_0000002   Pdt_05  722.4

解决方法

第一种解决方法

在Map端读取数据,构造出相应的OrderBean对象,以Order_id为key,OrderBean为Value将数据输出
在Reduce端读取出相同的order_id的所有的OrderBean进行排序
缺点
需要自己进行排序,没有利用好Shuffle过程中的排序,效率较低

第二种解决方案

在Shuffle的过程中是会进行排序的,我们需要充分利用它
首先,排序的时候,只是会对key进行排序,所以我们需要将OrderBean作为我们的Key输出到Reduce
这里写图片描述

问题:

第一,不同的Order的对象可能会被分配到不同的reduce端,所以我们需要自定义分区方法,对order对象进行分区
第二,不同的Order对象是无法像<a,1><a,1><a,1>一样将<order1,null><order2,null>看成是一组的,
     即使order1的order_id和order2的order_id一样的
第三,Order对象需要进行排序,按照money

解决问题:

    针对第一个问题:
        自己定义Partitioner类,根据order_id进行hashcode%numTasks
    针对第二个问题:
        自己定义一个GroupComparator类,根据order_id划分组,将order_id相同的划分到同一个组中
        注意,这边进行判断的时候,当compare方法返回一个非0的时候,就会认为两个对象不是在同一个组中的
        注意,它是一次判断两个连续的对象,即如果有一串对象<order1,null><order2,null>,<order3,null>
        <order4,null>即使order1和order3的order_id是一样的,那么由于order2的order_id与它们不一样,当
        order1与order2的时候返回不是0,就会调用新的reduce,同理order2和order3,这就要求我们必须现根据
        order_id进行排序,然后再根据money排序        
    针对第三个问题:
        将自己定义的类继承WritableComparator,重写compare方法(必须现根据order_id进行排序,然后再根据
        money排序)

java代码

public class OrderProduct implements WritableComparable<OrderProduct>{
    private String order_id;
    private String pdt_id;
    private double money;

    public String getOrder_id() {
        return order_id;
    }

    public void setOrder_id(String order_id) {
        this.order_id = order_id;
    }
    public String getPdt_id() {
        return pdt_id;
    }
    public void setPdt_id(String pdt_id) {
        this.pdt_id = pdt_id;
    }
    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }

    public int compareTo(OrderProduct o) {
        //不能直接这样写,因为这样的话,那么价格相同的也会被当成同一组了
        //相当于是先根据order_id进行排序,再根据money进行排序
        //必须先根据order_id排序,使得相同的order_id的对象在发送到reduce端的时候是连在一起的
        //因为之后的groupComparator的时候,是一个一个的跟后面的比较的,返回0,就认为是在同一个组中的
        //返回不为0就不是同一个组
        if(this.getOrder_id().compareTo(o.getOrder_id())==0){
            return Double.valueOf(money).compareTo(o.getMoney());
        }
        else return this.getOrder_id().compareTo(o.getOrder_id());

        /*return Double.valueOf(money).compareTo(o.getMoney());*/
    }

    public void write(DataOutput out) throws IOException {
        out.writeUTF(order_id);
        out.writeUTF(pdt_id);
        out.writeDouble(money);
    }

    public void readFields(DataInput in) throws IOException {
        order_id = in.readUTF();
        pdt_id = in.readUTF();
        money = in.readDouble();
    }

    @Override
    public String toString() {
        return "OrderProduct{" +
                "order_id='" + order_id + '\'' +
                ", pdt_id='" + pdt_id + '\'' +
                ", money=" + money +
                '}';
    }
}
public class FindCostMaxProduct {
    public static void main(String[] args) throws Exception {

        Configuration configuration = new Configuration();

        Job job = Job.getInstance(configuration);
        job.setJarByClass(FindCostMaxProduct.class);

        job.setMapperClass(FindCostMaxProductMapper.class);
        job.setMapOutputKeyClass(OrderProduct.class);
        job.setMapOutputValueClass(NullWritable.class);

        //job.setNumReduceTasks(7);
        job.setGroupingComparatorClass(OrderGroupComparator.class);


//
       job.setPartitionerClass(OrderPartitioner.class);

        job.setReducerClass(FindCostMaxProductReducer.class);
        job.setOutputKeyClass(OrderProduct.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.setInputPaths(job,new Path("F:\\hdp\\order\\input"));
        FileOutputFormat.setOutputPath(job,new Path("F:\\hdp\\order\\output"));

        job.waitForCompletion(true);
    }
}


class FindCostMaxProductMapper extends Mapper<LongWritable,Text,OrderProduct,NullWritable>{


    List<OrderProduct> list = new ArrayList<OrderProduct>();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] split = value.toString().split("\t");
        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setOrder_id(split[0]);
        orderProduct.setPdt_id(split[1]);
        orderProduct.setMoney(Double.valueOf(split[2]));
        context.write(orderProduct,NullWritable.get());
        list.add(orderProduct);
    }

    @Override
    protected void cleanup(Context context) throws IOException, InterruptedException {
        Collections.sort(list);
        System.out.println("1");
    }
}

class FindCostMaxProductReducer extends Reducer<OrderProduct,NullWritable,OrderProduct,NullWritable>{
    @Override
    protected void reduce(OrderProduct key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        context.write(key,NullWritable.get());
    }
}

class OrderPartitioner extends Partitioner<OrderProduct,NullWritable>{

    public int getPartition(OrderProduct orderProduct, NullWritable nullWritable, int numPartitions) {
        return orderProduct.getOrder_id().hashCode() % numPartitions;
    }
}


/*
默认情况下,即使将order_id相同的订单分配到了同一个reduce中,但是作为key的他们却不会是在同一个组中
不想<a.1><a,1><a,1>这样三个是在同一个组中的
 */
class OrderGroupComparator extends WritableComparator{

    public OrderGroupComparator(){
        super(OrderProduct.class,true);
    }

    //注意重写的需要是参数为WritableComparable类型的方法,因为其还有一个重载的参数类型为Object的方法
    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        OrderProduct o1 = (OrderProduct) a;
        OrderProduct o2 = (OrderProduct) b;
        return o1.getOrder_id().compareTo(o2.getOrder_id());
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值