windows編譯hadoop 2.x Hadoop-eclipse-plugin插件

一.簡介

  Hadoop2.x以後沒有Eclipse插件工具,咱們就不能在Eclipse上調試代碼,咱們要把寫好的java代碼的MapReduce打包成jar而後在Linux上運行,因此這種不方便咱們調試代碼,因此咱們本身編譯一個Eclipse插件,方便咱們在咱們本地上調試,通過hadoop1.x的發展,編譯hadoop2.x版本的eclipse插件比以前簡單多了。接下來我 們開始編譯Hadoop-eclipse-plugin插件,並在Eclipse開發Hadoop。java

二.軟件安裝並配置

 

 1.JDK配置

    1) 安裝jdkgit

    2) 配置環境變量github

      JAVA_HOME、CLASSPATH、PATH等設置,這裏就很少介紹,網上不少資料apache

 2.Eclipse

   1).下載eclipse-jee-juno-SR2.rarapp

   2).解壓到本地磁盤,如圖所示:eclipse

     

3.Ant

  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   如圖所示:

 

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目錄下,有一個統計文件,並查看結果,因此配置成功。

相關文章
相關標籤/搜索