关于MapReduce中自定义分区类(四)
MapTask类
if(useNewApi){runNewMapper(job, splitMetaInfo, umbilical, reporter);}
@SuppressWarnings("unchecked")private<INKEY,INVALUE,OUTKEY,OUTVALUE>void runNewMapper(final JobConf job,final TaskSplitIndex splitIndex,final TaskUmbilicalProtocol umbilical,TaskReporter reporter) throws IOException,ClassNotFoundException,InterruptedException{// make a task context so we can get the classesorg.apache.hadoop.mapreduce.TaskAttemptContext taskContext =new org.apache.hadoop.mapreduce.task.TaskAttemptContextImpl(job,getTaskID(),reporter);// make a mapperorg.apache.hadoop.mapreduce.Mapper<INKEY,INVALUE,OUTKEY,OUTVALUE> mapper =(org.apache.hadoop.mapreduce.Mapper<INKEY,INVALUE,OUTKEY,OUTVALUE>)ReflectionUtils.newInstance(taskContext.getMapperClass(), job);// make the input formatorg.apache.hadoop.mapreduce.InputFormat<INKEY,INVALUE> inputFormat =(org.apache.hadoop.mapreduce.InputFormat<INKEY,INVALUE>)ReflectionUtils.newInstance(taskContext.getInputFormatClass(), job);// rebuild the input splitorg.apache.hadoop.mapreduce.InputSplit split = null;split = getSplitDetails(newPath(splitIndex.getSplitLocation()),splitIndex.getStartOffset());LOG.info("Processing split: "+ split);org.apache.hadoop.mapreduce.RecordReader<INKEY,INVALUE> input =newNewTrackingRecordReader<INKEY,INVALUE>(split, inputFormat, reporter, taskContext);job.setBoolean(JobContext.SKIP_RECORDS, isSkipping());org.apache.hadoop.mapreduce.RecordWriter output = null;// get an output objectif(job.getNumReduceTasks()==0){output = 如果jreduce个数等于0.则执行该方法newNewDirectOutputCollector(taskContext, job, umbilical, reporter);}else{如果reduce个数大于0.则执行该方法output =newNewOutputCollector(taskContext, job, umbilical, reporter);}org.apache.hadoop.mapreduce.MapContext<INKEY, INVALUE, OUTKEY, OUTVALUE>mapContext =newMapContextImpl<INKEY, INVALUE, OUTKEY, OUTVALUE>(job, getTaskID(),input, output,committer,reporter, split);org.apache.hadoop.mapreduce.Mapper<INKEY,INVALUE,OUTKEY,OUTVALUE>.ContextmapperContext =newWrappedMapper<INKEY, INVALUE, OUTKEY, OUTVALUE>().getMapContext(mapContext);try{input.initialize(split, mapperContext);mapper.run(mapperContext);mapPhase.complete();setPhase(TaskStatus.Phase.SORT);statusUpdate(umbilical);input.close();input = null;output.close(mapperContext);output = null;} finally {closeQuietly(input);closeQuietly(output, mapperContext);}}
// get an output objectif(job.getNumReduceTasks()==0){output = 如果jreduce个数等于0.则执行该方法newNewDirectOutputCollector(taskContext, job, umbilical, reporter);}else{如果reduce个数大于0.则执行该方法output =newNewOutputCollector(taskContext, job, umbilical, reporter);}
NewOutputCollector(org.apache.hadoop.mapreduce.JobContext jobContext,JobConf job,TaskUmbilicalProtocol umbilical,TaskReporter reporter) throws IOException,ClassNotFoundException{collector = createSortingCollector(job, reporter);partitions = jobContext.getNumReduceTasks();if(partitions >1){partitioner =(org.apache.hadoop.mapreduce.Partitioner<K,V>)ReflectionUtils.newInstance(jobContext.getPartitionerClass(), job);}else{partitioner =new org.apache.hadoop.mapreduce.Partitioner<K,V>(){@Overridepublicint getPartition(K key, V value,int numPartitions){return partitions -1;}};}}
/*** Get the {@link Partitioner} class for the job.** @return the {@link Partitioner} class for the job.*/publicClass<? extends Partitioner<?,?>> getPartitionerClass()throws ClassNotFoundException;
/*** Get the {@link Partitioner} class for the job.** @return the {@link Partitioner} class for the job.*/@SuppressWarnings("unchecked")publicClass<? extends Partitioner<?,?>> getPartitionerClass()throws ClassNotFoundException{return(Class<? extends Partitioner<?,?>>)conf.getClass(PARTITIONER_CLASS_ATTR,HashPartitioner.class);}
publicclassHashPartitioner<K, V>extendsPartitioner<K, V>{/** Use {@link Object#hashCode()} to partition. */publicint getPartition(K key, V value,int numReduceTasks){return(key.hashCode()&Integer.MAX_VALUE)% numReduceTasks;}}
@Overridepublicint hashCode(){final int prime =31;int result =1;result = prime * result +((account == null)?0: account.hashCode());// result = prime * result + ((amount == null) ? 0 : amount.hashCode());return result;}
publicstaticclassKeyPartitioner extends Partitioner<SelfKey,DoubleWritable>{@Overridepublicint getPartition(SelfKey key,DoubleWritable value,int numPartitions){/*** 如何保证数据整体输出上的有序,需要我们自定义业务逻辑* 必须提示前知道num reduce task 个数?* \w 单词字符[a-zA-Z_0-9]**/String account =key.getAccount();//0xxaaabbb 0-9//[0-2][3-6][7-9]if(account.matches("\\w*[0-2]")){return0;}elseif(account.matches("\\w*[3-6]")){return1;}elseif(account.matches("\\w*[7-9]")){return2;}return0;}}
关于MapReduce中自定义分区类(四)的更多相关文章
- 关于MapReduce中自定义分组类(三)
Job类 /** * Define the comparator that controls which keys are grouped together * for a single ...
- 关于MapReduce中自定义Combine类(一)
MRJobConfig public static fina COMBINE_CLASS_ATTR 属性COMBINE_CLASS_ATTR = "mapreduce.j ...
- 在hadoop作业中自定义分区和归约
当遇到有特殊的业务需求时,需要对hadoop的作业进行分区处理 那么我们可以通过自定义的分区类来实现 还是通过单词计数的例子,JMapper和JReducer的代码不变,只是在JSubmit中改变了设 ...
- 关于MapReduce中自定义带比较key类、比较器类(二)——初学者从源码查看其原理
Job类 /** * Define the comparator that controls * how the keys are sorted before they * are pa ...
- MapReduce之自定义分区器Partitioner
@ 目录 问题引出 默认Partitioner分区 自定义Partitioner步骤 Partition分区案例实操 分区总结 问题引出 要求将统计结果按照条件输出到不同文件中(分区). 比如:将统计 ...
- python3.4中自定义数组类(即重写数组类)
'''自定义数组类,实现数组中数字之间的四则运算,内积运算,大小比较,数组元素访问修改及成员测试等功能''' class MyArray: '''保证输入值为数字元素(整型,浮点型,复数)''' de ...
- flask中自定义日志类
一:项目架构 二:自定义日志类 1. 建立log.conf的配置文件 log.conf [log] LOG_PATH = /log/ LOG_NAME = info.log 2. 定义日志类 LogC ...
- 读取SequenceFile中自定义Writable类型值
1)hadoop允许程序员创建自定义的数据类型,如果是key则必须要继承WritableComparable,因为key要参与排序,而value只需要继承Writable就可以了.以下定义一个Doub ...
- Java中自定义注解类,并加以运用
在Java框架中,经常会使用注解,而且还可以省很多事,来了解下自定义注解. 注解是一种能被添加到java代码中的元数据,类.方法.变量.参数和包都可以用注解来修饰.注解对于它所修饰的代码并没有直接的影 ...
随机推荐
- mac 键盘映射 karabiner
mac 键盘映射 karabiner 今天在vim编辑的时候觉得用mac的方向键有点麻烦 需要移动我的小右手,然后就搜个映射方案. 百度出来了 karabiner. 官网 安装什么的就不说了, 安完了 ...
- 高级查询---嵌套and分页
高级嵌套语句: 子查询: 语句: select * from 表名 where 列名= ( 子查询语句 ) 注意:子查询语句必须放在小括号呢 可以使用< >=等运算符号,sql serve ...
- JDBC中的Statement和PreparedStatement的区别
JDBC中的Statement和PreparedStatement的区别
- jdbc java数据库连接 11)中大文本类型的处理
1. Jdbc中大文本类型的处理 Oracle中大文本数据类型, Clob 长文本类型 (MySQL中不支持,使用的是text) Blob 二进制类型 MySQL数据库, Text ...
- 【repost】JS原型与原型链终极详解
一. 普通对象与函数对象 JavaScript 中,万物皆对象!但对象也是有区别的.分为普通对象和函数对象,Object ,Function 是JS自带的函数对象.下面举例说明 function f ...
- Keepalived使用梳理
keepalived介绍keepalived观察其名可知,保持存活,在网络里面就是保持在线了,也就是所谓的高可用或热备,它集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防 ...
- PAT 1046. 划拳(15)
划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字.如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输家罚一杯酒.两人同赢或两人同输 ...
- 解读ASP.NET 5 & MVC6系列(16):自定义View视图文件查找逻辑
之前MVC5和之前的版本中,我们要想对View文件的路径进行控制的话,则必须要对IViewEngine接口的FindPartialView或FindView方法进行重写,所有的视图引擎都继承于该IVi ...
- 解读ASP.NET 5 & MVC6系列(13):TagHelper
在新版的MVC6中,微软提供了强大的TagHelper功能,以便让我们摆脱如下的臃肿代码: @Html.LabelFor(model => model.FullName) @Html.EditF ...
- Mybatis关联查询和数据库不一致问题分析与解决
Mybatis关联查询和数据库不一致问题分析与解决 本文的前提是,确定sql语句没有问题,确定在数据库中使用sql和项目中结果不一致. 在使用SpringMVC+Mybatis做多表关联时候,发现也不 ...