1 需求有如下订单数据,现在需要求出每一个订单中最贵的商品。
0000001 Pdt_01 222.8
0000001 Pdt_06 25.8
0000002 Pdt_03 522.8
0000002 Pdt_04 122.4
0000002 Pdt_05 722.4
0000003 Pdt_01 222.8
0000003 Pdt_02 33.8
2 分析
1)利用“订单 id 和成交金额”作为 key,可以将 map 阶段读取到的所有订单数据按
照 id 分区,按照金额排序,发送到 reduce。
2)在 reduce 端利用 groupingcomparator 将订单 id 相同的 kv 聚合成组,然后取第一个即是最大值。
3 代码实现
1) 定义订单信息 OrderBean
package com.da.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 orderId;
private double price;
public OrderBean() {
super();
}
public OrderBean(int orderId, double price) {
super();
this.orderId = orderId;
this.price = price;
}
public void set(int orderId, double price) {
this.orderId = orderId;
this.price = price;
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(orderId);
out.writeDouble(price);
}
@Override
public void readFields(DataInput in) throws IOException {
this.orderId = in.readInt();
this.price = in.readDouble();
}
@Override
public String toString() {
return orderId + "\t" + price;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public int compareTo(OrderBean o) {
int result;
if (this.orderId > o.getOrderId()) {
result = 1;
} else if (this.orderId < o.getOrderId()) {
result = -1;
} else {
result = this.price > o.getPrice() ? -1 : 1;
}
return result;
}
}
2) 编写 OrderMapper
package com.da.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> {
private OrderBean k = new OrderBean();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// 1 获取一行
String line = value.toString();
// 2 截取
String[] files = line.split(" ");
// 3 封装对象
int id = Integer.parseInt(files[0]);
double price = Double.parseDouble(files[2]);
k.set(id, price);
// 4 写出
context.write(k, NullWritable.get());
}
}
3) 编写 OrderPartitioner
package com.da.order;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Partitioner;
public class OrderPartitioner extends Partitioner<OrderBean, NullWritable> {
@Override
public int getPartition(OrderBean key, NullWritable value, int numPartitions) {
return (key.getOrderId() & Integer.MAX_VALUE) % numPartitions;
}
}
4)编写 OrderGroupingComparator
package com.da.order;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
public class OrderGroupingComparator extends WritableComparator {
protected OrderGroupingComparator() {
super(OrderBean.class, true);
}
@SuppressWarnings("rawtypes")
@Override
public int compare(WritableComparable a, WritableComparable b) {
OrderBean aBean = (OrderBean) a;
OrderBean bBean = (OrderBean) b;
int result;
if (aBean.getOrderId() > bBean.getOrderId()) {
result = 1;
} else if (aBean.getOrderId() < bBean.getOrderId()) {
result = -1;
} else {
result = 0;
}
return result;
}
}
5) 编写 OrderReducer
package com.da.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> {
@Override
protected void reduce(OrderBean key, Iterable<NullWritable> values, Context context)
throws IOException, InterruptedException {
context.write(key, NullWritable.get());
}
}
6) 编写 OrderDriver
package com.da.order;
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 {
// 1 获取配置信息
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
// 2 设置 jar 包加载路径
job.setJarByClass(OrderDriver.class);
// 3 加载 map/reduce 类
job.setMapperClass(OrderMapper.class);
job.setReducerClass(OrderReducer.class);
// 4 设置 map 输出数据 key 和 value 类型
job.setMapOutputKeyClass(OrderBean.class);
job.setMapOutputValueClass(NullWritable.class);
// 5 设置最终输出数据的 key 和 value 类型
job.setOutputKeyClass(OrderBean.class);
job.setOutputValueClass(NullWritable.class);
// 6 设置输入数据和输出数据路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
// 10 设置 reduce 端的分组
job.setGroupingComparatorClass(OrderGroupingComparator.class);
// 7 设置分区
job.setPartitionerClass(OrderPartitioner.class);
// 8 设置 reduce 个数
job.setNumReduceTasks(3);
// 9 提交
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}