词频统计之Reducer实现

MapReduce WordCount Reducer详解
package com.imooc.bigdata.hadoop.hdfs.mr.wc;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

public class WordCountReducer extends Reducer <Text,IntWritable,Text,IntWritable>{
    /**
     *
     * (hello,1) (world,1)
     * (hello,1) (world,1)
     * (hello,1)(world,1)
     * (welcome,1)
     *
     * map的输出到reduce端,是按照相同的key分发到一个reduce上去执行
     *
     * reduce1:(hello,1) (hello,1) (hello,1) ==> (hello,<1,1,1>)
     * reduce2:(world,1) (world,1) (world,1) ==> (world,<1,1,1>)
     *
     *Reducer
     *
     */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        int count = 0;

        Iterator<IntWritable> iterator = values.iterator();
        while(iterator.hasNext()){
            IntWritable value = iterator.next();
            count += value.get();
        }
        context.write(key,new IntWritable(1));

    }
}

### Hadoop 实现词频统计教程 #### 了解Hadoop环境设置 为了成功运行词频统计程序,需确保已正确安装并配置好Hadoop集群。这通常涉及编辑`core-site.xml`, `hdfs-site.xml`, 和 `mapred-site.xml` 文件以适应特定部署需求[^4]。 #### 准备输入数据 在执行词频统计之前,应先准备好待处理的数据集,并将其上传至HDFS指定目录下。例如,可以使用如下命令完成此操作: ```bash hadoop fs -put local_input_path hdfs_destination_directory ``` 这里假设本地路径为`local_input_path`而目标HDFS位置则设为`hdfs_destination_directory`[^2]。 #### 编写MapReduce代码 对于简单的词频统计任务来说,可以直接调用内置于Hadoop中的WordCount例子;然而,理解其工作原理同样重要。以下是简化版Java MapReduce实现方式用于说明目的: ```java import java.io.IOException; 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; 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(); @Override protected void map(Object key, Text value, Context context) throws IOException, InterruptedException { String[] words = value.toString().split("\\W+"); for (String w : words) { if (!w.isEmpty()) { word.set(w.toLowerCase()); context.write(word, one); } } } } public static class SumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable(); @Override protected 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(SumReducer.class); job.setReducerClass(SumReducer.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); } } ``` 这段代码定义了一个标准的MapReduce作业流程,其中包含了映射器(Mappper)负责分割文本成单个词语以及规约器(Reducer)汇总相同关键词的数量[^1]。 #### 执行预构建好的WordCount工具 如果不想手动编写上述源码,则可利用官方提供的jar包快速启动词频统计过程。具体做法是在终端里键入类似下面这样的指令: ```bash hadoop jar path_to_examples_jar_file wordcount input_hdfs_dir output_hdfs_dir ``` 这里的`path_to_examples_jar_file`指的是包含有WordCount类别的JAR文件的位置,比如`/opt/cloudera/parcels/CDH/jars/hadoop-3.0.0-cdh6.2.0.jar`; 而`input_hdfs_dir`和`output_hdfs_dir`分别代表要读取的原始资料所在地址及其运算完成后结果保存之处。 #### 结果验证 一旦任务结束之后,可以通过查看输出目录下的部分文件(part-r-*)来获取最终得到的词频统计数据。这些文件位于由上一步骤所设定的目标存储区域之内[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值