MapReduce八股文模板

package com.bruce.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.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;

public class moduleMapReduce extends Configured implements Tool {

	// step 1: Map Class
	/**
	 * Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>
	 * 
	 */
	//TODO update paragram
	public static class ModuleMapper extends
			Mapper<LongWritable, Text, Text, IntWritable> {

		@Override
		protected void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
		}
	}

	// step 2: Reduce Class
	/**
	 * Reducer<KEYIN, VALUEIN, KEYOUT, VALUEOUT>
	 * 
	 */
	//TODO
	public static class ModuleReducer extends
			Reducer<Text, IntWritable, Text, IntWritable> {

		@Override
		protected void reduce(Text key, Iterable<IntWritable> values,
				Context context) throws IOException, InterruptedException {
			// TODO Auto-generated method stub
		}
	}

	// step 3: Driver ,component job, implements Tool
	public int run(String[] args) throws Exception {
		// 1: get configration
		Configuration configuration = getConf();

		// 2: create Job
		Job job = Job.getInstance(configuration, this.getClass()
				.getSimpleName());
		// run jar
		job.setJarByClass(this.getClass());

		// 3: set job
		// input -> map -> reduce -> output
		// 3.1 input
		Path inPath = new Path(args[0]);
		FileInputFormat.addInputPath(job, inPath);

		// 3.2: map
		job.setMapperClass(ModuleMapper.class);
		//TODO update paragram
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);

		// 3.3: reduce
		job.setReducerClass(ModuleReducer.class);
		//TODO
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);

		// 3.4: output
		Path outPath = new Path(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 configration
		Configuration configuration = new Configuration();
//		int status = new moduleMapReduce().run(args);
		int status = ToolRunner.run(configuration, new moduleMapReduce(), args);
		System.exit(status);

	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章