`

MapReduce中自定义Combiner

 
阅读更多

 以下作为自己的学习记录。

1 MapReduce中数据的整个处理流程。

 

  Map输出数据->key排序并且计算partintion->Map本地所有数据数据Combiner->

shuffle中的自定义排序->自定义分组->reduce中数据汇总

例子:

一、 自定义Combiner使用

1 自定义Combiner

  

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class MyCombiner extends Reducer<Text, LongWritable, Text, LongWritable> {
	
	protected void reduce( Text key, Iterable<LongWritable> values, Context context) throws java.io.IOException, InterruptedException {
		
		// 显示次数表示规约函数被调用了多少次,表示k2有多少个分组
		System.out.println("Combiner输入分组<" + key.toString() + ",N(N>=1)>");
		long count = 0L;
		for (LongWritable value : values) {
			count += value.get();
			// 显示次数表示输入的k2,v2的键值对数量
			System.out.println("Combiner输入键值对<" + key.toString() + "," + value.get() + ">"+this);
		}
		context.write(key, new LongWritable(count));
		// 显示次数表示输出的k2,v2的键值对数量
		System.out.println("Combiner输出键值对<" + key.toString() + "," + count + ">");
	};
}

 2  主类的使用

 

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class MyCombiner extends Reducer<Text, LongWritable, Text, LongWritable> {
	
	protected void reduce( Text key, Iterable<LongWritable> values, Context context) throws java.io.IOException, InterruptedException {
		
		// 显示次数表示规约函数被调用了多少次,表示k2有多少个分组
		System.out.println("Combiner输入分组<" + key.toString() + ",N(N>=1)>");
		long count = 0L;
		for (LongWritable value : values) {
			count += value.get();
			// 显示次数表示输入的k2,v2的键值对数量
			System.out.println("Combiner输入键值对<" + key.toString() + "," + value.get() + ">"+this);
		}
		context.write(key, new LongWritable(count));
		// 显示次数表示输出的k2,v2的键值对数量
		System.out.println("Combiner输出键值对<" + key.toString() + "," + count + ">");
	};
}

 

3 自定义Partition

  

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

 

public class MyPartintioner extends Partitioner<Text, LongWritable>{
 
	/**
	 * key map输出的key
	 * value map 输出的value 
	 */
	@Override
	public int getPartition(Text key, LongWritable value, int numPartitions) {
		
//		   System.out.println("--------enter DefinedPartition flag--------"); 
	        /** 
	        * 注意:这里采用默认的hash分区实现方法 
	        * 根据组合键的第一个值作为分区 
	        * 这里需要说明一下,如果不自定义分区的话,mapreduce框架会根据默认的hash分区方法, 
	        * 将整个组合将相等的分到一个分区中,这样的话显然不是我们要的效果 
	        */
//		   System.out.println(key+ "--------out DefinedPartition flag--------"+ value );
			System.out.println("Partitioner  key:"+key+"  value:"+value+"  "+ ( ( key.hashCode()&Integer.MAX_VALUE)%numPartitions ) +"   "+this);
	       return ( key.hashCode()&Integer.MAX_VALUE)%numPartitions; 
	}
	
}

 

 

 

分享到:
评论

相关推荐

    mapreduce高级特性及shuffle

    1.shuffle机制详细讲解 2.MR案例多文件输出 3.MR案例partition使用 4.MR案例内容去重 5.MR案例敏感词汇过滤 6.MR案例自定义combiner的使用 7.MR案例倒排序索引 8.MR案例简单排序

    MapReduce分布式计算平台编程示例

    2 用户自定义接口 3 2.1 map函数 3 2.2 Reduce函数 3 2.3输入和输出格式 4 2.4 partitioner函数 4 2.5 Combiner函数 4 3 Hadoop MapReduce平台使用 5 3.1 streaming介绍 5 3.2 C语言Map-Reduce程序示例 6 3.2.1计算...

    Hadoop 培训课程(4)MapReduce_2

    Hadoop 培训课程(4)MapReduce_2 ...Combiner和Partitioner编程** 自定义排序和分组编程** 常见的MapReduce算法** ---------------------------加深拓展---------------------- 常见大数据处理方法*

    MapReduce单词统计 hadoop集群

    单词统计的MapReduce源码,统计多个文本数据集,最终输出每个单词的出现次数,可帮功能扩展修改 Map阶段 采集数据 Combiner阶段 合并数据 Reduce阶段 最终处理,进行排序等自定义操作 每个阶段都会打印对应的数据...

    Hadoop大数据实训,求最高温度最低温度实验报告

    (1)统计全球每年的最高气温和最低气温。 (2)MapReduce输出结果包含年份、最高气温、...(4)结合Combiner和自定义数据类型完成全球每年最高气温和最低气温的统计。 (5)应用ToolRunner的使用和Eclipse提交MapReduce任务。

    Hadoop中MapReduce基本案例及代码(四)

    Combiner 就相当于map后多reduce几次。 排序 如果想将mapreduce结果排序,需将排序对象作为键值。 案例:将利润求和后按照顺序排序 数据源 profit.txt 编号 | 姓名 | 收入 | 支出 1 ls 2850 100 2 ls 3566 200 3 ls...

    Hadoop实训求最高温度和最低温度的数据集

    (1)统计全球每年的最高气温和最低气温。 (2)MapReduce输出结果包含年份、最高气温、...(4)结合Combiner和自定义数据类型完成全球每年最高气温和最低气温的统计。 (5)应用ToolRunner的使用和Eclipse提交MapReduce任务。

    Hadoop硬实战 [(美)霍姆斯著][电子工业出版社][2015.01]_PDF电子书下载 带书签目录 高清完整版.rar )

    技术点26 在HDFS、MapReduce、Pig 和Hive 中使用数据压缩 技术点27 在MapReduce、Hive 和Pig 中处理可分割的LZOP 5.3 本章小结 6 诊断和优化性能问题 6.1 衡量MapReduce 和你的环境 6.1.1 提取作业统计...

    Hadoop实战(第2版)

    技术点46 避免reducer 技术点47 过滤和投影技术点48 使用 combiner技术点49 超炫的使用比较器的快速排序6.4.4 减轻倾斜技术点50 收集倾斜数据技术点51 减轻reducer 阶段倾斜6.4.5 在MapReduce 中优化...

Global site tag (gtag.js) - Google Analytics