hadoop之定制自己的Partitioner
partitioner负责shuffle过程的分组部分,目的是让map出来的数据均匀分布在reducer上,当然,如果我们不需要数据均匀,那么这个时候可以自己定制符合要求的partitioner. 下面内容涉及到的源代码请参考https://hadoop.apache.org/docs/current/api/org/apache/hadoop/mapreduce/Partitioner.html
Partitioner

1.Maperduce提供了四中的partitioner,如下图所示,它们都实现了Partitioner基类中的方法,源代码如下:
public abstract class Partitioner<KEY, VALUE> {
/**
* Get the partition number for a given key (hence record) given the total
* number of partitions i.e. number of reduce-tasks for the job.
*
* <p>Typically a hash function on a all or a subset of the key.</p>
*
* @param key the key to be partioned.
* @param value the entry value.
* @param numPartitions the total number of partitions.
* @return the partition number for the <code>key</code>.
*/
public abstract int getPartition(KEY key, VALUE value, int numPartitions);
}
2.默认的为 HashPartitioner,实现的分组方式如下:
public class HashPartitioner<K, V> extends Partitioner<K, V> {
/** Use {@link Object#hashCode()} to partition. */
public int getPartition(K key, V value,int numReduceTasks) {
return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
}
}
3.BinaryPatitioner是partitioner的偏特化自雷,该类提供leftOffset和rightOffset,在计算时对key的[leftOffset,rightOffset]这个区间取hash.
4.KeyFieldBasedPartioner也是基于hash的partitionwe,和BinaryPatitioner不同,它提供了多个区间计算hash,当区间数为0时,退化成HashParitioner.5.TotalOrderPartitioner这个类可以实现输出的全排序。不同于以上3个partitioner,这个类并不是基于hash的。在另外一篇文章:hadoop之全排列
定制自己的partitioner
实现自己的partitioner只需要将自己的类继承Partitioner即可,如下面自定义分组代码:
public class MyPartition extends Partitioner<Text, LongWritable> {
public int getPartition(Text key, LongWritable value, int numReduceTasks) {
return key.toString().contains("luoliang") ? 0 : 1;
}
}
在main函数中添加,实现定制的patitioner:
job.setPartitionerClass(MyPartitioner.class);
在文章Hadoop 2.2.0词频统计,实现了定制partitioner和combiner的单词统计的完整例子,可以作为参考.
待续
hadoop之定制自己的Partitioner的更多相关文章
- hadoop之定制自己的sort过程
Key排序 1. 继承WritableComparator 在hadoop之Shuffle和Sort中,可以看到mapper的输出文件spill文件需要在内存中排序,并且在输入reducer之前,不同 ...
- hadoop之Shuffle和Sort
MapRduce保证reducer的输入是按照key进行排过序的,原因和归并排序有关,在reducer接收到不同的mapper输出的有序数据后,需要再次进行排序,然后是分组排序,如果mapper输出的 ...
- MapReduce教程(二)MapReduce框架Partitioner分区<转>
1 Partitioner分区 1.1 Partitioner分区描述 在进行MapReduce计算时,有时候需要把最终的输出数据分到不同的文件中,按照手机号码段划分的话,需要把同一手机号码段的数据放 ...
- Hadoop的那些事儿(转)
原文:http://www.searchtb.com/tag/mapreduce 在说Hadoop之前,作为一个铁杆粉丝先粉一下Google.Google的伟大之处不仅在于它建立了一个强悍 ...
- MapReduce笔记——技术点汇总
目录 · 概况 · 原理 · MapReduce编程模型 · MapReduce过程 · 容错机制 · API · 概况 · WordCount示例 · Writable接口 · Mapper类 · ...
- 初识Kafka:构架、生产消费模型以及其他相关概念
当前使用的事件总线采用的是Kafka分布式消息队列来完成的,近来项目需要接入到事件总线中,故开启了kafka的学习之旅(之前一直在听说kafka这玩意儿,但是学习计划中还没有将它安排进去,借着这个机会 ...
- Apache Ranger安装部署
1.概述 Apache Ranger提供了一个集中式的安全管理框架,用户可以通过操作Ranger Admin页面来配置各种策略,从而实现对Hadoop生成组件,比如HDFS.YARN.Hive.HBa ...
- Hadoop学习笔记—9.Partitioner与自定义Partitioner
一.初步探索Partitioner 1.1 再次回顾Map阶段五大步骤 在第四篇博文<初识MapReduce>中,我们认识了MapReduce的八大步凑,其中在Map阶段总共五个步骤,如下 ...
- Hadoop的partitioner、全排序
按数值排序 示例:按气温字段对天气数据集排序问题:不能将气温视为Text对象并以字典顺序排序正统做法:用顺序文件存储数据,其IntWritable键代表气温,其Text值就是数据行常用简单做法:首先, ...
随机推荐
- Java基础加强之并发(二)常用的多线程实现方式
概述 常用的多线程实现方式有2种: 1. 继承Thread类 2. 实现Runnable接口 之所以说是常用的,是因为通过还可以通过JUC(java.util.concurrent)包中的线程池来实现 ...
- Python文件和流
#coding = utf-8 from pprint import pprint import fileinput #read(n) f = open(r'E:\test_dir\somefile. ...
- 记一次js之button问题
问题描述:记得某天,发现一件让我非常气愤的事情,居然因为一个按钮导致页面跳转失败或者是根本跳转不了界面,哪怕404也不给我报. 问题回现步骤: (1)正常输入url localhost:8080/te ...
- Python3的动态加载模块简单实例
import os import sys import time import myconfig b = ['123'] a = os.path.abspath(myconfig.__file__) ...
- Caused by: org.apache.velocity.exception.MethodInvocationException: Invocation of method 'getUser' in class org.uncommons.reportng.ReportMetadata threw exception class java.net.UnknownHostException :
Running TestSuite [TestNG] [WARN] Ignoring duplicate listener : org.uncommons.reportng.HTMLReporter ...
- python js 处理弹窗图片
内置函数 : driver.execute_script() 2.自定义弹窗 由于alert弹窗不美观,现在大多数网站都会使用自定义弹窗,使用Selenium自带的方法就驾驭不了了,此时就要搬出JS大 ...
- 《You dont know JS》原生函数
原生函数 原生函数,即JavaScript的内建函数(built-in function).常用的原生函数有String().Number().Boolean().Array().Object().F ...
- 网络编程中的read,write函数
关于TCP/IP协议,建议参考Richard Stevens的<TCP/IP Illustrated,vol1>(TCP/IP详解卷1). 关于第二层面,依然建议Richard Steve ...
- SpringCloud 学习(一) :Features
话不多说,现在在开发微服务项目,也想系统的学习一下SpringCloud,顾选择硬着头皮跟着英文官方文档学习一遍SpringCloud. 现在公司在用SpringCloud,也有很好的实践应用,加上更 ...
- ABAP-DBC录屏
*&---------------------------------------------------------------------**& ZXXL_MM02_01*& ...