如何使用Hadoop的Partitioner
今天散仙要说的这个分区函数Partitioner,也是一样如此,下面我们先来看下Partitioner的作用:
对map端输出的数据key作一个散列,使数据能够均匀分布在各个reduce上进行后续操作,避免产生热点区。
Hadoop默认使用的分区函数是Hash Partitioner,源码如下:
- /**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.apache.hadoop.mapreduce.lib.partition;
- import org.apache.hadoop.mapreduce.Partitioner;
- /** Partition keys by their {@link Object#hashCode()}. */
- public class HashPartitioner<K, V> extends Partitioner<K, V> {
- /** Use {@link Object#hashCode()} to partition. */
- public int getPartition(K key, V value,
- int numReduceTasks) {
- //默认使用key的hash值与上int的最大值,避免出现数据溢出 的情况
- return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
- }
- }
大部分情况下,我们都会使用默认的分区函数,但有时我们又有一些,特殊的需求,而需要定制Partition来完成我们的业务,案例如下:
对如下数据,按字符串的长度分区,长度为1的放在一个,2的一个,3的各一个。
这时候,我们使用默认的分区函数,就不行了,所以需要我们定制自己的Partition,首先分析下,我们需要3个分区输出,所以在设置reduce的个数时,一定要设置为3,其次在partition里,进行分区时,要根据长度具体分区,而不是根据字符串的hash码来分区。核心代码如下:
- /**
- * Partitioner
- *
- *
- * */
- public static class PPartition extends Partitioner<Text, Text>{
- @Override
- public int getPartition(Text arg0, Text arg1, int arg2) {
- /**
- * 自定义分区,实现长度不同的字符串,分到不同的reduce里面
- *
- * 现在只有3个长度的字符串,所以可以把reduce的个数设置为3
- * 有几个分区,就设置为几
- * */
- String key=arg0.toString();
- ){
- %arg2;
- ){
- %arg2;
- ){
- %arg2;
- }
- ;
- }
- }
全部代码如下:
- package com.partition.test;
- import java.io.IOException;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapred.JobConf;
- import org.apache.hadoop.mapreduce.Job;
- import org.apache.hadoop.mapreduce.Mapper;
- import org.apache.hadoop.mapreduce.Partitioner;
- import org.apache.hadoop.mapreduce.Reducer;
- import org.apache.hadoop.mapreduce.lib.db.DBConfiguration;
- import org.apache.hadoop.mapreduce.lib.db.DBInputFormat;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
- import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
- import com.qin.operadb.PersonRecoder;
- import com.qin.operadb.ReadMapDB;
- /**
- * @author qindongliang
- *
- * 大数据交流群:376932160
- *
- *
- * **/
- public class MyTestPartition {
- /**
- * map任务
- *
- * */
- public static class PMapper extends Mapper<LongWritable, Text, Text, Text>{
- @Override
- protected void map(LongWritable key, Text value,Context context)
- throws IOException, InterruptedException {
- // System.out.println("进map了");
- //mos.write(namedOutput, key, value);
- String ss[]=value.toString().split(";");
- ]), new Text(ss[1]));
- }
- }
- /**
- * Partitioner
- *
- *
- * */
- public static class PPartition extends Partitioner<Text, Text>{
- @Override
- public int getPartition(Text arg0, Text arg1, int arg2) {
- /**
- * 自定义分区,实现长度不同的字符串,分到不同的reduce里面
- *
- * 现在只有3个长度的字符串,所以可以把reduce的个数设置为3
- * 有几个分区,就设置为几
- * */
- String key=arg0.toString();
- ){
- %arg2;
- ){
- %arg2;
- ){
- %arg2;
- }
- ;
- }
- }
- /***
- * Reduce任务
- *
- * **/
- public static class PReduce extends Reducer<Text, Text, Text, Text>{
- @Override
- protected void reduce(Text arg0, Iterable<Text> arg1, Context arg2)
- throws IOException, InterruptedException {
- ];
- System.out.println("key==> "+key);
- for(Text t:arg1){
- //System.out.println("Reduce: "+arg0.toString()+" "+t.toString());
- arg2.write(arg0, t);
- }
- }
- }
- public static void main(String[] args) throws Exception{
- JobConf conf=new JobConf(ReadMapDB.class);
- //Configuration conf=new Configuration();
- conf.set("mapred.job.tracker","192.168.75.130:9001");
- //读取person中的数据字段
- conf.setJar("tt.jar");
- //注意这行代码放在最前面,进行初始化,否则会报
- /**Job任务**/
- Job job=new Job(conf, "testpartion");
- job.setJarByClass(MyTestPartition.class);
- System.out.println("模式: "+conf.get("mapred.job.tracker"));;
- // job.setCombinerClass(PCombine.class);
- job.setPartitionerClass(PPartition.class);
- );//设置为3
- job.setMapperClass(PMapper.class);
- // MultipleOutputs.addNamedOutput(job, "hebei", TextOutputFormat.class, Text.class, Text.class);
- // MultipleOutputs.addNamedOutput(job, "henan", TextOutputFormat.class, Text.class, Text.class);
- job.setReducerClass(PReduce.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(Text.class);
- String path="hdfs://192.168.75.130:9000/root/outputdb";
- FileSystem fs=FileSystem.get(conf);
- Path p=new Path(path);
- if(fs.exists(p)){
- fs.delete(p, true);
- System.out.println("输出路径存在,已删除!");
- }
- FileInputFormat.setInputPaths(job, "hdfs://192.168.75.130:9000/root/input");
- FileOutputFormat.setOutputPath(job,p );
- : 1);
- }
- }
运行情况如下:
- 输出路径存在,已删除!
- ) | Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
- ) | Total input paths to process : 1
- ) | Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
- ) | Snappy native library not loaded
- ) | Running job: job_201404101853_0005
- ) | map 0% reduce 0%
- ) | map 100% reduce 0%
- ) | map 100% reduce 11%
- ) | map 100% reduce 22%
- ) | map 100% reduce 55%
- ) | map 100% reduce 100%
- ) | Job complete: job_201404101853_0005
- ) | Counters: 29
- ) | Job Counters
- ) | Launched reduce tasks=3
- ) | SLOTS_MILLIS_MAPS=7422
- ) | Total time spent by all reduces waiting after reserving slots (ms)=0
- ) | Total time spent by all maps waiting after reserving slots (ms)=0
- ) | Launched map tasks=1
- ) | Data-local map tasks=1
- ) | SLOTS_MILLIS_REDUCES=30036
- ) | File Output Format Counters
- ) | Bytes Written=61
- ) | FileSystemCounters
- ) | FILE_BYTES_READ=93
- ) | HDFS_BYTES_READ=179
- ) | FILE_BYTES_WRITTEN=218396
- ) | HDFS_BYTES_WRITTEN=61
- ) | File Input Format Counters
- ) | Bytes Read=68
- ) | Map-Reduce Framework
- ) | Map output materialized bytes=93
- ) | Map input records=7
- ) | Reduce shuffle bytes=93
- ) | Spilled Records=14
- ) | Map output bytes=61
- ) | Total committed heap usage (bytes)=207491072
- ) | CPU time spent (ms)=2650
- ) | Combine input records=0
- ) | SPLIT_RAW_BYTES=111
- ) | Reduce input records=7
- ) | Reduce input groups=7
- ) | Combine output records=0
- ) | Physical memory (bytes) snapshot=422174720
- ) | Reduce output records=7
- ) | Virtual memory (bytes) snapshot=2935713792
- ) | Map output records=7
运行后的结果文件如下:
其中,part-r-000000里面的数据
其中,part-r-000001里面的数据
其中,part-r-000002里面的数据
至此,我们使用自定义的分区策略完美的实现了,数据分区了。
总结:引用一段话
(Partition)分区出现的必要性,如何使用Hadoop产生一个全局排序的文件?最简单的方法就是使用一个分区,但是该方法在处理大型文件时效率极低,因为一台机器必须处理所有输出文件,从而完全丧失了MapReduce所提供的并行架构的优势。事实上我们可以这样做,首先创建一系列排好序的文件;其次,串联这些文件(类似于归并排序);最后得到一个全局有序的文件。主要的思路是使用一个partitioner来描述全局排序的输出。比方说我们有1000个1-10000的数据,跑10个ruduce任务, 如果我们运行进行partition的时候,能够将在1-1000中数据的分配到第一个reduce中,1001-2000的数据分配到第二个reduce中,以此类推。即第n个reduce所分配到的数据全部大于第n-1个reduce中的数据。这样,每个reduce出来之后都是有序的了,我们只要cat所有的输出文件,变成一个大的文件,就都是有序的了
基本思路就是这样,但是现在有一个问题,就是数据的区间如何划分,在数据量大,还有我们并不清楚数据分布的情况下。一个比较简单的方法就是采样,假如有一亿的数据,我们可以对数据进行采样,如取10000个数据采样,然后对采样数据分区间。在Hadoop中,patition我们可以用TotalOrderPartitioner替换默认的分区。然后将采样的结果传给他,就可以实现我们想要的分区。在采样时,我们可以使用hadoop的几种采样工具,RandomSampler,InputSampler,IntervalSampler。
这样,我们就可以对利用分布式文件系统进行大数据量的排序了,我们也可以重写Partitioner类中的compare函数,来定义比较的规则,从而可以实现字符串或其他非数字类型的排序,也可以实现二次排序乃至多次排序。
如何使用Hadoop的Partitioner的更多相关文章
- Hadoop的partitioner、全排序
按数值排序 示例:按气温字段对天气数据集排序问题:不能将气温视为Text对象并以字典顺序排序正统做法:用顺序文件存储数据,其IntWritable键代表气温,其Text值就是数据行常用简单做法:首先, ...
- python 实现Hadoop的partitioner和二次排序
我们知道,一个典型的Map-Reduce过程包 括:Input->Map->Partition->Reduce->Output. Partition负责把Map任务输出的中间结 ...
- Hadoop里的Partitioner
人们对于Mapreduce程序刚開始时都觉得仅仅须要一个reduce就够了. 毕竟,在你处理数据之前一个reducer已经把数据都分好类了,有谁不喜欢分好类的数据呢. 可是这样我们就忽略了并行计算的优 ...
- hadoop编程技巧(3)---定义自己的区划类别Partitioner
Hadoop代码测试环境:Hadoop2.4 原则:在Hadoop的MapReduce过程.Mapper阅读过程完成后数据.它将数据发送到Partitioner.由Partitioner每个记录应当采 ...
- Hadoop 综合揭秘——MapReduce 基础编程(介绍 Combine、Partitioner、WritableComparable、WritableComparator 使用方式)
前言 本文主要介绍 MapReduce 的原理及开发,讲解如何利用 Combine.Partitioner.WritableComparator等组件对数据进行排序筛选聚合分组的功能.由于文章是针对开 ...
- Hadoop基础-MapReduce的Partitioner用法案例
Hadoop基础-MapReduce的Partitioner用法案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Partitioner关键代码剖析 1>.返回的分区号 ...
- hadoop之定制自己的Partitioner
partitioner负责shuffle过程的分组部分,目的是让map出来的数据均匀分布在reducer上,当然,如果我们不需要数据均匀,那么这个时候可以自己定制符合要求的partitioner. 下 ...
- Hadoop日记Day17---计数器、map规约、分区学习
一.Hadoop计数器 1.1 什么是Hadoop计数器 Haoop是处理大数据的,不适合处理小数据,有些大数据问题是小数据程序是处理不了的,他是一个高延迟的任务,有时处理一个大数据需要花费好几个小时 ...
- 基于Hadoop 2.6.0运行数字排序的计算
上个博客写了Hadoop2.6.0的环境部署,下面写一个简单的基于数字排序的小程序,真正实现分布式的计算,原理就是对多个文件中的数字进行排序,每个文件中每个数字占一行,排序原理是按行读取后分块进行排序 ...
随机推荐
- MD5文件
我从某网站下载了一个iso系统镜像,我担心下载下来之后,被我电脑上的病毒感染了.我要确定这个文件还是“原汁原味”,就可以用软件再次生成该文件的md5码,然后和网站上的md5码对比一下就可以了.我用的是 ...
- PyVmomi Clone_VM with CustomizaitonSpec
调用CustomizaitonSpec来Clone VM ''' Created on 2017-09-03 @author: Vincen ''' from pyVmomi import vim f ...
- FastReport5的安装
1.运行recompile.exe,选择相应的delphi版本,选择recompile to chinese,编译: 2.选择recompile all pakages,编译: 3.将lib目录下的d ...
- JAVA- 数据库连接池原理
第一次Java程序要在MySQL中执行一条语句,那么就必须建立一个Connection对象,代表了与MySQL数据库的连接通过直接发送你要执行的SQL语句之后,就会调用Connection.close ...
- Linux下重命名文件或文件夹(mv命令与rename命令)
在Linux下重命名文件或目录,可以使用mv命令或rename命令 mv ———————————— mv命令既可以重命名,又可以移动文件或文件夹. 例子:将目录A重命名为B mv A B 例子:将/a ...
- 如何在node.js中使用neo4j
本章中你将会学到如何在node.js中使用neo4j图形数据库. 当你想存储或者查询和数据紧密关联的数据的时候,图形数据库很有用. neo4j是一个可有效存储,处理和查询你数据模型中紧密相连的元素的数 ...
- C#子线程执行完后通知主线程
其实这个比较简单,子线程怎么通知主线程,就是让子线程做完了自己的事儿就去干主线程的转回去干主线程的事儿. 那么怎么让子线程去做主线程的事儿呢,我们只需要把主线程的方法传递给子线程就行了,那么传递方法就 ...
- linux apt-get remove如何恢复
linux卸载或删除软件时,若不小心删除到关联的软件,如果想撤销删除操作需要在/var/log/apt/history.log中依次安装删除的软件,具体操作如下: $echo '#!/bin/bash ...
- codeforces 553B B. Kyoya and Permutation(找规律)
题目链接: B. Kyoya and Permutation time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- 基于libRTMP的流媒体直播之 AAC、H264 推送
这段时间在捣腾基于 RTMP 协议的流媒体直播框架,其间参考了众多博主的文章,剩下一些细节问题自行琢磨也算摸索出个门道,现将自己认为比较恼人的 AAC 音频帧的推送和解析.H264 码流的推送和解析以 ...