MapReduce自带wordcount的实现

xiaoxiao2021-02-28  68

package com.bruce.mapreduce; import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; 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; public class WordCount { // step 1: Map Class /** * Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> * */ public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private Text mapOutputKey = new Text(); private final static IntWritable mapOutputValue = new IntWritable(1); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // TODO Auto-generated method stub //line value String lineValue = value.toString(); //split StringTokenizer stringTokenizer = new StringTokenizer(lineValue); //iterator while(stringTokenizer.hasMoreElements()){ //get value String wordValue = stringTokenizer.nextToken(); //set value mapOutputKey.set(wordValue); //output context.write(mapOutputKey, mapOutputValue); } } } // step 2: Reduce Class /** * Reducer<KEYIN, VALUEIN, KEYOUT, VALUEOUT> * */ public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable outputValue = new IntWritable(); @Override protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { // TODO Auto-generated method stub //sum tmp int sum = 0; //iterator for(IntWritable value: values){ //total sum += value.get(); } //set value outputValue.set(sum); //output context.write(key, outputValue); } } // step 3: Driver ,component job public int run(String[] args) throws Exception { // 1: get configration Configuration configuration = new Configuration(); // 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(WordCountMapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); // 3.3: reduce job.setReducerClass(WordCountReducer.class); 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 { int status = new WordCount().run(args); System.exit(status); } }
转载请注明原文地址: https://www.6miu.com/read-64554.html

最新回复(0)