hadoop1中partition和combiner作用
---恢复内容开始---
1、解析Partiton
把map任务的输出的中间结果按照key的范围进行划分成r份,r代表reduce任务的个数。hadoop默认有个类HashPartition实现分区,通过key对reduce的个数取模(key%r),这样可以保证一段范围内的key交由一个reduce处理。以此来实现reduce的负载均衡。不至于使有些reduce处理的任务压力过大,有些reduce空闲。
如果我们对hadoop本身的分区算法不满意,或者我们因为我们的业务需求,我们可以自定义一个类实现Partition接口,实现里面的方法,在getPartiton()方法中实现自己的分区算法。在提交作业的main方法中通setPartitonclass()方法这个类,就可以了。
以下为代码实例
- 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);
- }
- }
2、解析Combiner
在Partiton之前,我们还可以对中间结果进行Combiner,即将中间结果中有着相同key 的(key,value)键值对进行合并成一对,Combiner的过程与reduce的过程类似,很多情况下可以直接使用reduce,但是Combiner作为Map任务的一部分,在Map输出后紧接着执行,通过Combiner的执行,减少了中间结果中的(key,value)对数目,reduce在从map复制数据时将会大大减少网络流量,每个reduce需要和原许多个map任务节点通信以此来取得落到它负责key区间内的中间结果,然后执行reduce函数,得到一个最中结果文件。有R个reduce任务,就有R个最终结果,这R个最终结果并不需要合并成一个结果,因为这R个最终结果又可以作为另一次计算的输入,开始另一次计算。
combiner使用总结:
combiner的使用可以在满足业务需求的情况下,大大提高job的运行速度,如果不合适,则将到最后导致结果不一致(如:求平均值)。
以下为Combiner代码示例
- package com;
- import java.io.IOException;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.conf.Configured;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.DoubleWritable;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Job;
- import org.apache.hadoop.mapreduce.Mapper;
- import org.apache.hadoop.mapreduce.Reducer;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
- import org.apache.hadoop.util.Tool;
- import org.apache.hadoop.util.ToolRunner;
- public class AveragingWithCombiner extends Configured implements Tool {
- public static class MapClass extends Mapper<LongWritable,Text,Text,Text> {
- static enum ClaimsCounters { MISSING, QUOTED };
- // Map Method
- public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
- String fields[] = value.toString().split(",", -20);
- String country = fields[4];
- String numClaims = fields[8];
- if (numClaims.length() > 0 && !numClaims.startsWith("\"")) {
- context.write(new Text(country), new Text(numClaims + ",1"));
- }
- }
- }
- public static class Reduce extends Reducer<Text,Text,Text,DoubleWritable> {
- // Reduce Method
- public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
- double sum = 0;
- int count = 0;
- for (Text value : values) {
- String fields[] = value.toString().split(",");
- sum += Double.parseDouble(fields[0]);
- count += Integer.parseInt(fields[1]);
- }
- context.write(key, new DoubleWritable(sum/count));
- }
- }
- public static class Combine extends Reducer<Text,Text,Text,Text> {
- // Reduce Method
- public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
- double sum = 0;
- int count = 0;
- for (Text value : values) {
- String fields[] = value.toString().split(",");
- sum += Double.parseDouble(fields[0]);
- count += Integer.parseInt(fields[1]);
- }
- context.write(key, new Text(sum+","+count));
- }
- }
- // run Method
- public int run(String[] args) throws Exception {
- // Create and Run the Job
- Job job = new Job();
- job.setJarByClass(AveragingWithCombiner.class);
- FileInputFormat.addInputPath(job, new Path(args[0]));
- FileOutputFormat.setOutputPath(job, new Path(args[1]));
- job.setJobName("AveragingWithCombiner");
- job.setMapperClass(MapClass.class);
- job.setCombinerClass(Combine.class);
- job.setReducerClass(Reduce.class);
- job.setInputFormatClass(TextInputFormat.class);
- job.setOutputFormatClass(TextOutputFormat.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(Text.class);
- System.exit(job.waitForCompletion(true) ? 0 : 1);
- return 0;
- }
- public static void main(String[] args) throws Exception {
- int res = ToolRunner.run(new Configuration(), new AveragingWithCombiner(), args);
- System.exit(res);
- }
- }
---恢复内容结束---
hadoop1中partition和combiner作用的更多相关文章
- map/reduce之间的shuffle,partition,combiner过程的详解
Shuffle的本意是洗牌.混乱的意思,类似于java中的Collections.shuffle(List)方法,它会随机地打乱参数list里的元素顺序.MapReduce中的Shuffle过程.所谓 ...
- 24、redis中的sentinel的作用?
redis中的sentinel的作用? Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Re ...
- Sql中partition by的使用
partition by关键字是oracle中分析性函数的一部分,它和聚合函数不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录,partition by用于给结果集分 ...
- SQLSERVER中NULL位图的作用
SQLSERVER中NULL位图的作用 首先感谢宋沄剑提供的文章和sqlskill网站:www.sqlskills.com,看下面文章之前请先看一下下面两篇文章 SQL Server误区30日谈-Da ...
- PHP中的header()函数作用
PHP 中 header()函数的作用是给客户端发送头信息. 什么是头信息?这里只作简单解释,详细的自己看http协议.在 HTTP协议中,服务器端的回答(response)内容包括两部分:头信息(h ...
- 浅析python 中__name__ = '__main__' 的作用
引用http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...
- log4net日志在app.config中assembly不起作用
log4net 1.2.15.0日志在app.config中assembly不起作用,必须 1.手动调用方法log4net.Config.XmlConfigurator.Configure()来初始化 ...
- URL中“#” “?” &“”号的作用
URL中"#" "?" &""号的作用 阅读目录 1. # 2. ? 3. & 回到顶部 1. # 10年9月,twit ...
- 【转】浅析python 中__name__ = '__main__' 的作用
原文链接:http://www.jb51.net/article/51892.htm 举例说明解释的非常清楚,应该是看到的类似博文里面最简单的一篇: 这篇文章主要介绍了python 中__name__ ...
随机推荐
- hdu1430魔板(BFS+康托展开)
做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...
- git 设置
系统乱码 项目中的编码统一设置为UTF-8编码. 设置系统的语言设置为 zh_UTF-8,把 export LANG=zh_CN.UTF-8 保存到~/.profile文件里. $ env|grep ...
- OOM-killer 线上设置 +vm +OOM机制
http://blog.csdn.net/tenfyguo/article/details/9409743 http://blog.csdn.net/tenfyguo/article/details/ ...
- HD1285(拓扑排序)
package cn.hncu.dataStruct.search.topSort; import java.util.Scanner; public class Hdu1285 { static S ...
- mysql优化概析
优化从几个方面来说: 表结构设计 适当索引(主键 普通 唯一 全文组合) mysql本身配置 硬件配置 SQL语句优化 存储过程 分表 分区 读写分离 清理垃圾数据
- Linux查看磁盘块大小
首先,使用df命令查看所在磁盘 df -hT 显示: Filesystem Type Size Used Avail Use% Mounted on /dev/vda1 ext4 15G .1G 12 ...
- T-SQL通过触发器创建级联更新·级联删除
create trigger t_table_a on table_a for update,dalete begin if exists(select 1 from inserted) update ...
- Asp.net中前台javascript与后台C#交互
方法一:使用Ajax开发框架,后台方法定义前添加[AjaxPro.AjaxMethod],然后就可以在前台js脚本中调用后台C#函数. 方法二:后台方法声明为public或者protected,然后前 ...
- Android开发环境搭建(2015年8月更新)
1. 下载和安装Android SDK Android的官方站点是http://www.android.com: 登录https://developer.android.com/intl/zh-cn ...
- cognos 10.2.2 搭建网关做负载均衡
最近要设计cognos服务器灾备模式,所以想到了cognos10自带的gateway负载均衡模式,搭建起来还是挺简洁的 设计背景: cognos主服务器:231 cognos灾备服务器:238 gat ...