自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Hadoop MapReduce如何進(jìn)行WordCount自主編譯運行

云計算 Hadoop
上次我們已經(jīng)搭建了Hadoop的偽分布式環(huán)境,并且運行了一下Hadoop自帶的例子–WordCount程序,展現(xiàn)良好。但是大多數(shù)時候還是得自己寫程序,編譯,打包,然后運行的,所以做一次自編譯打包運行的實驗。

上次我們已經(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目錄下輸入

  1. $ jar cvf WordCount.jar test/ 

即可將class文件打包為WordCount.jar.

 

4.運行hdfs:

  1. $ cd /usr/local/hadoop 
  2. $ ./sbin/start-dfs.sh 
  3. $ jps    //檢查是否啟動NameNode,DataNode等 

5.往HDFS上的input文件夾中put一個文本文件或者xml文件,上篇文章有講。比如:

  1. $ hadoop fs -put /usr/local/hadoop/etc/hadoop/*.xml input 

6.運行WordCount.jar

  1. $ cd ~/workspace/WordCount/bin   //進(jìn)入到WordCount.jar所在目錄 
  2. $ hadoop jar WordCount.jar test.WordCount input output 
  3. $ hadoop fs -cat output/part-r-00000   //查看輸出 

 

 

 

 

7.關(guān)閉hdfs

  1. $ cd /usr/local/hadoop 
  2. $ ./sbin/stop-dfs.sh 

下次運行時須將output目錄刪除。

到此,我們就編譯運行成功了。還是挺簡單的。畢竟WordCount是hadoop界的Helloworld啊。

以后我們編寫hadoop程序,只需要按這個過程編譯打包運行一下就可以了。

一個錯誤

之前沒有指定包,而是放在默認(rèn)包內(nèi)的時候,運行

  1. hadoop jar WordCount.jar WordCount input output 

會出現(xiàn):

  1. Exception in thread "main" java.lang.ClassNotFoundException: WordCount 

的錯誤,后來將WordCount.java重新寫在一個package(test)中就不再有這個問題了。

即第三個參數(shù)一定要是入口類,比如程序?qū)儆诎黷est,那么第三個參數(shù)須是 test.WordCount 。

WordCount 代碼

下面的代碼下載自網(wǎng)上,我看他還寫了很多注釋,就直接拿來用了。

  1. package test; 
  2.  
  3. import java.io.IOException; 
  4. import java.util.StringTokenizer; 
  5. import org.apache.hadoop.conf.Configuration; 
  6. import org.apache.hadoop.fs.Path; 
  7. import org.apache.hadoop.io.IntWritable; 
  8. import org.apache.hadoop.io.Text; 
  9. import org.apache.hadoop.mapred.JobConf; 
  10. import org.apache.hadoop.mapreduce.Job; 
  11. import org.apache.hadoop.mapreduce.Mapper; 
  12. import org.apache.hadoop.mapreduce.Reducer; 
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 
  15. import org.apache.hadoop.util.GenericOptionsParser; 
  16.  
  17. public class WordCount { 
  18.  
  19.  /**  
  20.      * MapReduceBase類:實現(xiàn)了Mapper和Reducer接口的基類(其中的方法只是實現(xiàn)接口,而未作任何事情)  
  21.      * Mapper接口:  
  22.      * WritableComparable接口:實現(xiàn)WritableComparable的類可以相互比較。所有被用作key的類應(yīng)該實現(xiàn)此接口。  
  23.      * Reporter 則可用于報告整個應(yīng)用的運行進(jìn)度,本例中未使用。   
  24.      *   
  25.      */   
  26.     public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> { 
  27.      
  28.   /**  
  29.        * LongWritable, IntWritable, Text 均是 Hadoop 中實現(xiàn)的用于封裝 Java 數(shù)據(jù)類型的類,這些類實現(xiàn)了WritableComparable接口,  
  30.        * 都能夠被串行化從而便于在分布式環(huán)境中進(jìn)行數(shù)據(jù)交換,你可以將它們分別視為long,int,String 的替代品。  
  31.        */  
  32.         private final static IntWritable one = new IntWritable(1); 
  33.         private Text word = new Text();      //Text 實現(xiàn)了BinaryComparable類可以作為key值 
  34.     
  35.     /**  
  36.      * Mapper接口中的map方法:  
  37.      * void map(K1 key, V1 value, OutputCollector<K2,V2> output, Reporter reporter)  
  38.      * 映射一個單個的輸入k/v對到一個中間的k/v對  
  39.      * 輸出對不需要和輸入對是相同的類型,輸入對可以映射到0個或多個輸出對。  
  40.      * OutputCollector接口:收集Mapper和Reducer輸出的<k,v>對。  
  41.      * OutputCollector接口的collect(k, v)方法:增加一個(k,v)對到output  
  42.      */   
  43.         public void map(Object key, Text value, Context context) throws IOException, InterruptedException { 
  44.          
  45.         /** 
  46.          * 原始數(shù)據(jù): 
  47.          * c++ java hello 
  48. world java hello 
  49. you me too 
  50. map階段,數(shù)據(jù)如下形式作為map的輸入值:key為偏移量 
  51. 0  c++ java hello 
  52.             16 world java hello 
  53.             34 you me too 
  54.  
  55.          */ 
  56.  
  57.             StringTokenizer itr = new StringTokenizer(value.toString());  //得到什么值 
  58.       
  59.             while (itr.hasMoreTokens()) { 
  60.                 word.set(itr.nextToken()); 
  61.                 context.write(word, one); 
  62.             } 
  63.         } 
  64.     } 
  65.    
  66.     public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { 
  67.         private IntWritable result = new IntWritable(); 
  68. /** 
  69.  * reduce過程是對輸入數(shù)據(jù)解析形成如下格式數(shù)據(jù): 
  70.  * (c++ [1]) 
  71.  * (java [1,1]) 
  72.  * (hello [1,1]) 
  73.  * (world [1]) 
  74.  * (you [1]) 
  75.  * (me [1]) 
  76.  * (you [1]) 
  77.  * 供接下來的實現(xiàn)的reduce程序分析數(shù)據(jù)數(shù)據(jù) 
  78.  *  
  79.  */ 
  80.         public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { 
  81.             int sum = 0
  82.             for (IntWritable val : values) { 
  83.                 sum += val.get(); 
  84.             } 
  85.             result.set(sum); 
  86.             context.write(key, result); 
  87.         } 
  88.     } 
  89.  
  90.     public static void main(String[] args) throws Exception { 
  91.    
  92.    /**  
  93.        * JobConf:map/reduce的job配置類,向hadoop框架描述map-reduce執(zhí)行的工作  
  94.        * 構(gòu)造方法:JobConf()、JobConf(Class exampleClass)、JobConf(Configuration conf)等  
  95.        */   
  96.         Configuration conf = new Configuration(); 
  97.         String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); 
  98.         //這里需要配置參數(shù)即輸入和輸出的HDFS的文件路徑 
  99.         if (otherArgs.length != 2) { 
  100.             System.err.println("Usage: wordcount <in> <out>"); 
  101.             System.exit(2); 
  102.         } 
  103.         Job job = new Job(conf, "word count");      // Job(Configuration conf, String jobName) 
  104.         job.setJarByClass(WordCount.class); 
  105.     job.setMapperClass(TokenizerMapper.class);  // 為job設(shè)置Mapper類  
  106.     job.setCombinerClass(IntSumReducer.class);  // 為job設(shè)置Combiner類   
  107.     job.setReducerClass(IntSumReducer.class);   // 為job設(shè)置Reduce類    
  108.     job.setOutputKeyClass(Text.class);          // 設(shè)置輸出key的類型 
  109.     job.setOutputValueClass(IntWritable.class); // 設(shè)置輸出value的類型 
  110.     FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  111.     FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); 
  112.     System.exit(job.waitForCompletion(true) ? 0 : 1); 
  113.     } 

 

 

 

 

 

 

責(zé)任編輯:趙寧寧 來源: Whatbeg's blog
相關(guān)推薦

2010-06-03 11:01:32

Hadoop安裝部署

2010-06-03 10:04:26

Hadoop安裝

2010-01-20 13:29:40

C++環(huán)境

2012-06-29 10:58:27

Hadoop集群

2010-02-05 13:44:36

Dalvik虛擬機(jī)

2023-09-27 15:34:48

數(shù)據(jù)編程

2010-08-05 09:46:45

FlexAIR文件打包

2010-06-03 14:42:47

Hadoop分布式集群

2017-08-04 10:47:20

2012-08-08 09:53:23

HadoopMapReduce

2021-04-24 23:06:47

JavaScript編程語言

2010-06-02 14:16:18

SVN版本控制

2023-03-24 16:18:08

微服務(wù)架構(gòu)

2010-02-03 13:55:51

Python 代碼

2010-07-21 14:17:07

Linux telne

2010-09-13 10:45:04

2011-07-28 14:07:30

2023-09-03 23:49:35

2010-07-22 10:58:49

batch Telne

2024-12-06 10:51:10

Hadoop大數(shù)據(jù)框架
點贊
收藏

51CTO技術(shù)棧公眾號