前2篇blog中測試hadoop代碼的時候都用到了這個jar,那麼很有必要去分析一下源碼。
分析源碼之前很有必要先寫一個wordcount,代碼如下
package mytest;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;public class WordCount { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.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(); public void reduce(Text key, Iterable<IntWritable> values, Context context ) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); }}
檢索相關源碼,發現需要2個jar,分別為hadoop-common-2.7.0.jar和hadoop-mapreduce-client-core-2.7.0.jar
使用myeclipse匯出為Runnable Jar後,執行
~/hadoop-2.7.0/bin/hadoop jar my.jar mytest.WordCount /user/hadoop/input /user/hadoop/output3
測試成功
因為有個“package mytest”所以執行的時候需要使用mytest.WorCount。
仔細回憶之前執行命令的時候並沒有加上類似mytest.這類的東西就能執行成功。我們去檢索源碼看看。執行。
find ~/ -name *hadoop-mapreduce-examples*
輸出內容為
/home/hadoop/hadoop-2.7.0/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.0.jar/home/hadoop/hadoop-2.7.0/share/hadoop/mapreduce/sources/hadoop-mapreduce-examples-2.7.0-sources.jar/home/hadoop/hadoop-2.7.0/share/hadoop/mapreduce/sources/hadoop-mapreduce-examples-2.7.0-test-sources.jar/home/hadoop/hadoop-2.7.0/share/doc/hadoop/hadoop-mapreduce-examples
解壓縮hadoop-mapreduce-examples-2.7.0-sources.jar後匯入myeclipse查看源碼。
檢索“grep”欄位,發現出現在ExampleDriver.java中,看樣這個檔案是這個jar的入口。
那麼Runnable Jar怎麼確定這個檔案的入口呢。解壓縮Runnable Jar後發現META-INF 中有如下的描述
Main-Class: org.apache.hadoop.examples.ExampleDriver
原來Runnable Jar是可以配置預設入口的。可以通過myeclipse匯出Jar的時候設定預設入口。
將ExampleDriver.java匯入自己的工程,修修改改後,測試。執行
~/hadoop-2.7.0/bin/hadoop jar my.jar wordcount /user/hadoop/input /user/hadoop/output4
很多東西具體看源碼比較詳細,以後有特殊的地方可以細細分析。
tip:分析log日誌可以發現。
map和reduce中的syso輸出到log日誌上。
Main中的syso輸出到螢幕上。