Mapreduce之wordcount词频统计

一、需求说明

1.输入文件图示

在这里插入图片描述

2.需求

统计数据文件中每个字母出现的次数,以字母-次数的形式输出,例如(a 14)。

二、代码实现

1.书写思路

在map阶段读取每一行以空格分隔的数据,以(字母,1)的形式输出到reduce阶段,在输出之前会底层会实现分区功能,把字母相同的结果分到一个区间,传递到reduce阶段,在reduce阶段统计每一个分区的字母出现次数。最终输出结果到指定文件夹。

2.代码

Mapper:

package com.worldcount.zqd;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class WCMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
    Text k = new Text();
    LongWritable l = new LongWritable();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString(); //读取一行数据
        String[] words = StringUtils.split(line, " ");  //利用工具类对读取的数据的空格进行切割,返回数组
        for (String word:words) {
            k.set(word);    // 获取word
            l.set(1);       // 默认为1
            context.write(k, l);    //写入键值对
        }

    }
}

Reducer:

package com.worldcount.zqd;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;

public class WCreducer extends Reducer<Text, LongWritable, Text, LongWritable> {

    LongWritable l = new LongWritable();

    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        long count = 0;     //计数变量
        // 增强for拿到分区的所有value值, 进行累加
        for (LongWritable value : values) {
            count += value.get();
        }
        l.set(count);
        context.write(key, l);
    }
}

主程序:

package com.worldcount.zqd;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class WCrunner {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(WCrunner.class);
        job.setMapperClass(WCMapper.class);
        job.setReducerClass(WCreducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        FileInputFormat.setInputPaths(job, new Path("C:\\Users\\Lenovo\\Desktop\\hadoop_mr\\wc_input"));
        FileOutputFormat.setOutputPath(job, new Path("C:\\Users\\Lenovo\\Desktop\\hadoop_mr\\wc_out"));
        job.waitForCompletion(true);
    }
}

结果如下图:
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,词频统计是一个非常经典的MapReduce程序案例。下面是一份Java代码示例: ``` public class WordCount { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); 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()); context.write(word, one); } } } public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable(); 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(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 在这个示例中,Mapper将输入文本分割成单词,并且将每个单词映射为键值对(单词,1)。然后,Combiner将相同单词的键值对合并起来,并且将它们的值相加。最后,Reducer将每个单词的总和输出为键值对(单词,总和)。在MapReduce执行过程中,Hadoop会自动将数据分割为若干个等大小的块,并行处理这些块,最终将它们的结果合并输出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值