Eclipse调试Hadoop-MapReduce程序

根据上一篇《Hadoop2.6.0-Centos7.0真机安装过程》的配置,后续配置开发环境
用户名,端口,hadoop的位置信息都在上文中配置,下面的链接需要一致

环境:

  • CentOS7.0-x64
  • Hadoop2.6.0
  • eclipse-java普通版
  • JDK1.8.0_40
  • hadoop-eclipse-plugin-2.6.0.jar

1) 启动hadoop进程,在hadoop根目录下:

sbin/.start-dfs.sh
sbin/.start-yarn.sh

2) 下载eclipse-java普通版,在本地解压

3) 安装hadoop-eclipse 插件
下载地址: https://github.com/winghc/hadoop2x-eclipse-plugin
或: http://download.csdn.net/detail/garreet/8604631
把.jar包放到eclipse的plugins目录下

4) 启动eclipse
打开Window–>Preferens,你会发现Hadoop Map/Reduce选项,在这个选项里你需要配置Hadoop installation directory ,填写本机中安装hadoop的目录。配置完成后退出
示例

5) 配置Map/Reduce Locations
在Window–>Show View中打开Map/Reduce Locations。
在Map/Reduce Locations中新建一个Hadoop Location。
在这个View中,右键–>New Hadoop Location。
在弹出的对话框中你需要配置
Location name,如mycentos
还有Map/Reduce Master和DFS Master
这里面的Host、Port分别为你在mapred-site.xml、core-site.xml中配置的地址及端口。
User name 配置启动hadoop的linux用户名
如:
这里写图片描述

6)新建项目HelloHadoop。
File–>New–>Other–>Map/Reduce Project
新建文件 WordCount.java,统计单词的使用次数,并按照顺序排序,代码如下:

import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
public class WordCount {
    public static class Map extends MapReduceBase implements
            Mapper<LongWritable, Text, Text, IntWritable> {
        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(LongWritable key, Text value,
                OutputCollector<Text, IntWritable> output, Reporter reporter)
                throws IOException {
            String line = value.toString();
            StringTokenizer tokenizer = new StringTokenizer(line);
            while (tokenizer.hasMoreTokens()) {
                word.set(tokenizer.nextToken());
                output.collect(word, one);
            }
        }
    }

    public static class Reduce extends MapReduceBase implements
            Reducer<Text, IntWritable, Text, IntWritable> {
        public void reduce(Text key, Iterator<IntWritable> values,
                OutputCollector<Text, IntWritable> output, Reporter reporter)
                throws IOException {
            int sum = 0;
            while (values.hasNext()) {
                sum += values.next().get();
            }
            output.collect(key, new IntWritable(sum));
        }
    }

    public static void main(String[] args) throws Exception {
        JobConf conf = new JobConf(WordCount.class);
        conf.setJobName("wordcount");

        conf.setOutputKeyClass(Text.class);
        conf.setOutputValueClass(IntWritable.class);

        conf.setMapperClass(Map.class);
        conf.setReducerClass(Reduce.class);

        conf.setInputFormat(TextInputFormat.class);
        conf.setOutputFormat(TextOutputFormat.class);

        FileInputFormat.setInputPaths(conf, new Path(args[0]));
        FileOutputFormat.setOutputPath(conf, new Path(args[1]));

        JobClient.runJob(conf);
    }
}

7) 上传模拟数据文件夹。
当前目录(如hadoop安装目录)下新建文件夹input,并在文件夹下新建两个文件file01、file02
file01 :

Hello World Bye World 

file02 :

Hello Hadoop Goodbye Hadoop  

上传到hadoop

bin/hadoop fs -put input /input01

8) 在eclipse中查看hadoop的文件
eclipse的 View -> Show View -> Other -> General -> Project Explorer
里面会有一个“DFS Location”,里面有个蓝色的大象,打开后右键“Refresh”
就可以看到刚才上传的文件夹(下面的截图里面把结果文件夹也包含了,可以忽略)
这里写图片描述

9) 运行工程
1.点击WordCount.java,右键–>Run As–>Run Configurations
2.在弹出的Run Configurations对话框中,点Java Application,右键–>New,这时会新建一个application名为WordCount
3.配置运行参数,点Arguments,在Program arguments中输入“你要传给程序的输入文件夹和你要求程序将计算结果保存的文件夹”,如:
这里写图片描述
点“Run”运行程序

10) 查看结果
1. 在DFS Location里右键“Refresh”可以刷新看到,上图已经有结果的展示了
2. 命令

# 查看输出文件夹
hadoop fs -ls /
# 查看输出文件
hadoop fs -ls /output01
# 查看文件内容
hadoop fs -cat output01/* 

结果:

Bye 1  
Goodbye 1  
Hadoop  2  
Hello   2  
World   2  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值