標籤:style blog http io ar color os sp java
準備環境
1 安裝好了Hadoop,之前安裝了Hadoop 2.5.0,安裝參考http://www.cnblogs.com/liuchangchun/p/4097286.html
2 安裝Eclipse,這個直接在其官網下載即可
安裝步驟
1 下載Eclipse外掛程式,我找的是Hadoop 2.2 的外掛程式,在Hadoop 2.5 下可以正常用,擷取外掛程式這裡有兩種方式
1.1 一是自己下載源碼自己編譯,過程如下
首先,下載eclipse-hadoop的外掛程式,網址是https://github.com/winghc/hadoop2x-eclipse-plugin,你可以點擊網頁右下方的Download ZIP下載。下載之後,解壓縮,。
然後,進入到 hadoop2x-eclipse-plugin-master/src/contrib/eclipse-plugin檔案夾裡面,執行命令
ant jar -Declipse.home=/usr/local/eclipse -Dhadoop.home=~/Downloads/hadoop-2.2.0 -Dversion=2.5.0
編譯順利通過,產生的外掛程式在hadoop2x-eclipse-plugin-master/build/contrib/eclipse-plugin目錄下。
1.2 或是直接下載編譯好的外掛程式,http://pan.baidu.com/s/1mgiHFok
2 將下載好的外掛程式複製到eclipse/plugins目錄下,需要重啟Eclipse
3 配置Hadoop installation directory
3.1 如果外掛程式安裝成功,開啟Windows—Preferences後,在視窗左側會有Hadoop Map/Reduce選項,點擊此選項,在視窗右側設定Hadoop安裝路徑。
3.2 配置Map/Reduce Locations:開啟Windows—Open Perspective—Other 選擇Map/Reduce,點擊OK
3.3 點擊Map/Reduce Location選項卡,點擊右邊小象表徵圖,開啟Hadoop Location配置視窗:輸入Location Name,任意名稱即可.配置Map/Reduce Master和DFS Mastrer,Host和Port配置成與core- site.xml的設定一致即可。如果沒有自己修改連接埠,那麼一個是9001,一個是9000
3.4 點擊左側的DFSLocations—>Location Name(上一步配置的location name),如能看到Hadoop下的檔案,那麼表示安裝成功。
4 測試MapReduce。Eclipse中,File—>Project,選擇Map/Reduce Project,輸入項目名稱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); }}
5 運行項目,先需要做些準備工作
5.1、在HDFS上建立目錄input
hadoop fs -mkdir input
5.2 、隨便拷貝本地README.txt到HDFS的input裡
hadoop fs -copyFromLocal /usr/local/hadoop/README.txt input
5.3、點擊WordCount.java,右鍵,點擊Run As—>Run Configurations,配置運行參數,即輸入和輸出檔案夾
hdfs://localhost:9000/user/hadoop/input hdfs://localhost:9000/user/hadoop/output
5.4 注意,輸入目錄output不要在Hadoop中建立,否則會報錯
6 查看結果,可以直接在DFS Locations重新整理下就會看到多個目錄,裡面就有結果
----------------------------------------------------------------------------------------------------------------------------------------
WordCount程式上面是寫在一個類裡面,規範一點是Map類,Reduce類,MapRedcueDriver分開建立,低耦合
1 建立Map/Reduce工程wordcount。
2 建立Mapper.java,選擇File——>New——>Mapper,輸入包名及類名。
3 建立Reduccer.java,選擇File——>New——>Reducer,輸入包名及類名。
4 建立Map/Reduce Driver,選擇File——>New——>MapReduce Driver,輸入包名及類名。
5 運行,同上面
Ubuntu 14.10 下Eclipse安裝Hadoop外掛程式