辅助排序和二次排序案例(GroupingComparator)
案例
现在需要求出每一个订单中最贵的商品。
[collapse title="数据 GroupingComparator.txt"]
0000001 Pdt_01 222.8
0000001 Pdt_05 722.4
0000002 Pdt_05 25.8
0000002 Pdt_01 222.8
0000002 Pdt_01 33.8
0000003 Pdt_03 522.8
0000003 Pdt_04 122.4
[/collapse]
[collapse title="输出数据预期"]
3 222.8
2 722.4
1 222.8
[/collapse]
code
[collapse title="ruaDriver"]
package com.kami.demo01;
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;
import java.io.IOException;
/**
* @version v 1.0
* @Author kamisamak
* @Date 2020/6/17
*/
public class ruaDriver {
public static void main(String[] args) throws Exception, IOException {
args = new String[]{"data\\d01\\GroupingComparator.txt","output\\d01"};
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(ruaDriver.class);
// job.setNumReduceTasks(3);
job.setGroupingComparatorClass(ruaGroupingComparator.class);
job.setPartitionerClass(ruaPartitioner.class);
job.setMapperClass(ruaMapper.class);
job.setReducerClass(ruaReducer.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]));
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}
[/collapse]
[collapse title="OrderBean"]
package com.kami.demo01;
import org.apache.hadoop.io.WritableComparable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
* @version v 1.0
* @Author kamisamak
* @Date 2020/6/17
*/
public class OrderBean implements WritableComparable {
// 订单id号
private int order_id;
// 价格
private double price;
public OrderBean() {
super();
}
public OrderBean(int order_id, double price) {
super();
this.order_id = order_id;
this.price = price;
}
@Override
public void write(DataOutput out) throws IOException {
out.writeInt(order_id);
out.writeDouble(price);
}
@Override
public void readFields(DataInput in) throws IOException {
order_id = in.readInt();
price = in.readDouble();
}
@Override
public String toString() {
return order_id + "\t" + price;
}
public int getOrder_id() {
return order_id;
}
public void setOrder_id(int order_id) {
this.order_id = order_id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
// 二次排序
@Override
public int compareTo(OrderBean o) {
int result = order_id > o.getOrder_id() ? 1 : -1;
if (order_id > o.getOrder_id()) {
result = 1;
} else if (order_id < o.getOrder_id()) {
result = -1;
} else {
// 价格倒序排序
result = price > o.getPrice() ? -1 : 1;
}
return result;
}
}
[/collapse]
[collapse title="ruaGroupingComparator"]
package com.kami.demo01;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
/**
* @version v 1.0
* @Author kamisamak
* @Date 2020/6/17
*/
public class ruaGroupingComparator extends WritableComparator {
protected ruaGroupingComparator() {
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.getOrder_id() > bBean.getOrder_id()) {
result = 1;
} else if (aBean.getOrder_id() < bBean.getOrder_id()) {
result = -1;
} else {
result = 0;
}
return result;
}
}
[/collapse]
[collapse title="ruaMapper"]
package com.kami.demo01;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
/**
* @version v 1.0
* @Author kamisamak
* @Date 2020/6/17
*/
public class ruaMapper extends Mapper {
OrderBean k = new OrderBean();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] fields = value.toString().split("\t");
k.setOrder_id(Integer.parseInt(fields[0]));
k.setPrice(Double.parseDouble(fields[2]));
context.write(k, NullWritable.get());
}
}
[/collapse]
[collapse title="ruaPartitioner"]
package com.kami.demo01;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Partitioner;
/**
* @version v 1.0
* @Author kamisamak
* @Date 2020/6/17
*/
public class ruaPartitioner extends Partitioner {
@Override
public int getPartition(OrderBean key, NullWritable value, int numReduceTasks) {
return (key.getOrder_id() & Integer.MAX_VALUE) % numReduceTasks;
}
}
[/collapse]
[collapse title="ruaReducer"]
package com.kami.demo01;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
/**
* @version v 1.0
* @Author kamisamak
* @Date 2020/6/17
*/
public class ruaReducer extends Reducer {
@Override
protected void reduce(OrderBean key, Iterable values, Context context) throws IOException, InterruptedException {
context.write(key, NullWritable.get());
}
}
[/collapse]
文章评论