模板类编写好后写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的更多相关文章

  1. Hadoop(十七)之MapReduce作业配置与Mapper和Reducer类

    前言 前面一篇博文写的是Combiner优化MapReduce执行,也就是使用Combiner在map端执行减少reduce端的计算量. 一.作业的默认配置 MapReduce程序的默认配置 1)概述 ...

  2. 024_MapReduce中的基类Mapper和基类Reducer

    内容提纲 1) MapReduce中的基类Mapper类,自定义Mapper类的父类. 2) MapReduce中的基类Reducer类,自定义Reducer类的父类. 1.Mapper类 API文档 ...

  3. [Hadoop in Action] 第4章 编写MapReduce基础程序

    基于hadoop的专利数据处理示例 MapReduce程序框架 用于计数统计的MapReduce基础程序 支持用脚本语言编写MapReduce程序的hadoop流式API 用于提升性能的Combine ...

  4. Hadoop:使用Mrjob框架编写MapReduce

    Mrjob简介 Mrjob是一个编写MapReduce任务的开源Python框架,它实际上对Hadoop Streaming的命令行进行了封装,因此接粗不到Hadoop的数据流命令行,使我们可以更轻松 ...

  5. 如何在maven项目里面编写mapreduce程序以及一个maven项目里面管理多个mapreduce程序

    我们平时创建普通的mapreduce项目,在遍代码当你需要导包使用一些工具类的时候, 你需要自己找到对应的架包,再导进项目里面其实这样做非常不方便,我建议我们还是用maven项目来得方便多了 话不多说 ...

  6. 整合使用持久层框架mybatis 使用SqlSessionTemplate模板类与使用映射接口 对比

    spring中整合使用mybatis的用法总结 一:在Spring配置Mybatis 第一步:将mybatis-spring类包添加 到项目的类库中 第二步:编写spring和持久层衔接的xml文件, ...

  7. c++模板类

    c++模板类 理解编译器的编译模板过程 如何组织编写模板程序 前言常遇到询问使用模板到底是否容易的问题,我的回答是:“模板的使用是容易的,但组织编写却不容易”.看看我们几乎每天都能遇到的模板类吧,如S ...

  8. C++ - 模板类模板成员函数(member function template)隐式处理(implicit)变化

    模板类模板成员函数(member function template)隐式处理(implicit)变化 本文地址: http://blog.csdn.net/caroline_wendy/articl ...

  9. 开涛spring3(7.2) - 对JDBC的支持 之 7.2 JDBC模板类

    7.2  JDBC模板类 7.2.1  概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDB ...

随机推荐

  1. VMware-vSphere-5.1--------群集、HA、DRS、FT

    VMware vSphere 5.1 高可用性       在本节中主要讲的是集群的一些功能和配置,相比5.0的设置,没有太大的变化.VMware vSphere为虚拟机提供虚拟化的基础架构,将现有的 ...

  2. opus 规范 与参数解析

    bytestream_put_buffer(&p, "OpusHead", 8); bytestream_put_byte(&p, 1); /* Version * ...

  3. 搞IT的技术人员为什么会如此苦逼

    http://www.cnblogs.com/springmvc-hibernate/archive/2012/05/10/2493733.html ————————————————————————— ...

  4. npm安装vue-cil出现错误

    这个错误有点尴尬..... 之前全局安装过cil,然后在全局安装出现了这个错误,各种手册看了半天也没有头绪,猛然想起来之前安装过,试下直接初始化项目试一下,果然成功了 然后在 npm install ...

  5. Hibernate无主键配置文件编写

    1.       环境:jdk1.4+hibernate2.0+weblogic8 一般情况下,我们建的表都会有主键,然后根据hibernate的配置文件编写条件 有一个主键key,剩下的是Prope ...

  6. important——》sql server 2000安装图解

    MS Sql Server 2000包含两个部分:服务器组件和客户端工具,其中服务器组建是以Windows服务的方式运行的,有四种服务分别是:MSSqlServer.Distributed Trans ...

  7. Eclipse 内置浏览器

    Web 浏览器 Eclipse 系统内部自带了浏览器,该浏览器可以通过点击 Window 菜单并选择 Show View > Other,在弹出来的对话框的搜索栏中输入 "browse ...

  8. echarts(3.0)的基本使用(标签式导入)

    function loadRainFallCharts(msg) { var obj = {}; obj.x = []; obj.y = []; obj.line = []; var accumula ...

  9. hdu 4705(树形DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4705 思路:反面考虑,用总的方案数减去A,B,C三点在同一路径上的方案数.于是我们可以确定中间点B,在 ...

  10. 关于swift 单元测试的补充

    最近小弟在自己学习研究swift , 习惯于写一边写单元测试一边写逻辑的我来说,在xcode环境没有单元测试,写起来就是有个不是实在的感觉. 至于怎么创建单元测试.怎么写,不是这个文章的主题,因为看了 ...