大數據之Hadoop學習(十一) 在ubuntu的eclipse安裝MapReduce,以及運行WordCount程序並打包成.Jar文件在終端執行(超級詳細)

一、安裝 Hadoop-Eclipse-Plugin

要在 Eclipse 上編譯和運行 MapReduce 程序,需要安裝 hadoop-eclipse-plugin,可下載 Github 上的 hadoop2x-eclipse-plugin(備用下載地址:http://pan.baidu.com/s/1i4ikIoP)。

將下載的解壓

unzip -qo ~/下載/hadoop2x-eclipse-plugin-master.zip -d ~/下載    # 解壓到 ~/下載 中

在這裏插入圖片描述

sudo cp ~/下載/hadoop2x-eclipse-plugin-master/release/hadoop-eclipse-plugin-2.6.0.jar /usr/lib/eclipse/plugins/    # 複製到 eclipse 安裝目錄的 plugins 目錄下

如圖確實複製成功,能在改改文件夾下找到
在這裏插入圖片描述

/usr/lib/eclipse/eclipse -clean    # 添加插件後需要用這種方式使插件生效

二、配置 Hadoop-Eclipse-Plugin

  1. 先開啓Hadoop
cd /usr/local/hadoop/
./sbin/start-dfs.sh

在這裏插入圖片描述
2. 第一步:選擇 Window 菜單下的 Preference。
在這裏插入圖片描述

此時會彈出一個窗體,窗體的左側會多出 Hadoop Map/Reduce 選項,點擊此選項,選擇 Hadoop 的安裝目錄(如/usr/local/hadoop,Ubuntu不好選擇目錄,直接輸入就行)。
在這裏插入圖片描述

  1. 第二步:切換 Map/Reduce 開發視圖,選擇 Window 菜單下選擇 Open Perspective -> Other(CentOS 是 Window -> Perspective -> Open Perspective -> Other),彈出一個窗體,從中選擇 Map/Reduce 選項即可進行切換。
    在這裏插入圖片描述

  2. 第三步:建立與 Hadoop 集羣的連接,點擊 Eclipse軟件右下角的 Map/Reduce Locations 面板,在面板中單擊右鍵,選擇 New Hadoop Location。
    在這裏插入圖片描述
    在彈出來的 General 選項面板中,General 的設置要與 Hadoop 的配置一致。一般兩個 Host 值是一樣的,如果是僞分佈式,填寫 localhost 即可,另外我使用的Hadoop僞分佈式配置,設置 fs.defaultFS 爲 hdfs://localhost:9000,則 DFS Master 的 Port 要改爲 9000。Map/Reduce(V2) Master 的 Port 用默認的即可,Location Name 隨意填寫。

最後的設置如下圖所示:
在這裏插入圖片描述

三、在 Eclipse 中操作 HDFS 中的文件

配置好後,點擊左側 Project Explorer 中的 MapReduce Location (點擊三角形展開)就能直接查看 HDFS 中的文件列表了(HDFS 中要有文件,如下圖是 WordCount 的輸出結果),雙擊可以查看內容,右鍵點擊可以上傳、下載、刪除 HDFS 中的文件,無需再通過繁瑣的 hdfs dfs -ls 等命令進行操作了。

在這裏插入圖片描述

四、在 Eclipse 中創建 MapReduce 項目

點擊 File 菜單,選擇 New -> Project…:
在這裏插入圖片描述
選擇 Map/Reduce Project,點擊 Next。
在這裏插入圖片描述
填寫 Project name 爲 WordCount 即可,點擊 Finish 就創建好了項目。
在這裏插入圖片描述
右鍵點擊剛創建的 WordCount 項目,選擇 New -> Class,然後填寫兩個地方:在 Package 處填寫 org.apache.hadoop.examples;在 Name 處填寫 WordCount。
在這裏插入圖片描述
創建 Class 完成後,在 Project 的 src 中就能看到 WordCount.java 這個文件。將如下 WordCount 的代碼複製到該文件中。

package org.apache.hadoop.examples;
 
import java.io.IOException;
import java.util.Iterator;
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 WordCount() {
    }
 
    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 = Job.getInstance(conf, "word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(WordCount.TokenizerMapper.class);
        job.setCombinerClass(WordCount.IntSumReducer.class);
        job.setReducerClass(WordCount.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);
    }
 
    public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable result = new IntWritable();
 
        public IntSumReducer() {
        }
 
        public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int sum = 0;
 
            IntWritable val;
            for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {
                val = (IntWritable)i$.next();
            }
 
            this.result.set(sum);
            context.write(key, this.result);
        }
    }
 
    public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
        private static final IntWritable one = new IntWritable(1);
        private Text word = new Text();
 
        public TokenizerMapper() {
        }
 
        public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
 
            while(itr.hasMoreTokens()) {
                this.word.set(itr.nextToken());
                context.write(this.word, one);
            }
 
        }
    }
}

