MapReduce入门详解(二)

个人博客原文链接

MapReduce实践攻略

超详细入门级-WordCount

问题描述:
统计一个文件中,各种单词出现的次数
思路分析:

  1. 在map阶段,对每行数据调用一次map方法,对读取到的每行数据按空格进行切割,将分割得到的每个单词作为key,value的值给定为1传递给reduce
  2. 在reduce阶段,从map接收到传递过来的key和value,key值相同的为同一组,对每一组只调用一次reduce方法,将每一组的value值累加即可得到该单词出现的次数,最后将该组的key作为key,累加的value作为value作为结果输出
public class WordCountMR2 extends Configured implements Tool {
    /**
     * KEYIN: 默认情况下,是mr框架所读到的一行文本的起始偏移量,Long,
     * 但是在hadoop中有自己的更精简的序列化接口,所以不直接用Long,而用LongWritable
     * VALUEIN:默认情况下,是mr框架所读到的一行文本的内容,String,同上,用Text
     * KEYOUT:是用户自定义逻辑处理完成之后输出数据中的key,在此处是单词,String,同上,用Text
     * VALUEOUT:是用户自定义逻辑处理完成之后输出数据中的value,在此处是单词次数,Integer,同上,用IntWritable
     */
    public static class WCMapper extends Mapper<LongWritable,Text, Text, IntWritable> {
        /**
         * map阶段的业务逻辑就写在自定义的map()方法中
         * maptask会对每一行输入数据调用一次我们自定义的map()方法
         * context是上下文引用对象,传递输出值
         */
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            Collections.list(new StringTokenizer(value.toString()," ")).stream().map(s -> ((String)s).trim())
                    .filter(s -> s.length() > 1).forEach(ExceptionConsumer.of(word -> context.write(new Text(word),new IntWritable(1))));
        }
    }

    /**
     * KEYIN, VALUEIN对应mapper输出的KEYOUT,VALUEOUT类型对应
     * KEYOUT, VALUEOUT是自定义reduce逻辑处理结果的输出数据类型
     * KEYOUT是单词
     * VLAUEOUT是总次数
     */
    public static class WCReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
        /**
         * reduce阶段的业务逻辑就写在自定义的reduce()方法中
         * reducetask会对所有相同的key调用一次reduce()方法
         * context是上下文引用对象,传递输出值
         */
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            //map阶段的输出是reduce阶段的输入,样式如下
            //<helle,1><hello,1><helle,1><hello,1><helle,1><hello,1>
            //<tom,1><tom,1><tom,1>
            //<good,1>

//            int count = 0;
//            for (IntWritable value : values){
//                count += value.get();
//            }
//            context.write(key, new IntWritable(count));

            IntWritable count = StreamSupport.stream(values.spliterator(), false).collect(Collectors.toSet()).stream()
                    .reduce((a, b) -> new IntWritable(a.get() + b.get())).get();
            context.write(key,count);
        }
    }
    @Override
    public int run(String[] strings) throws Exception {
        Configuration conf = getConf();
        //创建job实例对象
        Job job = Job.getInstance(conf,"test_fun_wordcount2");
        //指定本程序的jar包所在的本地路径
        job.setJarByClass(this.getClass());
        //指定本业务job要使用的mapper/Reducer业务类
        job.setMapperClass(WCMapper.class);
        job.setReducerClass(WCReducer.class);
        //指定mapper输出数据的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //指定最终输出的数据的kv类型
        //注:不是setReduceOutput,因为有的时候只需要用到map,直接输出map的结果就可以
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //指定job的输入原始文件所在目录
        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job,new Path(conf.get("inpath")));
        //指定job的输出结果所在目录
        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job,new Path(conf.get("outpath")));
        //指定开启的reduce的数量
        job.setNumReduceTasks(1);
        //将job中配置的相关参数,以及job所用的java类所在的jar包,提交给yarn去运行
        return job.waitForCompletion(true) ? 0 : 1;
    }
    public static void main(String[] args) throws Exception{
        ToolRunner.run(new WordCountMR2(),args);
    }
}

去重-DuplicateRemoveMR

问题描述:
去掉列表中所有重复的值,不考虑顺序
思路分析:
将每一行的值按分隔符切开重新排序,然后再拼接起来作为key,value置为NullWritable类型,传递给reduce,reduce对相同的key只会输出一次,以此达到去重复的效果。

public class DuplicateRemoveMR extends Configured implements Tool {
    public static class DRMapper extends Mapper<LongWritable,Text, Text, NullWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String str = Collections.list(new StringTokenizer(value.toString(),
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值