標籤:eclipse外掛程式 hadoop
一.簡介
Hadoop2.x之後沒有Eclipse外掛程式工具,我們就不能在Eclipse上調試代碼,我們要把寫好的java代碼的MapReduce打包成jar然後在Linux上運行,所以這種不方便我們調試代碼,所以我們自己編譯一個Eclipse外掛程式,方便我們在我們本地上調試,經過hadoop1.x的發展,編譯hadoop2.x版本的eclipse外掛程式比之前簡單多了。接下來我 們開始編譯Hadoop-eclipse-plugin外掛程式,並在Eclipse開發Hadoop。
二.軟體安裝並配置
1.JDK配置
1) 安裝jdk
2) 配置環境變數
JAVA_HOME、CLASSPATH、PATH等設定,這裡就不多介紹,網上很多資料
2.Eclipse
1).下載eclipse-jee-juno-SR2.rar
2).解壓到本地磁碟,:
3.Ant
1)下載
http://ant.apache.org/bindownload.cgi
apache-ant-1.9.4-bin.zip
2)解壓到一個盤,:
3).環境變數的配置
建立ANT_HOME=E:\ant\apache-ant-1.9.4-bin\apache-ant-1.9.4
在PATH後面加;%ANT_HOME%\bin
4)cmd 測試一下是否配置正確
ant version :
4.Hadoop
1).下載hadoop包
hadoop-2.6.0.tar.gz
解壓到本地磁碟,:
下載hadoop2x-eclipse-plugin原始碼
1)目前hadoop2的eclipse-plugins原始碼由github脫管,是https://github.com/winghc/hadoop2x-eclipse-plugin,然後在右側的Download ZIP連接點擊下載,:
2)下載hadoop2x-eclipse-plugin-master.zip
解壓到本地磁碟,:
三.編譯hadoop-eclipse-plugin外掛程式
1.hadoop2x-eclipse-plugin-master解壓在E:盤開啟命令列cmd,切換到E:\hadoop\hadoop2x-eclipse-plugin-master\src\contrib\eclipse-plugin 目錄,:
2.執行ant jar
antjar -Dversion=2.6.0 -Declipse.home=F:\tool\eclipse-jee-juno-SR2\eclipse-jee-juno-SR2 -Dhadoop.home=E:\hadoop\hadoop-2.6.0\hadoop-2.6.0,:
3.編譯成功產生的hadoop-eclipse-plugin-2.6.0.jar在E:\hadoop\hadoop2x-eclipse-plugin-master\build\contrib\eclipse-plugin路徑下,:
四.Eclipse配置hadoop-eclipse-plugin 外掛程式 1.把hadoop-eclipse-plugin-2.6.0.jar拷貝到F:\tool\eclipse-jee-juno-SR2\eclipse-jee-juno-SR2\plugins目錄下,重啟一下Eclipse,然後可以看到DFS Locations,:
2.開啟Window-->Preferens,可以看到Hadoop Map/Reduc選項,然後點擊,然後添加hadoop-2.6.0進來,:
3.配置Map/ReduceLocations
1)點擊Window-->Show View -->MapReduce Tools 點擊Map/ReduceLocation
2)點擊Map/ReduceLocation選項卡,點擊右邊小象表徵圖,開啟Hadoop Location配置視窗: 輸入Location Name,任意名稱即可.配置Map/Reduce Master和DFS Mastrer,Host和Port配置成hdfs-site.xml與core-site.xml的設定一致即可。
4.查看是否串連成功
五.運行建立WordCount 項目並運行
1.右擊New->Map/Reduce Project
2.建立WordCount.java
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;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(); Job job = Job.getInstance(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(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); }}3.在hdfs輸入目錄建立需要統計的文本
1)沒有輸入輸出目錄卡,先在hdfs上建個檔案夾
#bin/hdfs dfs -mkdir –p /user/root/input
#bin/hdfs dfs -mkdir -p /user/root/output
2).把要統計的文本上傳到hdfs的輸入目錄下
# bin/hdfs dfs -put/usr/local/hadoop/hadoop-2.6.0/test/* /user/root/input //把tes/file01檔案上傳到hdfs的/user/root/input中
3).查看
#bin/hdfs dfs -cat /user/root/input/file01
4.點擊WordCount.java右擊-->Run As-->Run COnfigurations 設定輸入和輸出目錄路徑,:
5.點擊WordCount.java右擊-->Run As-->Run on Hadoop
然後到output/count目錄下,有一個統計檔案,並查看結果,所以配置成功。
五.注意的地方
我們在這篇介了,Eclipse串連Linux虛擬機器上Hadoop並在Eclipse開發Hadoop的一些問題,解決Exception: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z 等一系列問題
windows編譯hadoop 2.x Hadoop-eclipse-plugin外掛程式