MapReduce编程模板
来源:互联网 发布:sql注入 跑管理员 编辑:程序博客网 时间:2024/06/12 21:49
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.IntWritable;
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.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
/**
* MapReuce
*
* @authorbeifeng
*
*/
public classModuleMapReduce extends Configuredimplements Tool {
// step 1: Map Class
/**
*
* public class Mapper<KEYIN,VALUEIN, KEYOUT, VALUEOUT>
*/
// TODO
public static class ModuleMapper extends
Mapper<LongWritable,Text, Text, IntWritable> {
@Override
public void setup(Context context) throws IOException,
InterruptedException{
// Nothing
}
@Override
public void map(LongWritable key,Text value, Context context)
throws IOException,InterruptedException {
// TODO
}
@Override
public void cleanup(Contextcontext) throwsIOException,
InterruptedException{
// Nothing
}
}
// step 2: Reduce Class
/**
*
* public classReducer<KEYIN,VALUEIN,KEYOUT,VALUEOUT>
*/
// TODO
public static class ModuleReducer extends
Reducer<Text,IntWritable, Text, IntWritable> {
@Override
protected void setup(Context context) throws IOException,
InterruptedException{
// Nothing
}
@Override
public void reduce(Text key,Iterable<IntWritable> values,
Contextcontext) throwsIOException, InterruptedException {
// TODO
}
@Override
protected void cleanup(Contextcontext) throwsIOException,
InterruptedException{
// Nothing
}
}
// step 3: Driver ,component job
public int run(String[] args) throws Exception {
// 1: get confifuration
Configurationconfiguration = getConf();
// 2: create Job
Jobjob = Job.getInstance(configuration, //
this.getClass().getSimpleName());
// run jar
job.setJarByClass(this.getClass());
// 3: set job
// input -> map -> reduce -> output
// 3.1: input
PathinPath = newPath(args[0]);
FileInputFormat.addInputPath(job,inPath);
// 3.2: map
job.setMapperClass(ModuleMapper.class);
// TODO
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//****************************Shuffle*********************************
// 1) partitioner
// job.setPartitionerClass(cls);
// 2) sort
// job.setSortComparatorClass(cls);
// 3) optional,combiner
// job.setCombinerClass(cls);
// 4) group
// job.setGroupingComparatorClass(cls);
//****************************Shuffle*********************************
// 3.3: reduce
job.setReducerClass(ModuleReducer.class);
// TODO
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// set reduce number
// job.setNumReduceTasks(2);
// 3.4: output
PathoutPath = newPath(args[1]);
FileOutputFormat.setOutputPath(job,outPath);
// 4: submit job
boolean isSuccess =job.waitForCompletion(true);
return isSuccess ? 0 : 1;
}
// step 4: run program
public static void main(String[] args)throws Exception {
// 1: get confifuration
Configurationconfiguration = newConfiguration();
int status = ToolRunner.run(configuration,//
new ModuleMapReduce(),//
args);
System.exit(status);
}
}
MapReduce编程格式相对固定,但是需要根据需求编写相关的map和reduce部分,来实现业务需求
- MapReduce 编程模板
- MapReduce编程模板
- hadoop 中的mapreduce编程模板
- 新版MapReduce的API编程简单模板
- MapReduce模板
- Mapreduce模板
- MapReduce编程
- MapReduce编程
- MapReduce编程
- MapReduce编程
- MapReduce编程
- MapReduce编程
- mapreduce编程
- MapReduce编程
- MapReduce模板程序
- MapReduce模板程序
- MapReduce模板2
- mapreduce 模板代码
- CCF-201604-1-折点计数
- python 字典处理的一些坑
- CentOS-7.0.中安装与配置Tomcat-7的方法
- 大数据分析的基本概念
- Perl语言中一些内置变量等,$、qw、cmp、eq、ne等
- MapReduce编程模板
- 单链表
- 设计模式笔记之简单工厂模式
- HTML5提供定义网页不同部分的语义元素
- Oracle Server Architecture/Oracle数据库服务器架构
- mysql优化
- unigui导出EXCEL使用NATIVEEXCEL
- jQueryday06(图片导航 滚动条位置 图片位置设置获取)
- 12:Integer to Roman(数字转为罗马数字)