Hadoop2.x以後沒有Eclipse插件工具,咱們就不能在Eclipse上調試代碼,咱們要把寫好的java代碼的MapReduce打包成jar而後在Linux上運行,因此這種不方便咱們調試代碼,因此咱們本身編譯一個Eclipse插件,方便咱們在咱們本地上調試,通過hadoop1.x的發展,編譯hadoop2.x版本的eclipse插件比以前簡單多了。接下來我 們開始編譯Hadoop-eclipse-plugin插件,並在Eclipse開發Hadoop。java
1) 安裝jdkgit
2) 配置環境變量github
JAVA_HOME、CLASSPATH、PATH等設置,這裏就很少介紹,網上不少資料apache
1).下載eclipse-jee-juno-SR2.rarapp
2).解壓到本地磁盤,如圖所示:eclipse
1)下載工具
http://ant.apache.org/bindownload.cgioop
apache-ant-1.9.4-bin.zip測試
2)解壓到一個盤,如圖所示:ui
3).環境變量的配置
新建ANT_HOME=E:\ant\apache-ant-1.9.4-bin\apache-ant-1.9.4
在PATH後面加;%ANT_HOME%\bin
4)cmd 測試一下是否配置正確
ant version 如圖所示:
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
解壓到本地磁盤,如圖所示:
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,如圖所示:
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的設置一致便可。
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目錄下,有一個統計文件,並查看結果,因此配置成功。