NO.5 算法測試(詞條統計)

 1、安裝Eclipsejava

    下載Eclipse,解壓安裝,例如安裝到/usr/local,即/usr/local/eclipseapache

    4.3.1版本下載地址:http://pan.baidu.com/s/1eQkpRguapp

2、在eclipse上安裝hadoop插件
 
   一、下載hadoop插件(就是一個jar包文件:hadoop-eclipse-plugin-1.2.1.jar)
 
   二、把插件放到eclipse/plugins目錄下
 
   三、重啓eclipse,按以下步驟配置Hadoop installation directory。 

     若是插件安裝成功,打開Windows—Preferences後,在窗口左側會有Hadoop Map/Reduce選項,點擊此選項,在窗口右側設置Hadoop安裝路徑。框架

 

四、配置Map/Reduce Locationseclipse

     打開Windows—Open Perspective—Other函數

搜索「Map」
    選擇Map/Reduce,點擊OK

    在右下方看到以下圖所示oop

點擊Map/Reduce Location選項卡,點擊右邊小象圖標,打開Hadoop Location配置窗口:spa

    輸入Location Name,任意名稱便可.配置Map/Reduce Master和DFS Mastrer,Host和Port配置成與core-site.xml的設置一致便可。.net

點擊"Finish"按鈕,關閉窗口。插件

 點擊左側的DFSLocations—>myhadoop(上一步配置的location name),如能看到user,表示安裝成功

 

 若是以下圖所示表示安裝失敗,請檢查Hadoop是否啓動,以及eclipse配置是否正確。使用eclipse版本與jdk的版本對應,能夠多安裝幾個jdk,靈活切換調用。

 

3、新建WordCount項目

    File—>Project,選擇Map/Reduce Project,輸入項目名稱WordCount等。

    在WordCount項目裏新建class,名稱爲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>{                 
                                 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();
                                
                                String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
                                
                                 if (otherArgs.length != 2) {                      
                                                System. err.println("Usage: wordcount <in> <out>" );                                               
                                                System. exit(2);
                                }
                                
                                Job job = new Job(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(otherArgs[0]));
                                
                                FileOutputFormat. setOutputPath(job, new Path(otherArgs[1]));
                                
                                System. exit(job.waitForCompletion(true) ? 0 : 1);
                }
}
 
 
4、運行
     一、在HDFS上建立目錄input
        hadoop fs -mkdir input
或者在Eclipse中的使用快捷功能

 

二、拷貝本地Test1.txt 到HDFS的input裏

         hadoop fs -copyFromLocal /usr/root/Test1.txt input

三、點擊WordCount.java,右鍵,點擊Run As—>Run Configurations,配置運行參數,即輸入和輸出文件夾

hdfs://localhost:9000/user/root/input         hdfs://localhost:9000/user/root/output

點擊Run按鈕,運行程序。

四、運行完成後,查看運行結果

        方法1:
        hadoop fs -ls output
        能夠看到有兩個輸出結果,_SUCCESS和part-r-00000
        執行hadoop fs -cat output/*
 

        方法2:

        展開DFS Locations,以下圖所示,雙擊打開part-r00000查看結果

////////////////////////////////////////////////////////////////////////////

小結: Hadoop程序處理流程

     (1)將文件拆分爲splits,並由MapReduce框架自動完成分割,將每個split分割爲<key,value>對

     (2)每一對<key,value>調用一次map函數,處理後生產新的<key,value>對,由Context傳遞給reduce處理

     (3)Mapper對<key,value>對進行按key值進行排序,並執行Combine過程,將key值相同的value進行合併。最後獲得Mapper的最終輸出結果

     (4)reduce處理,處理後將新的<key,value>對輸出。
相關文章
相關標籤/搜索