package org.apache.hadoop.examples;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.mapred.JobConf;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 { /** * MapReduceBase類:實現了Mapper和Reducer介面的基類(其中的方法只是實現介面,而未作任何事情) * Mapper介面: * WritableComparable介面:實現WritableComparable的類可以相互比較。所有被用作key的類應該實現此介面。 * Reporter 則可用於報告整個應用的運行進度,本例中未使用。 * */ public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ /** * LongWritable, IntWritable, Text 均是 Hadoop 中實現的用於封裝 Java 資料類型的類,這些類實現了WritableComparable介面, * 都能夠被序列化從而便於在分布式環境中進行資料交換,你可以將它們分別視為long,int,String 的替代品。 */ private final static IntWritable one = new IntWritable(1); private Text word = new Text();//Text 實現了BinaryComparable類可以作為key值 /** * Mapper介面中的map方法: * void map(K1 key, V1 value, OutputCollector<K2,V2> output, Reporter reporter) * 映射一個單個的輸入k/v對到一個中間的k/v對 * 輸出對不需要和輸入對是相同的類型,輸入對可以映射到0個或多個輸出對。 * OutputCollector介面:收集Mapper和Reducer輸出的<k,v>對。 * OutputCollector介面的collect(k, v)方法:增加一個(k,v)對到output */ public void map(Object key, Text value, Context context) throws IOException, InterruptedException { /** * 未經處理資料: * c++ java helloworld java helloyou me toomap階段,資料如下形式作為map的輸入值:key為位移量0 c++ java hello 16 world java hello 34 you me too */ /** * 以下解析索引值對 * 解析後以索引值對格式形成輸出資料 * 格式如下:前者是鍵排好序的,後者數字是值 * c++ 1 * java 1 * hello 1 * world 1 * java 1 * hello 1 * you 1 * me 1 * too 1 * 這些資料作為reduce的輸出資料 */ StringTokenizer itr = new StringTokenizer(value.toString());//得到什麼值 System.out.println("value什麼東西 : "+value.toString()); System.out.println("key什麼東西 : "+key.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } } public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable();/** * reduce過程是對輸入資料解析形成如下格式資料: * (c++ [1]) * (java [1,1]) * (hello [1,1]) * (world [1]) * (you [1]) * (me [1]) * (you [1]) * 供接下來的實現的reduce程式分析資料資料 * */ public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; /** * 自己的實現的reduce方法分析輸入資料 * 形成資料格式如下並儲存 * c++1 *hello2 *java2 *me1 *too1 *world1 *you1 * */ for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception { /** * JobConf:map/reduce的job配置類,向hadoop架構描述map-reduce執行的工作 * 構造方法:JobConf()、JobConf(Class exampleClass)、JobConf(Configuration conf)等 */ Configuration conf = new Configuration(); String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); //這裡需要配置參數即輸入和輸出的HDFS的檔案路徑 if (otherArgs.length != 2) { System.err.println("Usage: wordcount <in> <out>"); System.exit(2); } // JobConf conf1 = new JobConf(WordCount.class); Job job = new Job(conf, "word count");//Job(Configuration conf, String jobName) 設定job名稱和 job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); //為job設定Mapper類 job.setCombinerClass(IntSumReducer.class); //為job設定Combiner類 job.setReducerClass(IntSumReducer.class); //為job設定Reduce類 job.setOutputKeyClass(Text.class); //設定輸出key的類型 job.setOutputValueClass(IntWritable.class);// 設定輸出value的類型 FileInputFormat.addInputPath(job, new Path(otherArgs[0])); //為map-reduce任務設定InputFormat實作類別 設定輸入路徑 FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//為map-reduce任務設定OutputFormat實作類別 設定輸出路徑 System.exit(job.waitForCompletion(true) ? 0 : 1); }}
參考學習:http://blog.csdn.net/anbo724/article/details/6757350
http://coderplay.iteye.com/blog/295097
http://blog.csdn.net/anbo724/article/details/6339222