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. Halcon下载、安装

    下载地址: 官网:http://www.halcon.com/halcon/download/ Halcon学习网:http://www.ihalcon.com/read.php?tid=56 { 最 ...

  2. log4j 2 入门实例(1)

    本文介绍log4j的基本概念和将日志输出到控制台的例子. 参考文章: http://www.jianshu.com/p/464058bdbc76 http://www.hankcs.com/progr ...

  3. SSH Tunnel扫盲(ssh port forwarding端口转发)

    SSH的的Port Forward,中文可以称为端口转发,是SSH的一项非常重要的功能.它可以建立一条安全的SSH通道,并把任意的TCP连接放到这条通道中.下面仔细就仔细讨论SSH的这种非常有用的功能 ...

  4. spring cloud初识

    spring cloud是spring中的一个快速开发框架.本实例采用spring+maven来配置一个简单的spring开发实例. 1.首先安装java和maven环境. ①.安装java,不做过多 ...

  5. 转载:SPFA算法学习

    转载地址:http://www.cnblogs.com/scau20110726/archive/2012/11/18/2776124.html 粗略讲讲SPFA算法的原理,SPFA算法是1994年西 ...

  6. UVA12103 —— Leonardo's Notebook —— 置换分解

    题目链接:https://vjudge.net/problem/UVA-12103 题意: 给出大写字母“ABCD……Z”的一个置换B,问是否存在一个置换A,使得A^2 = B. 题解: 对于置换,有 ...

  7. win10安装tomcat7

    下载Tomcat 安装tomcat tomcat7是绿色软件,解压后即可使用,请大家先将tomcat解压到合适的位置(建议整个路径都是英文路径),下载 apache-tomcat-7.0.79-win ...

  8. jboss7的JAX-WS客户端

    jboss版本 jboss-eap-6.1, 实际上就是jboss-as-7.x.fianal 本篇讨论使用jboss7自带的cxf库,使用wsdl文件生成和部署jax-ws的客户端程序. 首先明确一 ...

  9. Oracle约束的使用

    --5个约束,主键约束.外键约束.唯一约束.检查约束.非空约束. --添加主键约束 Alter table table_name Add constraints constraint_name Pri ...

  10. deepin 安装微信与QQ

    安装QQ sudo apt-get install deepin.com.qq.im 安装微信 sudo apt-get install deepin.com.wechat 附录 其他安装包 http ...