wordcount源代碼詳解


package wordcount;
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.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.GenericOptionsParser;

public class wordcount {
        public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{   //繼承泛型類Mapper
               
private final static IntWritable one = new IntWritable(1);  //定義hadoop數據類型IntWritable實例one,並且賦值為1
                private Text word = new Text();                                    //定義hadoop數據類型Text實例word
 
               
public void map(Object key, Text value, Context context) throws IOException, InterruptedException { //實現map函數
                        StringTokenizer itr = new StringTokenizer(value.toString());//Java的字符串分解類,默認分隔符“空格”、“制表符(‘\t’)”、“換行符(‘\n’)”、“回車符(‘\r’)”

                        while (itr.hasMoreTokens()) {  //循環條件表示返回是否還有分隔符。
                                word.set(itr.nextToken());   // nextToken():返回從當前位置到下一個分隔符的字符串,word.set():Java數據類型與hadoop數據類型轉換
   
                             context.write(word, one);   //hadoop全局類context輸出函數write;
                        }
         }

}

public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {    //繼承泛型類Reducer
        
private IntWritable result = new IntWritable();   //實例化IntWritable
        
public void reduce(Text key, Iterable<IntWritable> values, Context context ) throws IOException, InterruptedException {  //實現reduce
                    int sum = 0;
                    for (IntWritable val : values)    //循環values,並記錄單詞個數
                               sum += val.get();
       
            result.set(sum);   //Java數據類型sum,轉換為hadoop數據類型result
                    context.write(key, result);   //輸出結果到hdfs
          }
}

public static void main(String[] args) throws Exception {
        
Configuration conf = new Configuration();   //實例化Configuration
/***********
GenericOptionsParser是hadoop框架中解析命令行參數的基本類。 getRemainingArgs();返回數組【一組路徑】
*********/
/**********
函數實現
public String[] getRemainingArgs() {
    return (commandLine == null) ? new String[]{} : commandLine.getArgs();
  }

/********
//總結上面:返回數組【一組路徑】
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

//如果只有一個路徑,則輸出需要有輸入路徑和輸出路徑
if (otherArgs.length < 2) {
   System.err.println("Usage: wordcount <in> [<in>...] <out>");
   System.exit(2);
}

Job job = Job.getInstance(conf, "word count");   //實例化job
job.setJarByClass(wordcount.class);   //為了能夠找到wordcount這個類
job.setMapperClass(TokenizerMapper.class);   //指定map類型
/********
指定CombinerClass類
這里很多人對CombinerClass不理解
************/
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);  //指定reduce類
job.setOutputKeyClass(Text.class); //rduce輸出Key的類型,是Text
job.setOutputValueClass(IntWritable.class);  // rduce輸出Value的類型

for (int i = 0; i < otherArgs.length - 1; ++i)
   FileInputFormat.addInputPath(job, new Path(otherArgs));  //添加輸入路徑

FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));   //添加輸出路徑
System.exit(job.waitForCompletion(true) ? 0 : 1);  //提交job
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM