当前位置:网站首页>Mapreduce实例(四):自然排序
Mapreduce实例(四):自然排序
2022-07-06 09:01:00 【笑看风云路】
大家好,我是风云,欢迎大家关注我的博客 或者 微信公众号【笑看风云路】,在未来的日子里我们一起来学习大数据相关的技术,一起努力奋斗,遇见更好的自己!
流程分析
Map、Reduce任务中Shuffle和排序的过程图如下:
流程分析:
1.Map端:
(1)每个输入分片会让一个map任务来处理,默认情况下,以HDFS的一个块的大小(默认为64M)为一个分片,当然我们也可以设置块的大小。map输出的结果会暂且放在一个环形内存缓冲区中(该缓冲区的大小默认为100M,由io.sort.mb属性控制),当该缓冲区快要溢出时(默认为缓冲区大小的80%,由io.sort.spill.percent属性控制),会在本地文件系统中创建一个溢出文件,将该缓冲区中的数据写入这个文件。
(2)在写入磁盘之前,线程首先根据reduce任务的数目将数据划分为相同数目的分区,也就是一个reduce任务对应一个分区的数据。这样做是为了避免有些reduce任务分配到大量数据,而有些reduce任务却分到很少数据,甚至没有分到数据的尴尬局面。其实分区就是对数据进行hash的过程。然后对每个分区中的数据进行排序,如果此时设置了Combiner,将排序后的结果进行Combine操作,这样做的目的是让尽可能少的数据写入到磁盘。
(3)当map任务输出最后一个记录时,可能会有很多的溢出文件,这时需要将这些文件合并。合并的过程中会不断地进行排序和combine操作,目的有两个:①尽量减少每次写入磁盘的数据量。②尽量减少下一复制阶段网络传输的数据量。最后合并成了一个已分区且已排序的文件。为了减少网络传输的数据量,这里可以将数据压缩,只要将mapred.compress.map.out设置为true就可以了。
(4)将分区中的数据拷贝给相对应的reduce任务。有人可能会问:分区中的数据怎么知道它对应的reduce是哪个呢?其实map任务一直和其父TaskTracker保持联系,而TaskTracker又一直和JobTracker保持心跳。所以JobTracker中保存了整个集群中的宏观信息。只要reduce任务向JobTracker获取对应的map输出位置就ok了哦。
到这里,map端就分析完了。那到底什么是Shuffle呢?Shuffle的中文意思是“洗牌”,如果我们这样看:一个map产生的数据,结果通过hash过程分区却分配给了不同的reduce任务,是不是一个对数据洗牌的过程呢?
2.Reduce端:
(1)Reduce会接收到不同map任务传来的数据,并且每个map传来的数据都是有序的。如果reduce端接受的数据量相当小,则直接存储在内存中(缓冲区大小由mapred.job.shuffle.input.buffer.percent属性控制,表示用作此用途的堆空间的百分比),如果数据量超过了该缓冲区大小的一定比例(由mapred.job.shuffle.merge.percent决定),则对数据合并后溢写到磁盘中。
(2)随着溢写文件的增多,后台线程会将它们合并成一个更大的有序的文件,这样做是为了给后面的合并节省时间。其实不管在map端还是reduce端,MapReduce都是反复地执行排序,合并操作,现在终于明白了有些人为什么会说:排序是hadoop的灵魂。
(3)合并的过程中会产生许多的中间文件(写入磁盘了),但MapReduce会让写入磁盘的数据尽可能地少,并且最后一次合并的结果并没有写入磁盘,而是直接输入到reduce函数。
熟悉MapReduce的人都知道:排序是MapReduce的天然特性!在数据达到reducer之前,MapReduce框架已经对这些数据按键排序了。但是在使用之前,首先需要了解它的默认排序规则。它是按照key值进行排序的,如果key为封装的int为IntWritable类型,那么MapReduce按照数字大小对key排序,如果Key为封装String的Text类型,那么MapReduce将按照数据字典顺序对字符排序。
了解了这个细节,我们就知道应该使用封装int的Intwritable型数据结构了,也就是在map这里,将读入的数据中要排序的字段转化为Intwritable型,然后作为key值输出(不排序的字段作为value)。reduce阶段拿到<key,value-list>之后,将输入的key作为输出的key,并根据value-list中的元素的个数决定输出的次数。
代码编写
在MapReduce过程中默认就有对数据的排序。它是按照key值进行排序的,如果key为封装int的IntWritable类型,那么MapReduce会按照数字大小对key排序,如果Key为封装String的Text类型,那么MapReduce将按照数据字典顺序对字符排序。在本例中我们用到第一种,key设置为IntWritable类型,其中MapReduce程序主要分为Mapper部分和Reducer部分。
Mapper代码
public static class Map extends Mapper<Object,Text,IntWritable,Text>{
private static Text goods=new Text();
private static IntWritable num=new IntWritable();
public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
String line=value.toString();
String arr[]=line.split("\t");
num.set(Integer.parseInt(arr[1]));
goods.set(arr[0]);
context.write(num,goods);
}
}
在map端采用Hadoop默认的输入方式之后,将输入的value值用split()方法截取,把要排序的点击次数字段转化为IntWritable类型并设置为key,商品id字段设置为value,然后直接输出<key,value>。map输出的<key,value>先要经过shuffle过程把相同key值的所有value聚集起来形成<key,value-list>后交给reduce端。
Reducer代码
public static class Reduce extends Reducer<IntWritable,Text,IntWritable,Text>{
private static IntWritable result= new IntWritable();
//声明对象result
public void reduce(IntWritable key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
for(Text val:values){
context.write(key,val);
}
}
}
reduce端接收到<key,value-list>之后,将输入的key直接复制给输出的key,用for循环遍历value-list并将里面的元素设置为输出的value,然后将<key,value>逐一输出,根据value-list中元素的个数决定输出的次数。
完整代码
package mapreduce;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class OneSort {
public static class Map extends Mapper<Object , Text , IntWritable,Text >{
private static Text goods=new Text();
private static IntWritable num=new IntWritable();
public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
String line=value.toString();
String arr[]=line.split("\t");
num.set(Integer.parseInt(arr[1]));
goods.set(arr[0]);
context.write(num,goods);
}
}
public static class Reduce extends Reducer< IntWritable, Text, IntWritable, Text>{
private static IntWritable result= new IntWritable();
public void reduce(IntWritable key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
for(Text val:values){
context.write(key,val);
}
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
Configuration conf=new Configuration();
Job job =new Job(conf,"OneSort");
job.setJarByClass(OneSort.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
Path in=new Path("hdfs://localhost:9000/mymapreduce3/in/goods_visit1");
Path out=new Path("hdfs://localhost:9000/mymapreduce3/out");
FileInputFormat.addInputPath(job,in);
FileOutputFormat.setOutputPath(job,out);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
-------------- end ----------------
微信公众号:扫描下方二维码
或 搜索 笑看风云路
关注
边栏推荐
- Pytest参数化你不知道的一些使用技巧 /你不知道的pytest
- Blue Bridge Cup_ Single chip microcomputer_ Measure the frequency of 555
- 面渣逆袭:Redis连环五十二问,图文详解,这下面试稳了
- Advanced Computer Network Review(5)——COPE
- Redis之五大基础数据结构深入、应用场景
- Go redis initialization connection
- Selenium+Pytest自动化测试框架实战(下)
- Design and implementation of online shopping system based on Web (attached: source code paper SQL file)
- Kratos ares microservice framework (II)
- One article read, DDD landing database design practice
猜你喜欢
英雄联盟轮播图手动轮播
[Yu Yue education] reference materials of complex variable function and integral transformation of Shenyang University of Technology
Redis之Geospatial
Redis之哨兵模式
Redis' bitmap
Redis之核心配置
Advanced Computer Network Review(3)——BBR
Ijcai2022 collection of papers (continuously updated)
Full stack development of quartz distributed timed task scheduling cluster
Post training quantification of bminf
随机推荐
【shell脚本】使用菜单命令构建在集群内创建文件夹的脚本
为什么要数据分层
Pytest's collection use case rules and running specified use cases
go-redis之初始化连接
CSP student queue
基于B/S的医院管理住院系统的研究与实现(附:源码 论文 sql文件)
[oc foundation framework] - < copy object copy >
Selenium+pytest automated test framework practice (Part 2)
068.查找插入位置--二分查找
How to intercept the string correctly (for example, intercepting the stock in operation by applying the error information)
Advance Computer Network Review(1)——FatTree
Basic usage of xargs command
Global and Chinese markets for modular storage area network (SAN) solutions 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of AVR series microcontrollers 2022-2028: Research Report on technology, participants, trends, market size and share
Redis之Lua脚本
One article read, DDD landing database design practice
Appears when importing MySQL
Multivariate cluster analysis
Five layer network architecture
Go redis initialization connection