MapReduce 编程模板

来源:互联网 发布:mac如何彻底关机 编辑:程序博客网 时间:2024/05/19 18:10
*一. MapReduce编程模板类结构图

二. MapReduce模板代码骨架
package com.hadoop.senior.mapreduce;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.Partitioner;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 *  * @author  *  */public class ModuleMapReduce extends Configured implements Tool {// step 1: Map Class/** *  * public class Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> */// TODOpublic static class ModuleMapper extendsMapper<LongWritable, Text, Text, IntWritable> {@Overridepublic void setup(Context context) throws IOException,InterruptedException {// Nothing}@Overridepublic void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {// TODO}@Overridepublic void cleanup(Context context) throws IOException,InterruptedException {// Nothing}}// step 2: Reduce Class/** *  * public class Reducer<KEYIN,VALUEIN,KEYOUT,VALUEOUT> */// TODOpublic static class ModuleReducer extendsReducer<Text, IntWritable, Text, IntWritable> {@Overrideprotected void setup(Context context) throws IOException,InterruptedException {// Nothing}@Overridepublic void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {// TODO}@Overrideprotected void cleanup(Context context) throws IOException,InterruptedException {// Nothing}}// step 3: Driver ,component jobpublic int run(String[] args) throws Exception {// 1: get confifurationConfiguration configuration = getConf();// 2: create JobJob job = Job.getInstance(configuration, //this.getClass().getSimpleName());// run jarjob.setJarByClass(this.getClass());// 3: set job// input -> map -> reduce -> output// 3.1: inputPath inPath = new Path(args[0]);FileInputFormat.addInputPath(job, inPath);// 3.2: mapjob.setMapperClass(ModuleMapper.class);// TODOjob.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: reducejob.setReducerClass(ModuleReducer.class);// TODOjob.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);// set reduce number//job.setNumReduceTasks(2);// 3.4: outputPath outPath = new Path(args[1]);FileOutputFormat.setOutputPath(job, outPath);// 4: submit jobboolean isSuccess = job.waitForCompletion(true);return isSuccess ? 0 : 1;}// step 4: run programpublic static void main(String[] args) throws Exception {// 1: get confifurationConfiguration configuration = new Configuration();//set compress//configuration.set("mapreduce.map.output.compress", "true");//configuration.set("mapreduce.map.output.compress.codec", "org.apache.hadoop.io.compress.SnappyCodec");// int status = new WordCountMapReduce().run(args);int status = ToolRunner.run(configuration,//new ModuleMapReduce(),//args);System.exit(status);}}



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信不能语音了怎么办 微信不能发语音怎么办? 苹果6话筒声音小怎么办 微信视频杂音大怎么办 微信语音有杂音怎么办 苹果手机音频坏了怎么办 苹果手机送话器坏了怎么办 苹果7听筒声音大怎么办 苹果6听筒声音小怎么办 苹果7电话声音小怎么办 苹果7plus声音小怎么办 苹果7通话音量小怎么办 苹果4s声音小怎么办 苹果4s没有声音怎么办 手机送话筒坏了怎么办 安卓手机声音小怎么办 微信说话没声音怎么办 小米6话筒声音小怎么办 小米4通话声音小怎么办 电容麦有电流声怎么办 苹果6说话声音小怎么办 耳机漏音怎么办小招 win8耳机孔坏了怎么办 魔音耳机开胶了怎么办 苹果6plus声音小怎么办 苹果6铃声不响怎么办 苹果6静音键失灵怎么办 苹果7手机音量小怎么办 苹果4s声音太小怎么办 苹果6声音变小了怎么办 6s扬声器声音小怎么办 苹果手机6听筒声音小怎么办 苹果7听筒声音小怎么办 苹果6音量小了怎么办 苹果手机8声音小怎么办 苹果6plus音量小怎么办 苹果8通话声音小怎么办 苹果7听筒坏了怎么办 苹果6s听筒坏了怎么办 苹果6听筒坏了怎么办 苹果7听筒进油了怎么办