1. 程式人生 > >windows10+eclipse neon+hadoop2.6.4(偽分散式)遠端連線虛擬機器環境搭建

windows10+eclipse neon+hadoop2.6.4(偽分散式)遠端連線虛擬機器環境搭建

0.需要用到的工具

jdk(我的是1.8)

hadoop-eclipse-plugin-2.6.4.jar(這裡我提供已編譯好的包 下載地址,若是其他版本可自行搜尋或用ant和hadoop原始碼自行編譯)

eclipse(我的版本是neon)

hadoop-2.6.4.tar.gz

hadoop.dll 和 winutil.exe(提供下載:下載地址

1.將hadoop-2.6.4.tar.gz解壓

2.Windows下的環境配置

配置HADOOP_HOME環境變數

新增HADOOP_HOME 路徑為你的hadoop目錄

編輯PATH 新增%HADOOP%\bin

防止專案執行時報錯,將hadoop.dll和winutils.exe(上面有下載連結)拷貝到hadoop中的bin目錄下

然後將hadoop.dll 拷貝到 C:\windows\system32下

3.更改hdfs-site.xml檔案,改成以下內容,若沒有則新增

<property> 
<name>dfs.permissions</name> 
<value>false</value> 
</property> 
目的是為了防止windows連線Hadoop伺服器時被拒絕報錯:org.apache.hadoop.security.AccessControlException: Permission denied: 
修改後重啟Hadoop

4.將hadoop-eclipse-plugin-2.6.4.jar 放到eclipse的plugins目錄中

5.開啟eclipse,windows》 preference 找到Hadoop Map/Reduce 設定你的hadoop目錄


6.顯示hadoop連線配置介面,windows》show view》other 找到Hadoop Map/Reduce


   在下方會顯示hadoop map/reduce 視窗

右鍵點選空白處,選擇New Hadoop Location 彈出此視窗


Location name:隨便填

Host:都填你的虛擬機器ip地址

User name:本地的windows使用者名稱稱(須修改你本地windows賬戶名稱為你的hadoop使用者名稱稱或者 在hadoop叢集下新建一個與windows賬戶名相同的賬戶

):

7.配置好後點擊 Finish,點選專案管理器上的hadoop伺服器名旁邊的小三角展開目錄,若成功連線則會顯示目錄


8.執行wordcount例項

 建立一個map/reduce project,新建專案new》file》other》map/reduce project 

建立類 org.apache.hadoop.examples.WordCount

     在WordCount.java,寫入以下程式碼

package 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);
  }
}

右鍵點選WordCount.java 選擇Run as》Run configuration 選擇Arguments

新增以下內容

hdfs://你的虛擬機器ip:9000/input hdfs://你的虛擬機器ip:9000/output

input是你的輸入目錄 output是你的輸出目錄,可自行更改

之後點選Run,程式會開始執行。



9.執行完成後,右鍵點選專案資源管理器上的hadoop伺服器,點選refresh,即可看到輸出資料夾,part-r-00000 就是輸出結果