027_编写MapReduce的模板类Mapper、Reducer和Driver
模板类编写好后写MapReduce程序,的模板类编写好以后只需要改参数就行了,代码如下:
package org.dragon.hadoop.mr.module; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.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;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; /**
*
* ###########################################
* ############ MapReduce 模板类 ##########
* ###########################################
*
* @author ZhuXY
* @time 2016-3-13 下午10:21:06
*
*/
public class ModuleMapReduce extends Configured implements Tool { /**
* Mapper Class
*/
public static class ModuleMapper extends
Mapper<LongWritable, Text, LongWritable, Text> { @Override
protected void setup(Context context) throws IOException,
InterruptedException {
super.setup(context);
} @Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
super.map(key, value, context);
} @Override
protected void cleanup(Context context) throws IOException,
InterruptedException {
super.cleanup(context);
}
} /**
* Reducer Class
*/
public static class ModuleReducer extends
Reducer<LongWritable, Text, LongWritable, Text> { @Override
protected void setup(Context context) throws IOException,
InterruptedException {
// TODO Auto-generated method stub
super.setup(context);
} @Override
protected void reduce(LongWritable key, Iterable<Text> values,
Context context) throws IOException, InterruptedException {
// TODO Auto-generated method stub
super.reduce(key, values, context);
} @Override
protected void cleanup(Context context) throws IOException,
InterruptedException {
// TODO Auto-generated method stub
super.cleanup(context);
} } /**
* Driver Class
*/ // 专门抽取一个方法出来用于设置
public Job parseInputAndOutput(Tool tool,Configuration conf,String[] args) throws IOException
{
if (args.length!=2) {
System.err.printf("Usage:%s [generic options] <input> <output>\n", tool.getClass().getSimpleName());
ToolRunner.printGenericCommandUsage(System.err);
return null;
} //创建job,并设置配置信息和job名称
Job job=new Job(conf, ModuleMapReduce.class.getSimpleName()); //设置job的运行类
// step 3:set job
// 1) set run jar class
job.setJarByClass(tool.getClass()); // 14) job output path
FileOutputFormat.setOutputPath(job, new Path(args[1])); return job;
} @Override
public int run(String[] args) throws Exception { // step 1:get conf
Configuration conf = new Configuration(); // step 2:create job
Job job = parseInputAndOutput(this, conf, args); // 2) set input format
job.setInputFormatClass(TextInputFormat.class); // 可省 // 3) set input path
FileInputFormat.addInputPath(job, new Path(args[0])); // 4) set mapper class
job.setMapperClass(ModuleMapper.class); // 可省 // 5)set map input key/value class
job.setMapOutputKeyClass(LongWritable.class); // 可省
job.setMapOutputValueClass(Text.class); // 可省 // 6) set partitioner class
job.setPartitionerClass(HashPartitioner.class); // 可省 // 7) set reducer number
job.setNumReduceTasks(1);// default 1 //可省 // 8)set sort comparator class
//job.setSortComparatorClass(LongWritable.Comparator.class); // 可省 // 9) set group comparator class
//job.setGroupingComparatorClass(LongWritable.Comparator.class); // 可省 // 10) set combiner class
// job.setCombinerClass(null);默认是null,但是此处不能写 //可省 // 11) set reducer class
job.setReducerClass(ModuleReducer.class); // 可省 // 12) set output format
job.setOutputFormatClass(TextOutputFormat.class); // 可省 // 13) job output key/value class
job.setOutputKeyClass(LongWritable.class); // 可省
job.setOutputValueClass(Text.class); // 可省 // step 4: submit job
boolean isSuccess = job.waitForCompletion(true); // step 5: return status
return isSuccess ? 0 : 1;
} public static void main(String[] args) throws Exception { args = new String[] {
"hdfs://hadoop-master.dragon.org:9000/wc/mininput/",
"hdfs://hadoop-master.dragon.org:9000/wc/minoutput"
}; //run mapreduce
int status=ToolRunner.run(new ModuleMapReduce(), args); //exit
System.exit(status);
}
}
View Module Code
模板使用步骤:
1) 改名称(MapReduce类的名称、Mapper类的名称、Reducer类的名称)
2) 依据实际的业务逻辑修改Mapper类和Reducer类的Key/Value输入输出参数的类型
3) 修改驱动Driver部分的Job的参数设置(Mapper类和Reducer类的输出类型)
4) 在Mapper类中编写实际的业务逻辑(setup()、map()、cleanup())
5) 在Reducer类中编写实际的业务逻辑(setup()、map()、cleanup())
6) 检查并修改驱动Driver代码(模板类中的run()方法)
7) 设置输入输出路径,进行MR测试。
使用ModuleMapReduce编写wordcount程序
package org.dragon.hadoop.mr.module; import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.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;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; /**
*
* ###########################################
* ############ MapReduce 模板类 ##########
* ###########################################
*
* @author ZhuXY
* @time 2016-3-13 下午10:21:06
*
*/
public class WordcountByModuleMapReduce extends Configured implements Tool { /**
* Mapper Class
*/
public static class WordcountMapper extends
Mapper<LongWritable, Text, Text, LongWritable> { @Override
protected void setup(Context context) throws IOException,
InterruptedException {
super.setup(context);
} private Text word = new Text();
private final static LongWritable one = new LongWritable(1); @Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException { // 获取每行数据的值
String lineValue = value.toString(); // 进行分割
StringTokenizer stringTokenizer = new StringTokenizer(lineValue); // 遍历
while (stringTokenizer.hasMoreElements()) { // 获取每个值
String worldValue = stringTokenizer.nextToken(); // 设置map, 输入的key值
word.set(worldValue);
context.write(word, one); // 如果出现就出现一次,存在每行出现几次,这时候键的值一样,多个键值对
}
} @Override
protected void cleanup(Context context) throws IOException,
InterruptedException {
super.cleanup(context);
}
} /**
* Reducer Class
*/
public static class WordcountReducer extends
Reducer<Text, LongWritable, Text, LongWritable> {
private LongWritable resultLongWritable = new LongWritable(); @Override
protected void setup(Context context) throws IOException,
InterruptedException {
// TODO Auto-generated method stub
super.setup(context);
} @Override
protected void reduce(Text key, Iterable<LongWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
// 循环遍历Interable
for (LongWritable value : values) {
// 累加
sum += value.get();
} // 设置总次数
resultLongWritable.set(sum);
context.write(key, resultLongWritable);
} @Override
protected void cleanup(Context context) throws IOException,
InterruptedException {
// TODO Auto-generated method stub
super.cleanup(context);
} } /**
* Driver Class
*/ // 专门抽取一个方法出来用于设置
public Job parseInputAndOutput(Tool tool, Configuration conf, String[] args)
throws IOException {
if (args.length != 2) {
System.err.printf("Usage:%s [generic options] <input> <output>\n",
tool.getClass().getSimpleName());
ToolRunner.printGenericCommandUsage(System.err);
return null;
} // 创建job,并设置配置信息和job名称
Job job = new Job(conf,
WordcountByModuleMapReduce.class.getSimpleName()); // 设置job的运行类
// step 3:set job
// 1) set run jar class
job.setJarByClass(tool.getClass()); // 14) job output path
FileOutputFormat.setOutputPath(job, new Path(args[1])); return job;
} @Override
public int run(String[] args) throws Exception { // step 1:get conf
Configuration conf = new Configuration(); // step 2:create job
Job job = parseInputAndOutput(this, conf, args); // 2) set input format
job.setInputFormatClass(TextInputFormat.class); // 可省 // 3) set input path
FileInputFormat.addInputPath(job, new Path(args[0])); // 4) set mapper class
job.setMapperClass(WordcountMapper.class); // 可省 // 5)set map input key/value class
job.setMapOutputKeyClass(Text.class); // 可省
job.setMapOutputValueClass(LongWritable.class); // 可省 // 6) set partitioner class
job.setPartitionerClass(HashPartitioner.class); // 可省 // 7) set reducer number
job.setNumReduceTasks(1);// default 1 //可省 // 8)set sort comparator class
// job.setSortComparatorClass(LongWritable.Comparator.class); // 可省 // 9) set group comparator class
// job.setGroupingComparatorClass(LongWritable.Comparator.class); // 可省 // 10) set combiner class
// job.setCombinerClass(null);默认是null,但是此处不能写 //可省 // 11) set reducer class
job.setReducerClass(WordcountReducer.class); // 可省 // 12) set output format
job.setOutputFormatClass(TextOutputFormat.class); // 可省 // 13) job output key/value class
job.setOutputKeyClass(Text.class); // 可省
job.setOutputValueClass(LongWritable.class); // 可省 // step 4: submit job
boolean isSuccess = job.waitForCompletion(true); // step 5: return status
return isSuccess ? 0 : 1;
} public static void main(String[] args) throws Exception { args = new String[] {
"hdfs://hadoop-master.dragon.org:9000/wc/mininput/",
"hdfs://hadoop-master.dragon.org:9000/wc/minoutput" }; // run mapreduce
int status = ToolRunner.run(new WordcountByModuleMapReduce(), args); // exit
System.exit(status);
}
}
View WordcountByModuleMapReduce Code
027_编写MapReduce的模板类Mapper、Reducer和Driver的更多相关文章
- Hadoop(十七)之MapReduce作业配置与Mapper和Reducer类
前言 前面一篇博文写的是Combiner优化MapReduce执行,也就是使用Combiner在map端执行减少reduce端的计算量. 一.作业的默认配置 MapReduce程序的默认配置 1)概述 ...
- 024_MapReduce中的基类Mapper和基类Reducer
内容提纲 1) MapReduce中的基类Mapper类,自定义Mapper类的父类. 2) MapReduce中的基类Reducer类,自定义Reducer类的父类. 1.Mapper类 API文档 ...
- [Hadoop in Action] 第4章 编写MapReduce基础程序
基于hadoop的专利数据处理示例 MapReduce程序框架 用于计数统计的MapReduce基础程序 支持用脚本语言编写MapReduce程序的hadoop流式API 用于提升性能的Combine ...
- Hadoop:使用Mrjob框架编写MapReduce
Mrjob简介 Mrjob是一个编写MapReduce任务的开源Python框架,它实际上对Hadoop Streaming的命令行进行了封装,因此接粗不到Hadoop的数据流命令行,使我们可以更轻松 ...
- 如何在maven项目里面编写mapreduce程序以及一个maven项目里面管理多个mapreduce程序
我们平时创建普通的mapreduce项目,在遍代码当你需要导包使用一些工具类的时候, 你需要自己找到对应的架包,再导进项目里面其实这样做非常不方便,我建议我们还是用maven项目来得方便多了 话不多说 ...
- 整合使用持久层框架mybatis 使用SqlSessionTemplate模板类与使用映射接口 对比
spring中整合使用mybatis的用法总结 一:在Spring配置Mybatis 第一步:将mybatis-spring类包添加 到项目的类库中 第二步:编写spring和持久层衔接的xml文件, ...
- c++模板类
c++模板类 理解编译器的编译模板过程 如何组织编写模板程序 前言常遇到询问使用模板到底是否容易的问题,我的回答是:“模板的使用是容易的,但组织编写却不容易”.看看我们几乎每天都能遇到的模板类吧,如S ...
- C++ - 模板类模板成员函数(member function template)隐式处理(implicit)变化
模板类模板成员函数(member function template)隐式处理(implicit)变化 本文地址: http://blog.csdn.net/caroline_wendy/articl ...
- 开涛spring3(7.2) - 对JDBC的支持 之 7.2 JDBC模板类
7.2 JDBC模板类 7.2.1 概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDB ...
随机推荐
- struts2的validate在使用过程中的一个问题
在项目中有一个新增客户信息的的功能: 1.在进入加入页面:add.jsp页面之前,要调用一个add_init.do来获取省份信息列表以供在add.jsp进行选择. 2.add页面填写完毕以后.提交给 ...
- Shiro学习(总结)
声明:本文原文地址:http://www.iteye.com/blogs/subjects/shiro 感谢开涛提供的博文,让我学到了非常多.在这里由衷的感谢你,同一时候我强烈的推荐开涛的博文.他的博 ...
- 做前端(单纯页面和js)遇到的问题辑录(一)
html标签的name和id的值一样,jQuery在选择的时候会混乱么? 1.超链接<a href="http://www.jb51.net" title="脚本之 ...
- 做自己生活的导演:华为CameraKit为你加持大师光环
今年最流行的娱乐方式,无疑是短视频,抖音等短视频平台,越来越多的消费者沉浸其中.除了看别人拍的短视频用以丰富生活乐趣之外,也有不少跃跃欲试的消费者加入到短视频拍摄的行列中.随着拍摄者的增加,对拍摄设备 ...
- RPMforge(Repoforge)源
centos使用rpmforge-release 时间:2017-10-09 09:48:29 阅读:536 评论:0 收藏:0 [点我收藏+] 标签:rpmf ...
- Swoole系列(二):安装
Window是没办法安装的,服务器版本建议用linux的centos7 Php版本5.4 安装步骤: 1.更新你的yum yum update 2.安装php相关扩展 2.yum install ph ...
- 通过Bag一对多映射示例(使用xml文件)
如果持久化类具有包含实体引用的列表对象,则需要使用一对多关联来映射列表元素. 我们可以通过列表(list)或包(bag)来映射这个列表对象. 请注意,bag不是基于索引的,而list是基于索引的. 在 ...
- android中延迟执行某个任务
android App开发在某些情况下需要有延时功能,比如说App首页显示定格3秒,然后自动跳到登录页的情况,这就好比是一个预加载,但是这个预加载可能瞬间就完成了,撑不到3秒钟,这是就要求你做延时处理 ...
- day8笔记
一.上节回顾 1,id() 内存地址2, == 比较的是值 is 比较的是内存地址 数字,字符串,有小数据池, #内存地址一样的 int -5--256 str:1,不能有空格. 2,长度不能超过20 ...
- python资源网站
whl安装包资源网站:http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy