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

(博客園-番茄醬原創)java

在個人系統中,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。linux

由於老師須要咱們寫mapreduce程序,因此如今須要配置hadoop的eclipse插件。以前在windows下面安裝hadoop一直會有莫名其妙的問題,因此索性直接在linux下面裝了。Linux下面還更簡單一些。git

下面談談如何配置吧。github

其實此次配置,並非直接生成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.gzapache

編程

  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,而後進行一些配置windows

先選擇hadoop的安裝路徑app

而後點擊okeclipse

 

而後點擊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下面文件內容

相關文章
相關標籤/搜索