首页 > 其他分享 >MapReduce实战之辅助排序和二次排序案例

MapReduce实战之辅助排序和二次排序案例

时间:2022-11-11 11:05:36浏览次数:42  
标签:实战 hadoop MapReduce id org apache import 排序 order


辅助排序和二次排序案例

1)需求

有如下订单数据

订单id

商品id

成交金额

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)输入数据

0000001    Pdt_01    222.8
0000002    Pdt_06    722.4
0000001    Pdt_05    25.8
0000003    Pdt_01    222.8
0000003    Pdt_01    33.8
0000002    Pdt_03    522.8
0000002    Pdt_04    122.4

输出数据预期:

0:3    222.8

1:2    722.4

2:1    222.8

3)分析

(1)利用“订单id和成交金额”作为key,可以将map阶段读取到的所有订单数据按照id分区,按照金额排序,发送到reduce。

(2)在reduce端利用groupingcomparator将订单id相同的kv聚合成组,然后取第一个即是最大值。

MapReduce实战之辅助排序和二次排序案例_mapreduce

4)代码实现

(1)定义订单信息OrderBean

package com.atguigu.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; // 订单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;

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;
}
}

(2)编写OrderSortMapper

package com.atguigu.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 = new OrderBean();

@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

// 1 获取一行
String line = value.toString();

// 2 截取
String[] fields = line.split("\t");

// 3 封装对象
k.setOrder_id(Integer.parseInt(fields[0]));
k.setPrice(Double.parseDouble(fields[2]));

// 4 写出
context.write(k, NullWritable.get());
}
}

(3)编写OrderSortPartitioner

package com.atguigu.mapreduce.order;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Partitioner;

publicclass OrderPartitioner extends Partitioner<OrderBean, NullWritable> {

@Override
publicint getPartition(OrderBean key, NullWritable value, int numReduceTasks) {

return (key.getOrder_id() & Integer.MAX_VALUE) % numReduceTasks;
}
}

(4)编写OrderSortGroupingComparator

package com.atguigu.mapreduce.order;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

publicclass OrderGroupingComparator extends WritableComparator {

protected OrderGroupingComparator() {
super(OrderBean.class, true);
}

@SuppressWarnings("rawtypes")
@Override
publicint compare(WritableComparable a, WritableComparable b) {

OrderBean aBean = (OrderBean) a;
OrderBean bBean = (OrderBean) b;

int result;
if (aBean.getOrder_id() > bBean.getOrder_id()) {
result = 1;
} elseif (aBean.getOrder_id() < bBean.getOrder_id()) {
result = -1;
} else {
result = 0;
}

return result;
}
}

(5)编写OrderSortReducer

package com.atguigu.mapreduce.order;
import java.io.IOException;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;

publicclass OrderReducer extends Reducer<OrderBean, NullWritable, OrderBean, NullWritable> {

@Override
protectedvoid reduce(OrderBean key, Iterable<NullWritable> values, Context context)
throws IOException, InterruptedException {

context.write(key, NullWritable.get());
}
}

(6)编写OrderSortDriver

package com.atguigu.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;

publicclass OrderDriver {

publicstaticvoid main(String[] args) throws Exception, IOException {

// 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);
}
}

标签:实战,hadoop,MapReduce,id,org,apache,import,排序,order
From: https://blog.51cto.com/u_12654321/5843249

相关文章

  • MapReduce实战之日志清洗案例
    简单解析版1)需求:去除日志中字段长度小于等于11的日志。2)输入数据   数据有点大3)实现代码:(1)编写LogMapperpackagecom.atguigu.mapreduce.weblog;importjava.io.IOExc......
  • MapReduce实战之 MapReduce中多表合并案例
     MapReduce中多表合并案例1)需求:订单数据表t_order:idpidamount1001011100202210030331001   01   11002   02   21003   03   31004   01 ......
  • MapReduce实战之倒排索引案例(多job串联)
    0)需求:有大量的文本(文档、网页),需要建立搜索索引输出数据:a:atguigupingpingatguigussatguigussb:atguigupingpingatguigupingpingpingpingssc:atguigussatguigup......
  • MapReduce实战之压缩/解压缩案例
    1数据流的压缩和解压缩CompressionCodec有两个方法可以用于轻松地压缩或解压缩数据。要想对正在被写入一个输出流的数据进行压缩,我们可以使用createOutputStream(OutputStr......
  • Linux vmstat命令实战详解
    vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况。这个命令是我查看Linux/Unix......
  • 0:Base API-Java API 实战
    目录​​0.1引言​​​​0.2API的定义和用处​​​​0.3Scanner(普通类)​​​​0.4Number(包装类)​​​​0.5Math(工具类)​​​​0.6Random(父子类)​​​​0.7ThreadLoca......
  • 1:Unit test and main function-Java API 实战
    目录​​1.抛出企业问题,脱离main测试,模块化编程​​​​2.Junit单元测试的含义和用途​​​​3.怎么获取各种Jar包?MavenRepository获取各类各个版本的jar,这就是仓库。......
  • 4:File-Java API 实战
    目录​​1.引言​​​​2.绝对路径和相对路径?先学送快递吧!​​​​3.绝对路径​​​​4.相对路径​​​​5.File类​​​​6.Linux上的绝对路径有所不同​​1.引言文......
  • 冒泡排序
    publicint[]sortMaopao(int[]arry){for(inti=0;i<arry.length;i++){for(intj=0;j<arry.length-1-i;j++){......
  • 力扣 81. 搜索旋转排序数组 II
    81.搜索旋转排序数组II已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同。在传递给函数之前,nums 在预先未知的某个下标 k(0<=k<nums.leng......