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 ...
随机推荐
- Python的open函数
打开一个文件并向其写入内容 Python的open方法用来打开一个文件.第一个參数是文件的位置和文件名称.第二个參数是读写模式.这里我们採用w模式,也就是写模式.在这样的模式下,文件原有的内容将会被删 ...
- FreeBSD编译安装emacs,不要用ports
1. 解压emacs 2. 进入解压之后的目录,执行configure命令,大体配置如下: ./configure --with-x-toolkit=no --with-xpm=no --with-j ...
- Sphinx 安装与使用(2)-- 配置Coreseek
1.必须先关闭守护进程才能做其他的操作(第一次启动不需要这一步) /usr/local/coreseek/bin/searchd --config /usr/local/coreseek/etc/te ...
- 将Mac上的***代理共享给其他设备
Windows版***带有Share over LAN功能,可以让一些不方便安装***客户端的设备一同“鸡犬升天”,如未越狱的iOS设备.但是 OS X 就没有这么幸运了,这时候你需要Privoxy助 ...
- 从 ie10浏览器下Symbol 未定义的问题 探索vue项目如何兼容ie低版本浏览器(ie9, ie10, ie 11 )
问题: vue项目在ie11下一片空白并报Symbol 未定义的错 原因: ie10浏览器解析不了es6的语法,需要我们使用babel(Babel是一种工具链,主要用于将ECMAScr ...
- 关于OCR,一些想法
OCR一般分为两种: 1,根据给定的字符特征集合,提取未知字符的特征进行匹配识别:(典型例子:GOCR) 2,不知道字符特征,但给出提取特征的规则,通过机器学习training来获取某个字符集的特征集 ...
- Linux命令之paste
介绍 cut用来从文本文件或标准输出中抽取数据列或者域,然后再用paste可以将这些数据粘贴起来形成相关文件.粘贴两个不同来源的数据时,首先需将其分类,并确保两个文件行数相同. paste将按行将不同 ...
- 非jsonp解决跨域问题
1.服务器端解决跨域问题:配置filter在filter中设置请求头import java.io.IOException; import javax.servlet.Filter;import jav ...
- grafana零散模块点记录(share,setting,datasourse)
一.Settings 1.General Details Name:当前doshboard名称 Description Tags:当前doshboard设置tag,输入完成是,点击“Enter”才能完 ...
- JavaScript数据结构与算法-散列练习
散列的实现 // 散列类 - 线性探测法 function HashTable () { this.table = new Array(137); this.values = []; this.sim ...