千家信息网

hadoop-reduce的示例分析

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,小编给大家分享一下hadoop-reduce的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!Map的结果,会通过p
千家信息网最后更新 2026年11月01日hadoop-reduce的示例分析

小编给大家分享一下hadoop-reduce的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

Map的结果,会通过partition分发到Reducer上,Reducer做完Reduce操作后,通过OutputFormat,进行输出。

* Licensed to the Apache Software Foundation (ASF) under onepackage org.apache.hadoop.mapreduce;import java.io.IOException; * Reduces a set of intermediate values which share a key to a smaller set ofpublic class Reducer {  public class Context     extends ReduceContext {    public Context(Configuration conf, TaskAttemptID taskid,                   RawKeyValueIterator input,                    Counter inputKeyCounter,                   Counter inputValueCounter,                   RecordWriter output,                   OutputCommitter committer,                   StatusReporter reporter,                   RawComparator comparator,                   Class keyClass,                   Class valueClass                   ) throws IOException, InterruptedException {      super(conf, taskid, input, inputKeyCounter, inputValueCounter,            output, committer, reporter,             comparator, keyClass, valueClass);    }  }  /**   * Called once at the start of the task.   */  protected void setup(Context context                       ) throws IOException, InterruptedException {    // NOTHING  }  /**   * This method is called once for each key. Most applications will define   * their reduce class by overriding this method. The default implementation   * is an identity function.   */  @SuppressWarnings("unchecked")  protected void reduce(KEYIN key, Iterable values, Context context                        ) throws IOException, InterruptedException {    for(VALUEIN value: values) {      context.write((KEYOUT) key, (VALUEOUT) value);    }  }  /**   * Called once at the end of the task.   */  protected void cleanup(Context context                         ) throws IOException, InterruptedException {    // NOTHING  }  /**   * Advanced application writers can use the    * {@link #run(org.apache.hadoop.mapreduce.Reducer.Context)} method to   * control how the reduce task works.   */  public void run(Context context) throws IOException, InterruptedException {    setup(context);    while (context.nextKey()) {      reduce(context.getCurrentKey(), context.getValues(), context);    }    cleanup(context);  }}

Mapper的结果,可能送到可能的Combiner做合并,Combiner在系统中并没有自己的基类,而是用Reducer作为Combiner的基类,他们对外的功能是一样的,只是使用的位置和使用时的上下文不太一样而已。

Mapper最终处理的结果对,是需要送到Reducer去合并的,合并的时候,有相同key的键/值对会送到同一个Reducer那,哪个key到哪个Reducer的分配过程,是由Partitioner规定的,它只有一个方法,输入是Map的结果对和Reducer的数目,输出则是分配的Reducer(整数编号)。系统缺省的Partitioner是HashPartitioner,它以key的Hash值对Reducer的数目取模,得到对应的Reducer。

* Licensed to the Apache Software Foundation (ASF) under onepackage org.apache.hadoop.mapreduce; * Partitions the key space.public abstract class Partitioner {    /**    * Get the partition number for a given key (hence record) given the total    * number of partitions i.e. number of reduce-tasks for the job.   *      * 

Typically a hash function on a all or a subset of the key.

* * @param key the key to be partioned. * @param value the entry value. * @param numPartitions the total number of partitions. * @return the partition number for the key. */ public abstract int getPartition(KEY key, VALUE value, int numPartitions); } * Licensed to the Apache Software Foundation (ASF) under onepackage org.apache.hadoop.mapreduce.lib.partition;import org.apache.hadoop.mapreduce.Partitioner;/** Partition keys by their {@link Object#hashCode()}. */public class HashPartitioner extends Partitioner { /** Use {@link Object#hashCode()} to partition. */ public int getPartition(K key, V value, int numReduceTasks) { return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks; }}

Reducer是所有用户定制Reducer类的基类,和Mapper类似,它也有setup,reduce,cleanup和run方法,其中setup和cleanup含义和Mapper相同,reduce是真正合并Mapper结果的地方,它的输入是key和这个key对应的所有value的一个迭代器,同时还包括Reducer的上下文。系统中定义了两个非常简单的Reducer,IntSumReducer和LongSumReducer,分别用于对整形/长整型的value求和。

* Licensed to the Apache Software Foundation (ASF) under onepackage org.apache.hadoop.mapreduce.lib.reduce;import java.io.IOException;public class IntSumReducer extends Reducer {  private IntWritable result = new IntWritable();  public void reduce(Key key, Iterable values,                      Context context) throws IOException, InterruptedException {    int sum = 0;    for (IntWritableval : values) {      sum += val.get();    }    result.set(sum);    context.write(key, result);  }}

Reduce的结果,通过Reducer.Context的方法collect输出到文件中,和输入类似,Hadoop引入了OutputFormat。OutputFormat依赖两个辅助接口:RecordWriter和OutputCommitter,来处理输出。RecordWriter提供了write方法,用于输出和close方法,用于关闭对应的输出。OutputCommitter提供了一系列方法,用户通过实现这些方法,可以定制OutputFormat生存期某些阶段需要的特殊操作。我们在TaskInputOutputContext中讨论过这些方法(明显,TaskInputOutputContext是OutputFormat和Reducer间的桥梁)。

OutputFormat和RecordWriter分别对应着InputFormat和RecordReader,系统提供了空输出NullOutputFormat(什么结果都不输出,NullOutputFormat.RecordWriter只是示例,系统中没有定义),LazyOutputFormat(没在类图中出现,不分析),FilterOutputFormat(不分析)和基于文件FileOutputFormat的SequenceFileOutputFormat和TextOutputFormat输出。

基于文件的输出FileOutputFormat利用了一些配置项配合工作,包括mapred.output.compress:是否压缩;mapred.output.compression.codec:压缩方法;mapred.output.dir:输出路径;mapred.work.output.dir:输出工作路径。FileOutputFormat还依赖于FileOutputCommitter,通过FileOutputCommitter提供一些和Job,Task相关的临时文件管理功能。如FileOutputCommitter的setupJob,会在输出路径下创建一个名为_temporary的临时目录,cleanupJob则会删除这个目录。

SequenceFileOutputFormat输出和TextOutputFormat输出分别对应输入的SequenceFileInputFormat和TextInputFormat

以上是"hadoop-reduce的示例分析"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

输出 方法 结果 系统 分析 文件 输入 示例 篇文章 路径 相同 上下 上下文 两个 内容 功能 只是 数目 用户 目录 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技图片 科技与互联网 易互联网科技有限公司 互联网金融科技创新 亿账通互联网科技 互联网科技小额贷款 多迪互联网科技有限公司 恒生科技互联网指数分析 杭州壁岐互联网科技有限公司 新兴科技互联网创新大赛颁奖典礼 链环科技传统企业怎么转型互联网 高科技互联网发展前景如何 深圳无忧互联网科技 广州富喜互联网络科技 浙南科技城互联网大厦招聘 厦门江牛科技互联网 三亚科技创新互联网基地 上海旺乐尼互联网科技有限公司 现代科技的标志之一的互联网 互联网科技创业有什么好项目 中国互联网科技工作委员会毛军 美国互联网科技巨头监管 奇酷互联网络科技公司 善班互联网科技有限公司 福州新华互联网科技学校 知乎 上海宁丞科技互联网 燊美集互联网科技有限公司 江苏互联网科技小贷 互联网科技手机排行榜 上海互联网科技园景观
0