MapReduce学习总结之Combiner、Partitioner、Jobhistory
一、Combiner
在MapReduce编程模型中,在Mapper和Reducer之间有一个非常重要的组件,主要用于解决MR性能瓶颈问题

combiner其实属于优化方案,由于带宽限制,应该尽量map和reduce之间的数据传输数量。它在Map端把同一个key的键值对合并在一起并计算,计算规则和reduce一致,所以combiner也可以看作特殊的Reducer(本地reduce)。
执行combiner操作要求开发者必须在程序中设置了combiner(程序中通过job.setCombinerClass(myCombine.class)自定义combiner操作)
wordcount中直接使用myreduce作为combiner:
// 设置Map规约Combiner
job.setCombinerClass(MyReducer.class);
参考资料:https://www.tuicool.com/articles/qAzUjav
二、Partitioner
Partitioner也是MR的重要组件,主要功能如下:
1)Partitioner决定MapTask输出的数据交由哪个ReduceTask处理
2)默认实现:分发的key的hash值对reduceTask 个数取模
which reducer=(key.hashCode() & Integer.MAX_VALUE) % numReduceTasks,得到当前的目的reducer。
例子:
文件内容:xiaomi 200
huawei 500
xiaomi 300
huawei 700
iphonex 100
iphonex 30
iphone7 60
对上面文件内容按手机品牌分类分发到四个reduce处理计算: package rdb.com.hadoop01.mapreduce; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; /**
*
* @author rdb
*
*/
public class PartitionerApp { /**
* map读取输入文件
* @author rdb
*
*/
public static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable>{ @Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, Text, LongWritable>.Context context)
throws IOException, InterruptedException {
//接收每一行数据
String line = value.toString();
//按空格进行分割
String[] words = line.split(" ");
//通过上下文把map处理结果输出
context.write(new Text(words[0]), new LongWritable(Long.parseLong(words[1])));
}
} /**
* reduce程序,归并统计
* @author rdb
*
*/
public static class MyReduce extends Reducer<Text, LongWritable, Text, LongWritable>{ @Override
protected void reduce(Text key, Iterable<LongWritable> values,
Reducer<Text, LongWritable, Text, LongWritable>.Context context)
throws IOException, InterruptedException {
long sum = 0;
for (LongWritable value : values){
//求单词次数
sum += value.get();
}
//通过上下文把reduce处理结果输出
context.write(key, new LongWritable(sum));
}
} /**
* 自定义partition
* @author rdb
*
*/
public static class MyPartitioner extends Partitioner<Text, LongWritable>{ @Override
public int getPartition(Text key, LongWritable value, int numPartitions) {
if(key.toString().equals("xiaomi")){
return 0;
}
if(key.toString().equals("huawei")){
return 1;
}
if(key.toString().equals("iphonex")){
return 2;
}
return 3;
} } /**
* 自定义driver:封装mapreduce作业所有信息
*@param args
* @throws IOException
*/
public static void main(String[] args) throws Exception { //创建配置
Configuration configuration = new Configuration(); //清理已经存在的输出目录
Path out = new Path(args[1]);
FileSystem fileSystem = FileSystem.get(configuration);
if(fileSystem.exists(out)){
fileSystem.delete(out, true);
System.out.println("output exists,but it has deleted");
} //创建job
Job job = Job.getInstance(configuration,"WordCount"); //设置job的处理类
job.setJarByClass(PartitionerApp.class); //设置作业处理的输入路径
FileInputFormat.setInputPaths(job, new Path(args[0])); //设置map相关的参数
job.setMapperClass(MyMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class); //设置reduce相关参数
job.setReducerClass(MyReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class); //设置combiner处理类,逻辑上和reduce是一样的
//job.setCombinerClass(MyReduce.class); //设置job partition
job.setPartitionerClass(MyPartitioner.class);
//设置4个reducer,每个分区一个
job.setNumReduceTasks(4); //设置作业处理的输出路径
FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true)? 0 : 1) ;
}
} 打包后调用:hadoop jar ~/lib/hadoop01-0.0.1-SNAPSHOT.jar rdb.com.hadoop01.mapreduce.PartitionerApp
hdfs://hadoop01:8020/partitioner.txt hdfs://hadoop01:8020/output/partitioner 结果: -rw-r--r-- 1 hadoop supergroup 11 2018-05-09 06:35 /output/partitioner/part-r-00000
-rw-r--r-- 1 hadoop supergroup 12 2018-05-09 06:35 /output/partitioner/part-r-00001
-rw-r--r-- 1 hadoop supergroup 12 2018-05-09 06:35 /output/partitioner/part-r-00002
-rw-r--r-- 1 hadoop supergroup 11 2018-05-09 06:35 /output/partitioner/part-r-00003 [hadoop@hadoop01 lib]$ hadoop fs -text /output/partitioner/part-r-00000
18/05/09 06:36:37 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
xiaomi 500
[hadoop@hadoop01 lib]$ hadoop fs -text /output/partitioner/part-r-00001
18/05/09 06:36:43 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
huawei 1200
[hadoop@hadoop01 lib]$ hadoop fs -text /output/partitioner/part-r-00002
18/05/09 06:36:49 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
iphonex 130
[hadoop@hadoop01 lib]$ hadoop fs -text /output/partitioner/part-r-00003
18/05/09 06:36:57 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
iphone7 60
三、Jobhistory
JobHistory用来记录已经finished的mapreduce运行日志,日志信息存放于HDFS目录中,默认情况下没有开启此功能。需要配置。
1)配置hadoop-2.6.0-cdh5.7.0/etc/hadoop/mapred-site.xml
<property>
<name>mapreduce.jobhistory.address</name>
<value>hadoop01:10020</value>
<description>MR JobHistory Server管理的日志的存放位置</description>
</property>
<property>
<name>mapreduce.jobhistory.webapp.address</name>
<value>hadoop01:19888</value>
<description>查看历史服务器已经运行完的Mapreduce作业记录的web地址,需要启动该服务才行</description>
</property>
<property>
<name>mapreduce.jobhistory.done-dir</name>
<value>/history/done</value>
<description>MR JobHistory Server管理的日志的存放位置,默认:/mr-history/done</description>
</property>
<property>
<name>mapreduce.jobhistory.intermediate-done-dir</name>
<value>/history/done_intermediate</value>
<description>MapReduce作业产生的日志存放位置,默认值:/mr-history/tmp</description>
</property>
2)配置好后重启yarn.启动jobhistory服务:hadoop-2.6.0-cdh5.7.0/sbin/mr-jobhistory-daemon.sh start historyserver
[hadoop@hadoop01 sbin]$ jps
24321 JobHistoryServer
24353 Jps
23957 NodeManager
7880 DataNode
8060 SecondaryNameNode
23854 ResourceManager
7791 NameNode
[hadoop@hadoop01 sbin]$
3)浏览器访问 :http://192.168.44.183:19888/
后台跑一个MapReduce程序:hadoop jar ~/lib/hadoop01-0.0.1-SNAPSHOT.jar rdb.com.hadoop01.mapreduce.WordCountApp hdfs://hadoop01:8020/hello.txt hdfs://hadoop01:8020/output/wc
刷新下浏览器可以看到刚才程序的日志:


