MapReduce统计单词

一、HDFS个人理解

       HDFS是个文件系统,只不过加了个分布式作为文件系统的前缀而已,大概的结构就像LINUX系统。文件大的一个电脑存储不下,怎办?那就想办法存储到不同的机子上呗,于是HDFS就应用而生了。众所周知,文件在硬盘上是以文件块的形式存储。什么句柄啊,真心不懂,只能感知,没人能明白0和1,当然也没必要。

二、Map个人理解

       数学函数大家都清楚吧,f(x)=2x,这是一个简单的线性函数。

       打开脑洞,将x看成文件中的一行行的字符串,x=“hello hadoop hello mapreduce ” 而此时f(x)的功能是将此字符串按照空格隔开,输出结果为:(hello 1)、(hadoop 1)、(hello 1)、(mapreduce 1)格式。

       继续打开脑洞,将x看成一个大文件的一个文件块,比如1万行作为一个文件块,作为x的数值。Hadoop会为每一个文件块构建一个Map任务。

       假设,某个HDFS文件有5万行数据,Hadoop将5万行数据切分成5个不同的分片,每个分片有1万行数据,再假设有5台hadoop集群,那么Hadoop会构建5个Map任务,每个Map任务处理一个分片,完全是并行处理的。

三、 Map代码大放送 

public static class WordCountMapper extends Mapper<LongWritable,Text,Text,IntWritable> {
   private static final IntWritable ONE = new IntWritable(1);
   Text value = new Text();
    @Override
    public void map(LongWritable key, Text value, Context context) 
                                                        throws IOException, InterruptedException {
        StringTokenizer tokenizer = new StringTokenizer(value.toString());
        while (tokenizer.hasMoreTokens()) {
            word.set(StringUtils.trim(tokenizer.nextToken().replaceAll("\\W", "")));
            //写到一个临时目录下了
            context.write(word, one);
        }
   }
}

public static class MyMapper implements Mapper<LongWritable, Text, Text, IntWritable> {
        private static final IntWritable ONE = new IntWritable(1);
        private Text word = new Text();
        @Override
        public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
            String record = value.toString();
            String[] split = record.split(" ");
            for (String one : split) {
                word.set(one);
                output.collect(word, ONE);
            }
        }
    }

二、Shuffle阶段

       Shuffle即混洗,这个代码不需要我们去写,但是这个过程是存在的 。无数的类似与 (hello 1)、(hadoop 1)、(hello 1)、(mapreduce 1)这种格式的数据存储在磁盘下,真心不知道它是如何转换成(hello  [1,1])、(hadoop  [1])、(mapreduce  [1])这种格式的,想想都可怕,想想都觉得是艰难,难道是每次在写的时候,都去查询了一把?......待我翻书,给你解释,最好不要有什么宽依赖,窄依赖的,太怕了。

三、Reduce代码大放送

       没什么好解释的,Map的输出作为Reduce的输入。

public static class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
    @Override
    public void reduce(Text key, Iterable<IntWritable> values,Context context) throws                     IOException,InterruptedException {
        int sum = 0;
        for (IntWritable val : values) {
            sum += val.get();
        }
        context.write(key, new IntWritable(sum));
    }
}
public static class MyReducer implements Reducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable result = new IntWritable();
        @Override
        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();

            }
            result.set(sum);
            output.collect(key, result);
        }
    }

四、主方法代码大放送

public class WordCount {
    public static void main(String[] args) throws IOException, 
                                                    ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = new Job(conf);
        job.setJobName("wordcount");
        //在hadoop集群上运行这个作业的时候,需要将执行的类打包成一个jar文件,然后进行运行,为什么要打成jar文件呢?因为,因为,因为这是集群、要分发代码、要知道其他机子和driver做同样的事情。
        job.setJarByClass(WordCountMapper.class);
        //设置MapRededuce输出格式,key和value
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //设置map和reduce的类型
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReduce.class);
        //设置输入和输出的格式
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        //分别输入路径和输出路径,都是绝对路径
        FileInputFormat.setInputPaths(job, new Path("/user/hadoop/wordcount.txt"));
        FileOutputFormat.setOutputPath(job, new Path("/user/hadoop/" + new SimpleDateFormat("mmss").format(new Date())));
        //提交任务
        job.waitForCompletion(true);
    }
}



转载于:https://my.oschina.net/momisabuilder/blog/285650

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值