Hadoop中Partition解析
1.解析Partition
Map的结果,会通过partition分发到Reducer上,Reducer做完Reduce操作后,通过OutputFormat,进行输出,下面我们就来分析参与这个过程的类。
Mapper的结果,可能送到Combiner做合并,Combiner在系统中并没有自己的基类,而是用Reducer作为Combiner的基类,他们对外的功能是一样的,只是使用的位置和使用时的上下文不太一样而已。Mapper最终处理的键值对<key, value>,是需要送到Reducer去合并的,合并的时候,有相同key的键/值对会送到同一个Reducer那。哪个key到哪个Reducer的分配过程,是由Partitioner规定的。它只有一个方法,
- getPartition(Text key, Text value, int numPartitions)
输入是Map的结果对<key, value>和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方法,用于输出<key, value>和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.代码实例
- package org.apache.hadoop.examples;
- import java.io.IOException;
- import java.util.*;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.conf.*;
- import org.apache.hadoop.io.*;
- import org.apache.hadoop.mapred.*;
- import org.apache.hadoop.util.*;
- /**
- * 输入文本,以tab间隔
- * kaka 1 28
- * hua 0 26
- * chao 1
- * tao 1 22
- * mao 0 29 22
- * */
- //Partitioner函数的使用
- public class MyPartitioner {
- // Map函数
- public static class MyMap extends MapReduceBase implements
- Mapper<LongWritable, Text, Text, Text> {
- public void map(LongWritable key, Text value,
- OutputCollector<Text, Text> output, Reporter reporter)
- throws IOException {
- String[] arr_value = value.toString().split("\t");
- //测试输出
- // for(int i=0;i<arr_value.length;i++)
- // {
- // System.out.print(arr_value[i]+"\t");
- // }
- // System.out.print(arr_value.length);
- // System.out.println();
- Text word1 = new Text();
- Text word2 = new Text();
- if (arr_value.length > 3) {
- word1.set("long");
- word2.set(value);
- } else if (arr_value.length < 3) {
- word1.set("short");
- word2.set(value);
- } else {
- word1.set("right");
- word2.set(value);
- }
- output.collect(word1, word2);
- }
- }
- public static class MyReduce extends MapReduceBase implements
- Reducer<Text, Text, Text, Text> {
- public void reduce(Text key, Iterator<Text> values,
- OutputCollector<Text, Text> output, Reporter reporter)
- throws IOException {
- int sum = 0;
- System.out.println(key);
- while (values.hasNext()) {
- output.collect(key, new Text(values.next().getBytes()));
- }
- }
- }
- // 接口Partitioner继承JobConfigurable,所以这里有两个override方法
- public static class MyPartitionerPar implements Partitioner<Text, Text> {
- /**
- * getPartition()方法的
- * 输入参数:键/值对<key,value>与reducer数量numPartitions
- * 输出参数:分配的Reducer编号,这里是result
- * */
- @Override
- public int getPartition(Text key, Text value, int numPartitions) {
- // TODO Auto-generated method stub
- int result = 0;
- System.out.println("numPartitions--" + numPartitions);
- if (key.toString().equals("long")) {
- result = 0 % numPartitions;
- } else if (key.toString().equals("short")) {
- result = 1 % numPartitions;
- } else if (key.toString().equals("right")) {
- result = 2 % numPartitions;
- }
- System.out.println("result--" + result);
- return result;
- }
- @Override
- public void configure(JobConf arg0)
- {
- // TODO Auto-generated method stub
- }
- }
- //输入参数:/home/hadoop/input/PartitionerExample /home/hadoop/output/Partitioner
- public static void main(String[] args) throws Exception {
- JobConf conf = new JobConf(MyPartitioner.class);
- conf.setJobName("MyPartitioner");
- //控制reducer数量,因为要分3个区,所以这里设定了3个reducer
- conf.setNumReduceTasks(3);
- conf.setMapOutputKeyClass(Text.class);
- conf.setMapOutputValueClass(Text.class);
- //设定分区类
- conf.setPartitionerClass(MyPartitionerPar.class);
- conf.setOutputKeyClass(Text.class);
- conf.setOutputValueClass(Text.class);
- //设定mapper和reducer类
- conf.setMapperClass(MyMap.class);
- conf.setReducerClass(MyReduce.class);
- conf.setInputFormat(TextInputFormat.class);
- conf.setOutputFormat(TextOutputFormat.class);
- FileInputFormat.setInputPaths(conf, new Path(args[0]));
- FileOutputFormat.setOutputPath(conf, new Path(args[1]));
- JobClient.runJob(conf);
- }
- }
Hadoop中Partition解析相关推荐
- 7、大数据中常见的文件存储格式以及hadoop中支持的压缩算法
Hadoop系列文章目录 1.hadoop3.1.4简单介绍及部署.简单验证 2.HDFS操作 - shell客户端 3.HDFS的使用(读写.上传.下载.遍历.查找文件.整个目录拷贝.只拷贝文件.列 ...
- Hadoop常见错误解析
1:Shuffle Error: Exceeded MAX_FAILED_UNIQUE_FETCHES; bailing-out Answer: 程序里面需要打开多个文件,进行分析,系统一般默认数量 ...
- 考究Hadoop中split的计算方法
Hadoop中block块大小和split切片大小会影响到MapReduce程序在运行过程中的效率.map的个数.在本文中,以经典入门案例WordCount为例,通过debug的方式跟踪源代码,来分析 ...
- Hadoop中mapreduce作业日志是如何生成的
摘要:本篇博客介绍了hadoop中mapreduce类型的作业日志是如何生成的.主要介绍日志生成的几个关键过程,不涉及过多细节性的内容. 本文分享自华为云社区<hadoop中mapreduce作 ...
- 浅析 Hadoop 中的数据倾斜
最近几次被问到关于数据倾斜的问题,这里找了些资料也结合一些自己的理解. 在并行计算中我们总希望分配的每一个task 都能以差不多的粒度来切分并且完成时间相差不大,但是集群中可能硬件不同,应用的类型不同 ...
- kafka partition java,kafka中partition数量与消费者对应关系以及Java实践
kafka中partition数量与消费者对应关系以及Java实践 kafka中partition数量与消费者对应关系以及Java实践 kafka是由Apache软件基金会开发的一个开源流处理平台.k ...
- Zookeeper 在Hadoop中的应用
Zookeeper 简单介绍 Zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目.它主要是用来解决分布式应用中常常遇到的一些数据管理问题,如:统一命名服务.状态同步服务.集 ...
- 数据蒋堂 | Hadoop中理论与工程的错位
作者:蒋步星 来源:数据蒋堂 校对:林亦霖 本文共1400字,建议阅读6分钟. 本文分析了在Hadoop的设计和实现中的理论问题和工程问题. Hadoop是当前重要的大数据计算平台,它试图摒弃传统数据 ...
- hadoop中的序列化与Writable类
本文地址:http://www.cnblogs.com/archimedes/p/hadoop-writable-class.html,转载请注明源地址. hadoop中自带的org.apache.h ...
最新文章
- 崛起于Springboot2.X之Mybatis-全注解方式操作Mysql(4)
- pandas比较两个dataframe特定数据列的数值是否相同并给出差值:使用np.where函数
- 英国最新报告:40% AI公司其实没用任何AI技术
- 欢迎参加“城市大脑与应急管理”专家研讨会
- php_yaf 安装
- tomcat 部署脚本
- php数组遍历相同的元素覆盖_php获取数组中重复数据的两种方法
- Docker及K8S使用碎碎记
- 计算机主机内有哪些部件常用的,智慧职教: 计算机系统由什么组成?计算机主机内有哪些部件?常用的计算机外设有哪些...
- 人脸识别资源推荐:20款人脸检测/识别的API、库和软件
- Linux下辅助DNS的搭建以及远程和加密更新
- 作为一个生鲜电商自媒体
- python时间模块 dir(time)_python sys,os,time模块的使用(包括时间格式的各种转换)...
- 创建maven工程时总是带有后缀名Maven Webapp解决办法
- DDA数值微分法详解
- Java线程状态与方法关系
- IPhone4S自定义铃声
- Android audio 三 AudioRecord 分析下
- 苹果手机支持鸿蒙,除了苹果,这四款华为也能三年不卡,还能升级鸿蒙
- Vue+DataV+Echarts组件创建炫酷科技大屏~(注释多多)
热门文章
- ElasticSearch 未授权访问记录(端口:9200)
- module ‘urllib‘ has no attribute ‘unquote‘(url解码)
- 虚拟机Ubuntu蓝屏闪屏解决方法
- C/C++:uint64_t 转为char*
- linux开机启动详细流程图
- 20155207实验2 Windows口令破解
- ASP.Net WebForm温故知新学习笔记:二、ViewState与UpdatePanel探秘
- 洛谷 P3183 [HAOI2016]食物链
- [转载]oracle常用经典SQL查询
- 重命名数据库解决“无法用排他锁锁定该数据库”