Hadoop MapReduce如何進行WordCount自主編譯運行
上次我們已經(jīng)搭建了Hadoop的偽分布式環(huán)境,并且運行了一下Hadoop自帶的例子–WordCount程序,展現(xiàn)良好。但是大多數(shù)時候還是得自己寫程序,編譯,打包,然后運行的,所以做一次自編譯打包運行的實驗。
編輯程序
在Eclipse或者NetBeans中編輯WordCount.java程序,用IDE的好處就是我們可以更方便的選擇各種依賴的jar包,并且它會幫我們編譯好,我們只需要去workspace中拿出class文件打包就好了,或者直接打包就行。而不用在命令行輸入很多依賴jar包去打包,這樣更加省事。
1.新建Java Project,名為WordCount,然后建立一個叫test的package,新建WordCount.java,編輯好。結(jié)構(gòu)如下:
2.這時候我們的workspace/WordCount/bin/test目錄下自動生成了編譯好的三個class文件。

3.將class文件打包。如下圖所示,在bin/test目錄下輸入
- $ jar cvf WordCount.jar test/
 
即可將class文件打包為WordCount.jar.
4.運行hdfs:
- $ cd /usr/local/hadoop
 - $ ./sbin/start-dfs.sh
 - $ jps //檢查是否啟動NameNode,DataNode等
 
5.往HDFS上的input文件夾中put一個文本文件或者xml文件,上篇文章有講。比如:
- $ hadoop fs -put /usr/local/hadoop/etc/hadoop/*.xml input
 
6.運行WordCount.jar
- $ cd ~/workspace/WordCount/bin //進入到WordCount.jar所在目錄
 - $ hadoop jar WordCount.jar test.WordCount input output
 - $ hadoop fs -cat output/part-r-00000 //查看輸出
 
7.關(guān)閉hdfs
- $ cd /usr/local/hadoop
 - $ ./sbin/stop-dfs.sh
 
下次運行時須將output目錄刪除。
到此,我們就編譯運行成功了。還是挺簡單的。畢竟WordCount是hadoop界的Helloworld啊。
以后我們編寫hadoop程序,只需要按這個過程編譯打包運行一下就可以了。
一個錯誤
之前沒有指定包,而是放在默認包內(nèi)的時候,運行
- hadoop jar WordCount.jar WordCount input output
 
會出現(xiàn):
- Exception in thread "main" java.lang.ClassNotFoundException: WordCount
 
的錯誤,后來將WordCount.java重新寫在一個package(test)中就不再有這個問題了。
即第三個參數(shù)一定要是入口類,比如程序?qū)儆诎黷est,那么第三個參數(shù)須是 test.WordCount 。
WordCount 代碼
下面的代碼下載自網(wǎng)上,我看他還寫了很多注釋,就直接拿來用了。
- package test;
 - 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類:實現(xiàn)了Mapper和Reducer接口的基類(其中的方法只是實現(xiàn)接口,而未作任何事情)
 - * Mapper接口:
 - * WritableComparable接口:實現(xiàn)WritableComparable的類可以相互比較。所有被用作key的類應(yīng)該實現(xiàn)此接口。
 - * Reporter 則可用于報告整個應(yīng)用的運行進度,本例中未使用。
 - *
 - */
 - public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
 - /**
 - * LongWritable, IntWritable, Text 均是 Hadoop 中實現(xiàn)的用于封裝 Java 數(shù)據(jù)類型的類,這些類實現(xiàn)了WritableComparable接口,
 - * 都能夠被串行化從而便于在分布式環(huán)境中進行數(shù)據(jù)交換,你可以將它們分別視為long,int,String 的替代品。
 - */
 - private final static IntWritable one = new IntWritable(1);
 - private Text word = new Text(); //Text 實現(xiàn)了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 {
 - /**
 - * 原始數(shù)據(jù):
 - * c++ java hello
 - world java hello
 - you me too
 - map階段,數(shù)據(jù)如下形式作為map的輸入值:key為偏移量
 - 0 c++ java hello
 - 16 world java hello
 - 34 you me too
 - */
 - 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();
 - /**
 - * reduce過程是對輸入數(shù)據(jù)解析形成如下格式數(shù)據(jù):
 - * (c++ [1])
 - * (java [1,1])
 - * (hello [1,1])
 - * (world [1])
 - * (you [1])
 - * (me [1])
 - * (you [1])
 - * 供接下來的實現(xiàn)的reduce程序分析數(shù)據(jù)數(shù)據(jù)
 - *
 - */
 - 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 {
 - /**
 - * JobConf:map/reduce的job配置類,向hadoop框架描述map-reduce執(zhí)行的工作
 - * 構(gòu)造方法:JobConf()、JobConf(Class exampleClass)、JobConf(Configuration conf)等
 - */
 - Configuration conf = new Configuration();
 - String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
 - //這里需要配置參數(shù)即輸入和輸出的HDFS的文件路徑
 - if (otherArgs.length != 2) {
 - System.err.println("Usage: wordcount <in> <out>");
 - System.exit(2);
 - }
 - Job job = new Job(conf, "word count"); // Job(Configuration conf, String jobName)
 - job.setJarByClass(WordCount.class);
 - job.setMapperClass(TokenizerMapper.class); // 為job設(shè)置Mapper類
 - job.setCombinerClass(IntSumReducer.class); // 為job設(shè)置Combiner類
 - job.setReducerClass(IntSumReducer.class); // 為job設(shè)置Reduce類
 - job.setOutputKeyClass(Text.class); // 設(shè)置輸出key的類型
 - job.setOutputValueClass(IntWritable.class); // 設(shè)置輸出value的類型
 - FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
 - FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
 - System.exit(job.waitForCompletion(true) ? 0 : 1);
 - }
 - }
 




















 
 
 




 
 
 
 