1.解析Partition

Map的结果,会通过partition分发到Reducer上,Reducer做完Reduce操作后,通过OutputFormat,进行输出,下面我们就来分析参与这个过程的类。

Mapper的结果,可能送到Combiner做合并,Combiner在系统中并没有自己的基类,而是用Reducer作为Combiner的基类,他们对外的功能是一样的,只是使用的位置和使用时的上下文不太一样而已。Mapper最终处理的键值对,是需要送到Reducer去合并的,合并的时候,有相同key的键/值对会送到同一个Reducer那。哪个key到哪个Reducer的分配过程,是由Partitioner规定的。它只有一个方法,

[java] view
plain
copy



  1. getPartition(Text key, Text value, int numPartitions)

输入是Map的结果对和Reducer的数目,输出则是分配的Reducer(整数编号)。就是指定Mappr输出的键值对到哪一个reducer上去。系统缺省的Partitioner是HashPartitioner,它以key的Hash值对Reducer的数目取模,得到对应的Reducer。这样保证如果有相同的key值,肯定被分配到同一个reducre上。如果有N个reducer,编号就为0,1,2,3……(N-1)。

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

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。

2.代码实例

[java] view
plain
copy



  1. package org.apache.hadoop.examples;
  2. import java.io.IOException;
  3. import java.util.*;
  4. import org.apache.hadoop.fs.Path;
  5. import org.apache.hadoop.conf.*;
  6. import org.apache.hadoop.io.*;
  7. import org.apache.hadoop.mapred.*;
  8. import org.apache.hadoop.util.*;
  9. //Partitioner函数的使用
  10. public class MyPartitioner {
  11. // Map函数
  12. public static class MyMap extends MapReduceBase implements
  13. Mapper {
  14. public void map(LongWritable key, Text value,
  15. OutputCollector output, Reporter reporter)
  16. throws IOException {
  17. String[] arr_value = value.toString().split("\t");
  18. //测试输出
  19. //          for(int i=0;i
  20. //          {
  21. //              System.out.print(arr_value[i]+"\t");
  22. //          }
  23. //          System.out.print(arr_value.length);
  24. //          System.out.println();
  25. Text word1 = new Text();
  26. Text word2 = new Text();
  27. if (arr_value.length > 3) {
  28. word1.set("long");
  29. word2.set(value);
  30. } else if (arr_value.length < 3) {
  31. word1.set("short");
  32. word2.set(value);
  33. } else {
  34. word1.set("right");
  35. word2.set(value);
  36. }
  37. output.collect(word1, word2);
  38. }
  39. }
  40. public static class MyReduce extends MapReduceBase implements
  41. Reducer {
  42. public void reduce(Text key, Iterator values,
  43. OutputCollector output, Reporter reporter)
  44. throws IOException {
  45. int sum = 0;
  46. System.out.println(key);
  47. while (values.hasNext()) {
  48. output.collect(key, new Text(values.next().getBytes()));
  49. }
  50. }
  51. }
  52. // 接口Partitioner继承JobConfigurable,所以这里有两个override方法
  53. public static class MyPartitionerPar implements Partitioner {
  54. @Override
  55. public int getPartition(Text key, Text value, int numPartitions) {
  56. // TODO Auto-generated method stub
  57. int result = 0;
  58. System.out.println("numPartitions--" + numPartitions);
  59. if (key.toString().equals("long")) {
  60. result = 0 % numPartitions;
  61. } else if (key.toString().equals("short")) {
  62. result = 1 % numPartitions;
  63. } else if (key.toString().equals("right")) {
  64. result = 2 % numPartitions;
  65. }
  66. System.out.println("result--" + result);
  67. return result;
  68. }
  69. @Override
  70. public void configure(JobConf arg0)
  71. {
  72. // TODO Auto-generated method stub
  73. }
  74. }
  75. //输入参数:/home/hadoop/input/PartitionerExample /home/hadoop/output/Partitioner
  76. public static void main(String[] args) throws Exception {
  77. JobConf conf = new JobConf(MyPartitioner.class);
  78. conf.setJobName("MyPartitioner");
  79. //控制reducer数量,因为要分3个区,所以这里设定了3个reducer
  80. conf.setNumReduceTasks(3);
  81. conf.setMapOutputKeyClass(Text.class);
  82. conf.setMapOutputValueClass(Text.class);
  83. //设定分区类
  84. conf.setPartitionerClass(MyPartitionerPar.class);
  85. conf.setOutputKeyClass(Text.class);
  86. conf.setOutputValueClass(Text.class);
  87. //设定mapper和reducer类
  88. conf.setMapperClass(MyMap.class);
  89. conf.setReducerClass(MyReduce.class);
  90. conf.setInputFormat(TextInputFormat.class);
  91. conf.setOutputFormat(TextOutputFormat.class);
  92. FileInputFormat.setInputPaths(conf, new Path(args[0]));
  93. FileOutputFormat.setOutputPath(conf, new Path(args[1]));
  94. JobClient.runJob(conf);
  95. }
  96. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

Hadoop中Partition解析的更多相关文章

  1. Hadoop 中疑问解析

    Hadoop 中疑问解析 FAQ问题剖析 一.HDFS 文件备份与数据安全性分析1 HDFS 原理分析1.1 Hdfs master/slave模型 hdfs采用的是master/slave模型,一个 ...

  2. Hadoop中Partition深度解析

    本文地址:http://www.cnblogs.com/archimedes/p/hadoop-partitioner.html,转载请注明源地址. 旧版 API 的 Partitioner 解析 P ...

  3. Hadoop中Partition的定制

    1.解析Partition Map的结果,会通过partition分发到Reducer上,Reducer做完Reduce操作后,通过OutputFormat,进行输出,下面我们就来分析参与这个过程的类 ...

  4. Hadoop中OutputFormat解析

    一.OutputFormat OutputFormat描述的是MapReduce的输出格式,它主要的任务是: 1.验证job输出格式的有效性,如:检查输出的目录是否存在. 2.通过实现RecordWr ...

  5. Hadoop中Yarnrunner里面submit Job以及AM生成 至Job处理过程源码解析

    参考 http://blog.csdn.net/caodaoxi/article/details/12970993 Hadoop中Yarnrunner里面submit Job以及AM生成 至Job处理 ...

  6. Hadoop中的各种排序

    本篇博客是金子在学习hadoop过程中的笔记的整理,不论看别人写的怎么好,还是自己边学边做笔记最好了. 1:shuffle阶段的排序(部分排序) shuffle阶段的排序可以理解成两部分,一个是对sp ...

  7. 1 weekend110的复习 + hadoop中的序列化机制 + 流量求和mr程序开发

    以上是,weekend110的yarn的job提交流程源码分析的复习总结 下面呢,来讲weekend110的hadoop中的序列化机制 1363157985066      13726230503  ...

  8. 用shell获得hadoop中mapreduce任务运行结果的状态

    在近期的工作中,我需要用脚本来运行mapreduce,并且要判断运行的结果,根据结果来做下一步的动作. 开始我想到shell中获得上一条命令运行结果的方法,即判断"$?"的值 if ...

  9. hadoop中实现java网络爬虫

    这一篇网络爬虫的实现就要联系上大数据了.在前两篇java实现网络爬虫和heritrix实现网络爬虫的基础上,这一次是要完整的做一次数据的收集.数据上传.数据分析.数据结果读取.数据可视化. 需要用到 ...

随机推荐

  1. 利用Docker Compose快速搭建本地测试环境

    前言 Compose是一个定义和运行多个Docker应用的工具,用一个YAML(dockder-compose.yml)文件就能配置我们的应用.然后用一个简单命令就能启动所有的服务.Compose编排 ...

  2. myql 5.6 安装

    环境: centos 6.5  192.168.9.28  4核4G 虚拟机 一. 安装编译源码所需要的工具和库 [root@localhost ~]# yum -y install gcc gcc- ...

  3. python 基础及资料汇总

    Python 包.模块.类以及代码文件和目录的一种管理方案     Numpy 小结   用 Python 3 的 async / await 做异步编程  K-means 在 Python 中的实现 ...

  4. hdu 4667 Building Fence < 计算几何模板>

    //大白p263 #include <cmath> #include <cstdio> #include <cstring> #include <string ...

  5. 马尔科夫链在第n步转移的状态的概率分布

  6. Python爬虫--Urllib库

    Urllib库 Urllib是python内置的HTTP请求库,包括以下模块:urllib.request (请求模块).urllib.error( 异常处理模块).urllib.parse (url ...

  7. Struts2问题总结

    1 如何搭建Struts2开发环境? Struts2 获取   http://struts.apache.org/download.cgi Struts-2.3.16.3-all.zip 创建Web项 ...

  8. Spring IOC 容器源码分析(转)

    原文地址 Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IOC 容器.既然大家平时都要用到 Spring,怎么可以不好好了解 Spring 呢 ...

  9. GEO(地理信息定位)

    核心知识点: 1.GEO是利用zset来存储地理位置信息,可以用来计算地理位置之间的距离,也可以做统计: 2.命令:geoadd geopos geodist geohash georadius/ge ...

  10. 编写你的第一个django应用程序3

    这一篇从教程第2部分结尾的地方继续讲起.我们将继续编写投票应用,并且专注于如何创建公用界面--也被称为视图 概况 django视图概念是一类具有相同功能和末班的网页的集合,比如,在一个博客应用中,你可 ...