五、通過 Eclipse 運行 MapReduce

在運行 MapReduce 程序前,還需要執行一項重要操作(也就是上面提到的通過複製配置文件解決參數設置問題):將 /usr/local/hadoop/etc/hadoop 中將有修改過的配置文件(如僞分佈式需要 core-site.xml 和 hdfs-site.xml),以及 log4j.properties 複製到 WordCount 項目下的 src 文件夾(~/workspace/WordCount/src)中:
如圖:/usr/local/hadoop/workspace/WordCount/src(本人的路徑)
在這裏插入圖片描述

cp /usr/local/hadoop/etc/hadoop/core-site.xml /usr/local/hadoop/workspace/WordCount/src

cp /usr/local/hadoop/etc/hadoop/hdfs-site.xml /usr/local/hadoop/workspace/WordCount/src
cp /usr/local/hadoop/etc/hadoop/log4j.properties /usr/local/hadoop/workspace/WordCount/src

運行結果:

在這裏插入圖片描述

複製完成後,務必右鍵點擊 WordCount 選擇 refresh 進行刷新(不會自動刷新,需要手動刷新),可以看到文件結構如下所示:

在這裏插入圖片描述

通過Eclipse設定一下運行參數:

右鍵點擊剛創建的 WordCount.java,選擇 Run As -> Run Configurations,在此處可以設置運行時的相關參數(如果 Java Application 下面沒有 WordCount,那麼需要先雙擊 Java Application)。切換到 “Arguments” 欄,在 Program arguments 處填寫 “input output” 就可以了。
在這裏插入圖片描述

運行後,會報錯。

在這裏插入圖片描述

解決辦法
:小編在此鏈接中有具體解答

運行結果

在這裏插入圖片描述

原本的文件如下:
在這裏插入圖片描述
運行程序右鍵點擊Refresh,刷新哈
在這裏插入圖片描述
就會發現多了已經生成的output文件
在這裏插入圖片描述

六、將WordCount程序並打包成.Jar文件在終端執行

打開~/.bashrc
sudo gedit ~/.bashrc
我們將 Hadoop 的 classhpath 信息添加到 CLASSPATH 變量中,在 ~/.bashrc 中增加如下幾行:
export HADOOP_HOME=/usr/local/hadoop
export CLASSPATH=$($HADOOP_HOME/bin/hadoop classpath):$CLASSPATH

在這裏插入圖片描述

執行 source ~/.bashrc 使變量生效,
source ~/.bashrc
接着就可以通過 javac 命令編譯 WordCount.java 了
javac WordCount.java

在這裏插入圖片描述注意 :WordCount.java代碼的存放路徑,我的是/usr/local/hadoop/workspace/WordCount/src/org/apache/hadoop/examples

接着把 .class 文件打包成 jar,才能在 Hadoop 中運行:
jar -cvf WordCount.jar ./WordCount*.class

在這裏插入圖片描述
在這裏插入圖片描述

/usr/local/hadoop/bin/hadoop jar WordCount.jar org/apache/ha
doop/examples/WordCount hdfs://localhost:9000/user/hadoop/input hdfs://localhost:9000/user/hadoop/output1
由於我的hdfs:裏面已經存在output,所以將生成的命名爲output1

運行結果
在這裏插入圖片描述

 在用 -cat命令查看生成的output1中part-r-00000的內容
./bin/hdfs dfs -cat /user/hadoop/output1/part-r-00000

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章