在fedora20下配置hadoop2.5.1的eclipse插件


(博客園-番茄醬原創)

在我的系統中,hadoop-2.5.1的安裝路徑是/opt/lib64/hadoop-2.5.1下面,然后hadoop-2.2.0的路徑是/home/hadoop/下載/hadoop-2.2.0,我的eclipse的安裝路徑是/opt/programming/atd-bundle/eclipse。

因為老師需要我們寫mapreduce程序,所以現在需要配置hadoop的eclipse插件。之前在windows下面安裝hadoop一直會有莫名其妙的問題,所以索性直接在linux下面裝了。Linux下面還更簡單一些。

下面談談如何配置吧。

其實這次配置,並不是直接生成hadoop2.5.1的插件,而是生成hadoop2.2.0的插件,但是兼容hadoop-2.5.1。(這句話實際上指的是下面1步驟中的那個包是基於hadoop-2.2.0的開發的並且編譯時候依賴hadoop-2.2.0,所以我們需要下載hadoop-2.2.0)。因此,我們需要下載的東西有3個,一個是hadoop插件源文件,一個是ant(fedora20在線安裝),一個是額外的hadoop-2.2.0.tar.gz

  1. 下載hadoop2x-eclipse-plugin-master.zip,這是插件源文件,需要用ant編譯。下載地址是:https://codeload.github.com/jaradgreen/hadoop2x-eclipse-plugin/zip/master
  2. 然后安裝在線安裝ant編譯工具,在終端輸入:yum install ant,一路選擇yes或y安裝。
  3. 將下載的hadoop2x-eclipse-plugin-master.zip解壓,cd到下載的文件目錄,終端輸入:unzip hadoop2x-eclipse-plugin-master.zip,然后在當前目錄下就會多出一個文件夾:hadoop2x-eclipse-plugin-master
  4. 然后cd hadoop2x-eclipse-plugin-master/src/contrib/eclipse-plugin進入解壓的文件夾/hadoop2x-eclipse-plugin-master/src/contrib/eclipse-plugin目錄下,修改build.xml。
  5. 修改編譯配置文件build.xml.輸入命令vi build.xml
  6. 在build.xml文件中project標簽后面第三行,添加 <property name="eclipse.home" location="/opt/programming/adt-bundle/eclipse"/><!---這個是用來指出eclipse的安裝目錄->
  7. <property name="hadoop.home" location="/home/hadoop/下載/hadoop-2.2.0"/><!--此處需要hadoop2.2.0的發行包,編譯依賴此路徑(不是源包,也不是你安裝的hadoop-2.5.1的路徑,別弄混了)-->
  8. <property name="version" value="2.5.1"/> <!--(注意:自己根據自己的hadoop2.2.0和eclipse的路徑情況,更改上述的安裝位置)-->
  9. 然后進行ant編譯, 運行ant命令, ant jar -D eclipse.home=/opt/programming/adt-bundle/eclipse -D hadoop.home=/home/hadoop/下載/hadoop-2.2.0 -D version=2.5.1
  10. 然后ant就會編譯生成一個jar文件,在hadoop2x-eclipse-plugin-master/build/contrib/eclipse-plugin 下面,名為hadoop-eclipse-plugin-2.5.1.jar 然后將其拷到eclipse安裝路徑下的plugin文件夾下面,我的是/opt/programming/adt-bundle/eclipse/plugins 這個命令是mv /home/hadoop/下載/hadoop2x-eclipse-plugin-master/build/contrib/eclipse-plugin/hadoop-eclipse-plugin-2.5.1.jar /opt/programming/adt-bundle/eclipse/plugins/根據自己的情況進行更改啦
  11. 重啟eclipse,一路點擊windows->show view->other->mapreduce tools就可以選擇hadoop視圖了,然后就可以進行相應的編程了。
  12. 把剛剛的hadoop-2.2.0刪掉,他已經光榮的完成使命啦

打開eclipse,然后進行一些配置

先選擇hadoop的安裝路徑

然后點擊ok

 

然后點擊hadoop location,然后新建一個location

右擊鼠標新建一個location

到這邊,hadoop的eclipse就配置完畢了。如果你的hadoop的是開啟的狀態下,在eclipse中便可以直接操作dfs了

 

對了,如果你要跑wordcount程序,你需要在hadoop的src包中找到WordCount.java文件,

該目錄下面有好多例子,目錄是hadoop-2.5.1-src/hadoop-mapreduce-project/hadoop-mapreduce-examples/src/main/java/org/apache/hadoop/examples

附上其中除了命名空間的包名的代碼

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> [<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);
    for (int i = 0; i < otherArgs.length - 1; ++i) {
      FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
    }
    FileOutputFormat.setOutputPath(job,
      new Path(otherArgs[otherArgs.length - 1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

 

運行之前,需要配置run的參數:hdfs://localhost:9000/input hdfs://localhost:9000/output。然后再run as-> run on hadoop(要先把hadoop開啟)

在input文件夾下面放置2個文件,比如file1.txt,file2.txt,然后運行過后程序會新建一個output文件夾,里面會包含結果

file1

file2.txt

output下面文件內容

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM