Hadoop学习:MapReduce实现WordCount经典案例

## ✌✌✌古人有云,好记性不如烂笔头,千里之行,始于足下,每日千行代码必不可少,每日总结写一写,目标大厂,满怀希望便会所向披靡,哈哈哈!!!✌✌✌

在这里插入图片描述

一、✌题目要求

> 统计文本中每个单词的数量

二、✌实现思想

> Map阶段默认输入为TextInputFormat,键值对对应为行的偏移量和每行的文本内容
> 在map函数中将每行文本进行切分,提取出每个单词
> 在Reduce阶段根据相同Key值进行累加求和
> 

三、✌代码实现

1.✌Map类

public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        //获取每一行的文本
        String line = value.toString();

        //将每行文本进行切分
        String[] words = line.split(" ");

        //循环写入,写出格式为:hello 1
        for (String word : words) {
            context.write(new Text(word), new IntWritable(1));
        }
    }

}

2.✌Reduce类

public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        //创建计数变量
        int sum = 0;

        //统计相同key对应的value值进行累加
        for (IntWritable value : values) {
            sum += value.get();
        }

        //写出,写出格式为:hello 3
        context.write(key, new IntWritable(sum));
    }
}

3.✌Driver类

public class WordCountDriver {
    
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        //设置文件输入输出路径
        args=new String[]{"D:/input/inputword","D:/output"};
        
        //打印日志信息
        BasicConfigurator.configure();
        
        //创建配置信息对象
        Configuration conf=new Configuration();

        //获取job对象
        Job job= Job.getInstance(conf);

        //关联Driver、Map、Reduce类
        job.setJarByClass(WordCountDriver.class);
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        //设置Map阶段的输出格式
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //设置最终的输出格式
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //配置文件路径
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        //提交任务
        boolean result=job.waitForCompletion(true);

        System.exit(result?0:1);
        
    }
    
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

海洋 之心

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

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

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

打赏作者

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

抵扣说明:

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

余额充值