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. 微信小程序TabBar的使用

    一.TabBar使用步骤 1.创建所需要的界面和所需要的图片: 2.配置文件: 我们找到项目根目录中的配置文件 app.json 加入如下配置信息 "tabBar": { &quo ...

  2. Python爬虫--Urllib库

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

  3. php总结7——文件函数库、序列化数据、文件包含

    7.1 文件函数库 php用来操作文件的 1) fopen    代开文件或URL 格式:resource fopen(string $filename, string $mode) 'r' 只读方式 ...

  4. VM tools安装错误The path "" is not a valid path to the xx generic kernel headers.

    VMWARE TOOLS安装提示THE PATH IS NOT A VALID PATH TO THE GENERIC KERNEL HEADERSI solved this problem, I g ...

  5. maven 手动安装本地jar包

    1.需要知道groupId.artifactId.version通过 cmd命令行执行 mvn install:install-file ,比如安装sigar.jar如下: mvn install:i ...

  6. pinpoint本地开发-web模块

    web模块中的前端依赖会导致工程很难打包成功,对于这些,我们可以直接注释掉 比如: <plugin> <groupId>com.github.eirslett</grou ...

  7. <关于JSP技术>运行机制及语法概述(附对本次同济校内ACM选拔赛决赛的吐槽)

    (一)JSP运行的机制 JSP是一种建立在Servlet规范功能之上的动态网页技术,它们都是在通常的网页文件中嵌入脚本代码,用于产生动态内容,不过和ASP不同的是JSP文件中嵌入的是Java代码和JS ...

  8. java中判断字符串是否相等有两种方法:

    1.用“==”运算符,该运算符表示指向字符串的引用是否相同,比如: String a="abc";String b="abc",那么a==b将返回true.这是 ...

  9. LNMP搭建随笔

    LNMP(即Linux+Nginx+MYSQL+PHP)是目前非常热门的动态网站部署架构,一般是指: Linux:如RHEL.Centos.Debian.Fedora.Ubuntu等系统. Nginx ...

  10. python当前工作文件夹中创建空的.txt文件

    import os def new_txt(): a1='实线' b = os.getcwd() + '\\fazhandadao_test_txt\\' if not os.path.exists( ...