点击页面中对应mr程序中的logs可以看详细日志。
问题记录:

MapReduce学习总结之Combiner、Partitioner、Jobhistory的更多相关文章
- 第2节 mapreduce深入学习:7、MapReduce的规约过程combiner
第2节 mapreduce深入学习:7.MapReduce的规约过程combiner 每一个 map 都可能会产生大量的本地输出,Combiner 的作用就是对 map 端的输出先做一次合并,以减少在 ...
- Hadoop之MapReduce学习笔记(二)
主要内容: mapreduce编程模型再解释: ob提交方式: windows->yarn windows->local : linux->local linux->yarn: ...
- MapReduce学习
参考文章 参考文章2 shuffle的过程分析 Hadoop学习笔记:MapReduce框架详解 谈mapreduce运行机制,可以从很多不同的角度来描述,比如说从mapreduce运行流程来讲解,也 ...
- Hadoop学习笔记—8.Combiner与自定义Combiner
一.Combiner的出现背景 1.1 回顾Map阶段五大步骤 在第四篇博文<初识MapReduce>中,我们认识了MapReduce的八大步凑,其中在Map阶段总共五个步骤,如下图所示: ...
- MapReducer Counter计数器的使用,Combiner ,Partitioner,Sort,Grop的使用,
一:Counter计数器的使用 hadoop计数器:可以让开发人员以全局的视角来审查程序的运行情况以及各项指标,及时做出错误诊断并进行相应处理. 内置计数器(MapReduce相关.文件系统相关和作业 ...
- mapreduce学习指导及疑难解惑汇总
原文链接http://www.aboutyun.com/thread-7091-1-1.html 1.思想起源: 我们在学习mapreduce,首先我们从思想上来认识.其实任何的奇思妙想,抽象的,好的 ...
- mapreduce (二) MapReduce实现倒排索引(一) combiner是把同一个机器上的多个map的结果先聚合一次
1 思路:0.txt MapReduce is simple1.txt MapReduce is powerfull is simple2.txt Hello MapReduce bye MapRed ...
- MapReduce学习总结之简介
执行步骤:1)准备Map处理的输入数据 2)Mapper处理 3)Shuffle 4)Reduce处理 5)结果输出 三.mapreduce核心概念: 1)split:交由MapReduce作业来处理 ...
- Hadoop - MapReduce学习笔记(详细)
第1章 MapReduce概述 定义:是一个分布式运算程序的编程框架 优缺点:易于编程.良好的扩展性.高容错性.适合PB级以上数据的离线处理 核心思想:MapReduce 编程模型只能包含一个Map ...
随机推荐
- RTOS诊断和错误检查
RTOS诊断和错误检查 RTOS diagnostics and error checking 查看RTOS显示系列 错误处理不太可能是任何用于嵌入式系统应用程序的操作系统的主要功能.这是资源限制的必 ...
- fiddler选项卡-Filters(过滤)
Filter filter的意思是过滤,在fiddler中,它可以用来过滤请求,使得session列表能够更加精准的展现抓到的数据流,而不是杂乱的一堆. 1.filter的界面 2.界面详解 1.Us ...
- NOIP模拟测试21「折纸·不等式」
折纸 题解 考试时无限接近正解,然而最终也只是接近而已了 考虑模拟会爆炸,拿手折纸条试一试,很简单 考你动手能力 代码 #include<bits/stdc++.h> using name ...
- bzoj2839 集合计数(容斥+组合)
集合计数 内存限制:128 MiB 时间限制:1000 ms 标准输入输出 题目描述 一个有N个元素的集合有2^N个不同子集(包含空集),现在要在这2^N个集合中取出若干集合(至少一个),使得 ...
- token & refresh token 机制总结
token & refresh token 机制总结 废话 我在项目上写了个配置页面,之前很简单直接登录,毕竟配置页面自己人用就没有做token机制,后来公司的安全审核不过,现在要加上toke ...
- 『无为则无心』Python基础 — 15、Python流程控制语句(for循环语句)
目录 1.for循环语法 2.for循环中的break和continue 3.循环+else结构 (1)while...else (2)while...else退出循环的方式 (3)for...els ...
- 精尽Spring Boot源码分析 - 支持外部 Tomcat 容器的实现
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- JS 使用try catch捕获异常
JS 使用try catch捕获异常 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 简介 前端是攻克客户的先锋,需要特别注意到 ...
- Docker安装运行Portainer
基本简介 Portainer是一个轻量级的docker环境管理UI,可以用来管理docker宿主机和docker swarm集群.他的轻量级,轻量到只要个不到100M的docker镜像容器就可以完整的 ...
- oracle 日常运维
1.查询表或存储过程.函数异常 select * from user_errors where name ='TEST_TABLE' 2.查询表是否存在 select * from user_tabl ...