实现每个Map或Reduce任务只执行一次map或reduce方法

实现每个Map或Reduce任务只执行一次map或reduce方法

 

    默认情况下map和reduce方法是针对每一个<key,value>对执行一次,但是用户可以书写让map或reduce task只执行一次map或者reduce方法的应用程序。书写这样的程序之前,请先弄清MapReduce框架中map、reduce方法的运行方式。

    实际上只需要在用户程序的map类和reduce类中重载run方法,把循环遍历每个输入的<key,value>对放到map函数和reduce函数中处理就行了。

    运行:将代码打包成jar后,在集群中运行。

    hadoop-0.20.1下详细代码实例如下: 

    package org.apache.hadoop.examples; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapred.FileAlreadyExistsException; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.Mapper.Context; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser; /* * AUTHOR: zhankunlin 2010-8-16 */ public class WordCountZKL { public static class WordCountMapperZKL extends Mapper<Object, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Context context) throws IOException, InterruptedException { while (context.nextKeyValue()) { //循环拿出来处理 Object key = context.getCurrentKey(); Text value = (Text) context.getCurrentValue(); ///// StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } ///// } } /** * Expert users can override this method for more complete control over the * execution of the Mapper. * @param context * @throws IOException */ public void run(Context context) throws IOException, InterruptedException { setup(context); map(context); //修改此处 cleanup(context); } } public static class WordCountReducerZKL extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Context context) throws IOException, InterruptedException { while (context.nextKey()) { //把循环提取出来 Text key = context.getCurrentKey(); Iterable<IntWritable> values = context.getValues(); /////// int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); /////// } } /** * Advanced application writers can use the * {@link #run(org.apache.hadoop.mapreduce.Reducer.Context)} method to * control how the reduce task works. */ public void run(Context context) throws IOException, InterruptedException { setup(context); reduce(context); //修改这个地方 cleanup(context); } } @SuppressWarnings("deprecation") public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); /* * String[] otherArgs = new GenericOptionsParser(conf, * args).getRemainingArgs(); if (otherArgs.length != 2) { * System.err.println("Usage: wordcount <in> <out>"); System.exit(2); } */ String[] inputPars = { "wcinZKL", "wcoutZKL" }; String[] otherArgs = new GenericOptionsParser(conf, inputPars) .getRemainingArgs(); Path outputPaths = new Path(otherArgs[1]); FileSystem fs = FileSystem.get(conf); if (fs.exists(outputPaths)) { // please see the code of exists() method // throw new FileAlreadyExistsException("Output directory " + // outputPaths + " already exists"); FileStatus fsStatus = fs.getFileStatus(outputPaths); if (fsStatus.isDir()) // only test the methods of hdfs,but it is not necessary fs.delete(outputPaths, true); else fs.delete(outputPaths, false);// true is also ok System.out.println("Output directory /"" + outputPaths + "/" already exists" + ",firstly delete it"); } Job job = new Job(conf, "word count zkl"); job.setJarByClass(WordCountZKL.class); job.setMapperClass(WordCountMapperZKL.class); job.setCombinerClass(WordCountReducerZKL.class); job.setReducerClass(WordCountReducerZKL.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.out.println("job "+job.getJobName()+"("+job.getJobID()+")"+" finished? "+job.waitForCompletion(true)); } }

你可能感兴趣的:(mapreduce,exception,String,Path,任务)