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文件夹了
在这里插入图片描述
里面就存放了我们要统计的结果
在这里插入图片描述
成功!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Joker-Tong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值