hadoop大数据入门篇-hdfs(二)

1.主题

  采用MapReduce框架实现wordCount功能

2.适用读者对象

  本文适用于hadoop大数据入门初学者

3.基础环境信息

软件系统haoop
版本centos72.6.0-cdh5.14.4

4.问题描述

你有一个100TB的文件,存放着大量的单词,每行有多个单词,用逗号分隔,现在你需要计算每个单词的出现次数。

文件内容如下:

    helllo,world

    hdfs,world

    world,hdfs

    ......



以上问题如果用单机程序处理,代码复杂,性能低下!

5.解决方案

采用MapReduce模型计算框架



(1)代码处理数据流程图

在这里插入图片描述
(2)详细代码

package com.zhenglihan.cdh.hdfs;
import java.io.IOException;
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;

/**
 * 功能描述:
 *   采用MapReduce框架实现wordCount功能
 *   输入:hdfs
 *   输出:hdfs
 *   args(0): /tmp/zhenglihan/cdhBigdata/hdfs/wordCount.txt
 *   args(1):/tmp/zhenglihan/cdhBigdata/hdfs/mapredTmp
 */
public class MapreduceWordCountDemo {
    public static class TokenizerMapper
            extends Mapper<Object, Text, Text, IntWritable>{

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        /**
         * map函数负责以“行”为单位处理文件
         * @param key:输入键(不重要)
         * @param value:输入值(真正要处理的文本数据)
         * @param context:存放输出键和输出值的应用上下文
         * @throws IOException
         * @throws InterruptedException
         */
        public void map(Object key, Text value, Context context
        ) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                //单词每出现一次发送一个键值对(word,1)
                context.write(word, one);
            }
        }
    }

    public static class IntSumReducer
            extends Reducer<Text,IntWritable,Text,IntWritable> {
        private IntWritable result = new IntWritable();

        /**
         *  reduce函数会把map函数的处理结果中相同key值的(key,value)对收集到一起,上次(key,Iterable<IntWritable> values)
         * @param key:这里表示文本中的单词
         * @param values:value集合
         * @param context
         * @throws IOException
         * @throws InterruptedException
         */
        public void reduce(Text key, Iterable<IntWritable> values,
                           Context context
        ) throws IOException, InterruptedException {
            int sum = 0;
            //累加单词出现的次数
            for (IntWritable val : values) {
                sum += val.get();
            }
            result.set(sum);
            context.write(key, result);
        }
    }

    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:  <in> [<in>...] <out>");
            System.exit(2);
        }
        Job job = Job.getInstance(conf, "word count");
        job.setJarByClass(MapreduceWordCountDemo.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(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);
    }

}

6.总结

  MapReduce是一个基于分而治之思想,用于处理海量数据的分布式计算框架,它是大数据处理最基本的一个思想,要学好大数据,必须深刻理解该框架,后续很多组件的设计,都是建立在该框架思想上的。

  本文主要讲解最基本的代码框架和数据处理流程,更为深度的讲解请扫描底部二维码关注公众号,关注后续博文,一起学习hadoop大数据!
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值