Eclipes實現Mapreduce的配置(配圖解與WordCount案例)

Eclipes實現Mapreduce的配置(虛擬機中)

1.插件準備

我們需要下載 hadoop-eclipse-plugin-2.7.1.jar插件,插件已經上傳到羣文件中

鏈接: https://pan.baidu.com/s/19Zr6zkvWXK_u8ZIm-F-aBw 
提取碼: v9xx

如果你的文件下載在了windows中,點擊下方的鏈接完成文件的上傳

windows上傳文件到虛擬機的幾種方法

如果我們的文件在下載目錄,我們可以通過下面的命令檢查一下是否成功上傳

ls | grep hadoop-e

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-NEDU2KQw-1587003916650)(C:\Users\Tong\AppData\Roaming\Typora\typora-user-images\image-20200416100911040.png)]
接下來的操作假設我們已經把文件上傳到了虛擬機下載目錄中

2.Eclipes配置

接下來我們進入虛擬機中進行操作

1)啓動hadoop

start-dfs.sh # 啓動hadoop
jps	# 查看啓動狀態

在這裏插入圖片描述

2)複製插件到eclipes\plugins 目錄下

可以通過whereis eclipse來查看 eclipes的位置
在這裏插入圖片描述
我的eclipes目錄在/usr/local/eclipse
注意:如果有些人 有多個eclipes路徑 可以試試 /usr/lib/eclipse
複製插件 cp命令

hadoop@hadoop-VirtualBox:~$ cp ~/下載/hadoop-eclipse-plugin-2.7.1.jar /usr/local/eclipse/plugins/
hadoop@hadoop-VirtualBox:~$ chown hadoop:hadoop /usr/local/eclipse/plugins/hadoop-eclipse-plugin-2.7.1.jar 

3)通過命令使插件生效

生效插件,啓動eclipes

hadoop@hadoop-VirtualBox:~/下載$  cd /usr/local/eclipse
hadoop@hadoop-VirtualBox:/usr/local/eclipse$ ./eclipse -clean # 重啓eclipes
hadoop@hadoop-VirtualBox:/usr/local/eclipse$ eclipse -clean # 或者也可以用這個

注 clean命令只需要用一次即可,爲了使插件生效,之後仍然可以和原先一樣正常啓動
在這裏插入圖片描述

4)eclipes中Map/Reduce配置

eclipes已經啓動了,我們可以隨便創個項目Test,直接finish
在這裏插入圖片描述
點擊Window>Preferences 進入設置
在這裏插入圖片描述

題外話

如果插件沒有出現的話可能是因爲放的目錄不對,對於 whereis eclipse有多種路徑的情況下,可以換一個路徑的plugins試試 比如 /usr/lib/eclipse/plugins
在這裏插入圖片描述

繼續正文

點擊設置中的Hadoop Map/Reduce 就是我們剛剛安裝的插件
點擊後面的Browse按鈕,把虛擬機中的Hadoop位置添加進去
usr/local/hadoop
在這裏插入圖片描述
效果如下,接下來 Apply and Close
在這裏插入圖片描述
但是我們發現左邊的視圖仍沒有改變,這是因爲我們處於默認的Java default視圖下,我們需要切換到Map/reduce
點擊 Window>Perspectivr>open Perspectivr>other
在這裏插入圖片描述
選擇Map/Reduce視圖
在這裏插入圖片描述
就得到了我們想要的結果了
在這裏插入圖片描述
點擊下面的藍藍的小圖標進行創建
在這裏插入圖片描述
默認是這樣的
在這裏插入圖片描述
我們只需要爲它取名字,修改DFS Master的端口號爲9000即可,點擊Finish
在這裏插入圖片描述
可以看到HDFS的目錄已經出現在了Eclipes中雙擊可以查看內容,右鍵點擊可以上傳、下載、刪除 HDFS 中的文件,無需再通過繁瑣的 hdfs dfs -ls 等命令進行操作了。
注意,這裏的文件是自己事先上傳到HDFS中的,並不是打開就有這麼多
在這裏插入圖片描述

Tips

HDFS 中的內容變動後,Eclipse 不會同步刷新,需要右鍵點擊 Refresh,才能看到變動後的文件。

5)代碼測試

新建一個MapReduce Project ,隨便取個名字,直接finish
在這裏插入圖片描述
我們可以看到.eclipes已經自動爲我們導入了所需要的包
在這裏插入圖片描述在這裏插入圖片描述
在剛創建的項目中新建一個Class
在這裏插入圖片描述
代碼如下

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

在這之前,我們要先往hdfs中的input目錄上傳2個要統計的文件,內容可以自定
在這裏插入圖片描述
接下來我們就可以右鍵run,當然在這裏我們要自己設置一下參數
在這裏插入圖片描述

在這裏插入圖片描述
手動輸入參數,點擊運行,我們會發現他報錯了,並且錯誤原因是 Path not exist
在這裏插入圖片描述
這是因爲我們沒有上傳我們配置過的文件到項目的src目錄下
來自廈門大學林子雨老師實驗室
將我們之前修改過的文件和日誌加入eclipes的項目src目錄下

cp /usr/local/hadoop/etc/hadoop/core-site.xml ~/workspace/Mytest/src
cp /usr/local/hadoop/etc/hadoop/hdfs-site.xml ~/workspace/Mytest/src
cp /usr/local/hadoop/etc/hadoop/log4j.properties ~/workspace/Mytest/src

右鍵src Refresh刷新,就可以看到他們了
在這裏插入圖片描述
接下來按剛剛的步驟重新運行,記得輸入參數,運行結果如下
在這裏插入圖片描述
刷新之後我們就可以看到output文件夾了
在這裏插入圖片描述
裏面就存放了我們要統計的結果
在這裏插入圖片描述
成功!

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