1.安裝Eclipsejava
1》下載Eclipselinux
能夠以多種方式下載Eclipse,下面介紹直接從eplise官網下載和從中國鏡像站點下載,下載把eclipse上傳到Hadoop環境中。apache
第一種方式從elipse官網下載:app
http://www.eclipse.org/downloads/?osType=linuxeclipse
咱們運行的環境爲CentOS 64位系統,須要選擇eclipse類型爲linux,而後點擊linux 64bit連接下載oop
會根據用戶所在地,推薦最佳的下載地址測試
在該頁面的下部分也能夠根據本身的狀況選擇合適的鏡像站點進行下載spa
第二種方式從鏡像站點直接下載elipse:.net
http://mirror.bit.edu.cn/eclipse/technology/epp/downloads/release/luna/R/插件
在鏡像站點選擇 eclipse-jee-luna-R-linux-gtk-x86_64.tar.gz進行下載
(http://mirror.bit.edu.cn/eclipse/technology/epp/downloads/release/luna/R/eclipse-jee-luna-R-linux-gtk-x86_64.tar.gz)
2》解壓elipse
在/home/hadoop/Downloads/目錄中,使用以下命令解壓elipse並移動到/usr/local目錄下:
cd /home/hadoop/Downloads
tar -zxvf eclipse-jee-luna-SR1-linux-gtk-x86_64.tar.gz
sudo mv eclipse /usr/local/
cd /usr/local
ls
登陸到虛擬機桌面,進入/usr/local/eclipse目錄,經過以下命令啓動eclipse:
cd /usr/local/eclipse
./eclipse
爲了方便操做,能夠在虛擬機的桌面上創建elipse的快捷操做
2.在Eclipse中安裝hadoop插件
Hadoop2.7.1插件下載:http://download.csdn.net/download/gaoyunbo007/9973198
一、將下載好的插件移動到eclipse安裝目錄下的plugins文件夾下。
二、從新啓動eclispe,配置hadoop安裝目錄和hdfs端口。
若是插件安裝成功,打開【Windows】—>【Preferences】後,在窗口左側會有Hadoop Map/Reduce選項,點擊此選項,在窗口右側設置hadoop安裝路徑,而後點擊【OK】。
打開【Windows】–>【Perspective】–>【Open perspective】–>【Other】,選擇【Map/Reduce】,點擊【OK】。
點擊【Map/Reduce Location】選項卡,點擊右邊小象圖標,打開Hadoop Location配置窗口:
輸入Location Name,任意名稱便可。配置Map/Reduce Master,Host和Port配置成與mapred-site.xml的設置一致和DFS Mastrer,Host和Port配置成與core-site.xml的設置一致,點擊【Finish】。
點擊左側的DFSLocations—>MyHadoop(上一步配置的location name),若是不報錯,表示安裝成功。
注意:這裏和Hadoop1.x不同,1.x版本這裏會有user文件夾,2.x之後沒有,若是你是新裝的hadoop,這裏顯示的文件數爲0,此時並非報錯。
3.測試插件是否配置成功
一、點擊【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); } } @SuppressWarnings("deprecation") 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); } }
二、在HDFS上建立目錄input
hadoop fs -mkdir /input
三、拷貝本地README.txt到HDFS的input裏
hadoop fs -copyFromLocal /home/hadoop/labc/hadoop/README.txt /input
四、點擊WordCount.java,右鍵,點擊【Run As】—>【Run Configurations】,配置運行參數,即輸入和輸出文件夾
hdfs://Master:9000/input hdfs://Master:9000/output
五、點擊【Run】,運行程序。
查看運行結果:
1> 在控制檯輸入:
hadoop fs -cat /output/part-r-00000
2>展開【DFS Locations】