本地Eclipse開發連線遠端阿里雲Hadoop

weixin_34377065發表於2017-03-03

本文主要介紹本地Eclipse開發連線遠端阿里雲Hadoop的環境搭建
首先需要在遠端伺服器部署好Hadoop執行環境
可以參考Hadoop偽分散式環境搭建
遠端Hadoop環境建立好以後,接下來開始本地Eclipse環境的搭建

一,Eclipse下載和Hadoop外掛下載

1,Eclipse官網下載Eclipse

2,接下來解決對應版本的Hadoop外掛
示例中Hadoop的環境是2.7.3,這裡也需要網上下載hadoop-eclipse-plugin-2.7.3.jar

3,將下載好的外掛hadoop-eclipse-plugin-2.7.3.jar放在eclipse/dropins中,然後重啟Eclipse

二,外掛配置

1,Eclipse重啟後將會出現紅圈所示的部分,這說明外掛加


1040484-7d0e5768cde41f5a.png
9B9E2508-5B89-4C3F-AEAF-7ACE3E42AC45.png

2,
選擇File->New->Project->Map/Reduce Project
建立一個WordCount工程

3,開啟Eclipse的Preferences介面
選擇Hadoop Map/Reduce選項
把Hadoop的安裝目錄選擇進去.
這裡可能會有一個疑問, Hadoop是在遠端阿里雲上安裝的,這個目錄怎麼選擇?
其實是把遠端hadoop的執行程式在本地copy一份,然後解壓,選擇的是本地的

1040484-268fcadfb8113887.png
5FD251A4-D3B3-46F5-8A9B-BE6D3A42A504.png

4,設定Hadoop Tool
點選Window-->Show View -->MapReduce Tools 點選 Map/ReduceLocation
彈出如下介面,然後進


1040484-61aa150959fbde5a.png
A4DC31C6-B60F-4A0F-95FB-71FB03A0B238.png

設定成功後,會出現如下介面

1040484-fb7a5dd6eafd849d.png
CB2244E9-A6F6-430B-A3EA-4AEFB1FB3F0D.png

5,設定阿里雲
1,修改 hadoop/etc/hadoop/hdfs-site.xml

<configuration>
        <property>
                <name>dfs.replication</name>
                <value>1</value>
        </property>

        <property>
                <name>dfs.permissions</name>
                <value>false</value>
        </property>
</configuration>

2,修改hadoop/etc/hadoop/core-site.xml

<configuration>
        <property>
                <name>fs.defaultFS</name>
                <value>hdfs://120.27.4.193:9000</value>
        </property>
</configuration>

三,建立Demo測試

1,新建WordCount工程,新增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;
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> <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);
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

2,在工程下面新增一個輸入檔案,就是程式的輸入資料Input

1040484-2389a8fa5f71f627.png
EFB33C8A-080B-424D-9F4A-985692609A2F.png

3,選中WordCount.java右鍵->Run As -> Run as configure
執行輸入檔名字就是剛才的Input檔案,和輸出目錄名字Out(自動生成),然後點選右下角的run執行

1040484-23103ef15202eb76.png
8ECF399A-BA49-4FDE-A44D-0129406FD7CE.png

4,執行完畢後,選中WordCount工程,然後Refresh
執行結果目錄就出來了,裡面有結果檔案

1040484-250b69282cd7cb39.png
9D156AD1-4454-4B6F-8FCF-68137310FA90.png

至此,本地Eclipse開發連線遠端阿里雲Hadoop結束

相關文章