1.需求
有如下订单数据,现在需要求出每一个订单中最贵的商品。
(1)输入数据 GroupingComparator.txt
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聚合成组,然后取第一个即是该订单中最贵商品,如图4-18所示。
3.代码实现
(1)定义订单信息OrderBean类
package com.jinghang.mapreduce.order;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.WritableComparable;
public class OrderBean implements WritableComparable<OrderBean> {
private int order_id;
private double price;
public OrderBean() {
super();
}
public OrderBean(int order_id, double price) {
super();
this.order_id &#61; order_id;
this.price &#61; price;
}
&#64;Override
public void write(DataOutput out) throws IOException {
out.writeInt(order_id);
out.writeDouble(price);
}
&#64;Override
public void readFields(DataInput in) throws IOException {
order_id &#61; in.readInt();
price &#61; in.readDouble();
}
&#64;Override
public String toString() {
return order_id &#43; "\\t" &#43; price;
}
public int getOrder_id() {
return order_id;
}
public void setOrder_id(int order_id) {
this.order_id &#61; order_id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price &#61; price;
}
&#64;Override
public int compareTo(OrderBean o) {
int result;
if (order_id > o.getOrder_id()) {
result &#61; 1;
} else if (order_id < o.getOrder_id()) {
result &#61; -1;
} else {
result &#61; price > o.getPrice() ? -1 : 1;
}
return result;
}
}
&#xff08;2&#xff09;编写OrderSortMapper类
package com.jinghang.mapreduce.order;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class OrderMapper extends Mapper<LongWritable, Text, OrderBean, NullWritable> {
OrderBean k &#61; new OrderBean();
&#64;Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line &#61; value.toString();
String[] fields &#61; line.split("\\t");
k.setOrder_id(Integer.parseInt(fields[0]));
k.setPrice(Double.parseDouble(fields[2]));
context.write(k, NullWritable.get());
}
}
&#xff08;3&#xff09;编写OrderSortGroupingComparator类
package com.jinghang.mapreduce.order;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
public class OrderGroupingComparator extends WritableComparator {
protected OrderGroupingComparator() {
super(OrderBean.class, true);
}
&#64;Override
public int compare(WritableComparable a, WritableComparable b) {
OrderBean aBean &#61; (OrderBean) a;
OrderBean bBean &#61; (OrderBean) b;
int result;
if (aBean.getOrder_id() > bBean.getOrder_id()) {
result &#61; 1;
} else if (aBean.getOrder_id() < bBean.getOrder_id()) {
result &#61; -1;
} else {
result &#61; 0;
}
return result;
}
}
&#xff08;4&#xff09;编写OrderSortReducer类
package com.jinghang.mapreduce.order;
import java.io.IOException;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;
public class OrderReducer extends Reducer<OrderBean, NullWritable, OrderBean, NullWritable> {
&#64;Override
protected void reduce(OrderBean key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
context.write(key, NullWritable.get());
}
}
&#xff08;5&#xff09;编写OrderSortDriver类
package com.jinghang.mapreduce.order;
import java.io.IOException;
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;
public class OrderDriver {
public static void main(String[] args) throws Exception, IOException {
args &#61; new String[]{"e:/input/inputorder" , "e:/output1"};
Configuration conf &#61; new Configuration();
Job job &#61; Job.getInstance(conf);
job.setJarByClass(OrderDriver.class);
job.setMapperClass(OrderMapper.class);
job.setReducerClass(OrderReducer.class);
job.setMapOutputKeyClass(OrderBean.class);
job.setMapOutputValueClass(NullWritable.class);
job.setOutputKeyClass(OrderBean.class);
job.setOutputValueClass(NullWritable.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setGroupingComparatorClass(OrderGroupingComparator.class);
boolean result &#61; job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}