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());
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章