MapReduce编程实例4
MapReduce编程实例:
MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析
MapReduce编程实例(五),MapReduce实现单表关联
排序,比较简单,上代码,代码中有注释,欢迎交流。
总体是利用MapReduce本身对Key进行排序的特性和按key值有序的分配到不同的partition。Mapreduce默认会对每个reduce按text类型key按字母顺序排序,对intwritable类型按大小进行排序。
- package com.t.hadoop;
- import java.io.IOException;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.Text;
- 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.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.util.GenericOptionsParser;
- /**
- * 排序
- * 利用MapReduce默认的对Key进行排序
- * 继承Partitioner类,重写getPartition使Mapper结果整体有序分到相应的Partition,输入到Reduce分别排序。
- * 利用全局变量统计位置
- * @author daT dev.tao@gmail.com
- *
- */
- public class Sort {
- public static class SortMapper extends Mapper<Object, Text, IntWritable, IntWritable>{
- //直接输出key,value,key为需要排序的值,value任意
- @Override
- protected void map(Object key, Text value,
- Context context)throws IOException, InterruptedException {
- System.out.println("Key: "+key+" "+"Value: "+value);
- context.write(new IntWritable(Integer.valueOf(value.toString())),new IntWritable(1));
- }
- }
- public static class SortReducer extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable>{
- public static IntWritable lineNum = new IntWritable(1);//记录该数据的位置
- //查询value的个数,有多少个就输出多少个Key值。
- @Override
- protected void reduce(IntWritable key, Iterable<IntWritable> value,
- Context context) throws IOException, InterruptedException {
- System.out.println("lineNum: "+lineNum);
- for(IntWritable i:value){
- context.write(lineNum, key);
- }
- lineNum = new IntWritable(lineNum.get()+1);
- }
- }
- public static class SortPartitioner extends Partitioner<IntWritable, IntWritable>{
- //根据key对数据进行分派
- @Override
- public int getPartition(IntWritable key, IntWritable value, int partitionNum) {
- System.out.println("partitionNum: "+partitionNum);
- int maxnum = 23492;//输入的最大值,自己定义的。mapreduce 自带的有采样算法和partition的实现可以用,此例没有用。
- int bound = maxnum/partitionNum;
- int keyNum = key.get();
- for(int i=0;i<partitionNum;i++){
- if(keyNum>bound*i&&keyNum<=bound*(i+1)){
- return i;
- }
- }
- return -1;
- }
- }
- public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
- Configuration conf = new Configuration();
- String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
- if(otherArgs.length<2){
- System.out.println("input parameters errors");
- System.exit(2);
- }
- Job job= new Job(conf);
- job.setJarByClass(Sort.class);
- job.setMapperClass(SortMapper.class);
- job.setPartitionerClass(SortPartitioner.class);//此例不需要combiner,需要设置Partitioner
- job.setReducerClass(SortReducer.class);
- job.setOutputKeyClass(IntWritable.class);
- job.setOutputValueClass(IntWritable.class);
- FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
- FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
- System.exit(job.waitForCompletion(true)?0:1);
- }
- }
MapReduce编程实例4的更多相关文章
- MapReduce编程实例6
前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...
- MapReduce编程实例5
前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...
- MapReduce编程实例3
MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...
- MapReduce编程实例2
MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...
- 三、MapReduce编程实例
前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...
- hadoop2.2编程:使用MapReduce编程实例(转)
原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...
- MapReduce编程实例
MapReduce常见编程实例集锦. WordCount单词统计 数据去重 倒排索引 1. WordCount单词统计 (1) 输入输出 输入数据: file1.csv内容 hellod world ...
- hadoop之mapreduce编程实例(系统日志初步清洗过滤处理)
刚刚开始接触hadoop的时候,总觉得必须要先安装hadoop集群才能开始学习MR编程,其实并不用这样,当然如果你有条件有机器那最好是自己安装配置一个hadoop集群,这样你会更容易理解其工作原理.我 ...
- Hadoop--mapreduce编程实例1
前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...
随机推荐
- 天台人满为患,不如来看下这个Ramnit蠕虫DesktopLayer.exe分析
今年的世界杯越来越看不懂,想去天台吹吹风都不一定有位置,心凉了,事儿还得做,先从网上抓个可疑样本压压惊!上手分析才发现并没有我想得那么简单…… 一.基本信息 MD5 ff5e1f27193ce51ee ...
- [转载]《Delphi 版 everything、光速搜索代码》 关于获取文件全路径 GetFullFileName 函数的优化
Delphi 版 everything.光速搜索代码>,文章中关于获取文件全路径的函数:GetFullFileName,有一个地方值得优化. 就是有多个文件,它们可能属于同一个目录. 譬如 Sy ...
- FPGA作为从机与STM32进行SPI协议通信---Verilog实现
一.SPI协议简要介绍 SPI,是英语Serial Peripheral Interface的缩写,顾名思义就是串行外围设备接口.SPI,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用 ...
- 判断一组checkbox中是否有被选中的
if ($(":checkbox[name=subcheck]:checked").size() == 0) { alert("请至少选择一条记录进行删除操作!" ...
- Item 33: 避免覆盖(hiding)“通过继承得到的名字”
莎士比亚有一个关于名字的说法."What's in a name?" 他问道,"A rose by any other name would smell as sweet ...
- FreeMarker最简单的例子(1)
一.通过文件来创建模版对象,并执行插值操作 文件结构为: Test.Java中的代码为: package com.abc; import freemarker.template.Configurati ...
- 《深入理解Java虚拟机》笔记5
Java虚拟机可以执行的语言并不是只有Java语言,比如jython也可以 运行在Java虚拟机上.不明白字节码之前觉得挺疑惑,为什么和Java 完全不同语法的程序语言也可以运行在虚拟机上呢? 不得不 ...
- 算法笔记_075:蓝桥杯练习 最短路(Java)
目录 1 问题描述 2 解决方案 2.1 floyd算法解决 2.2 spfa算法解决 1 问题描述 问题描述 给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证没有负环).请你计算从 ...
- EAS开发
WAFII中的 数据获取与传输 首先看实例代码: DataAction:function(){ //获取选中所有列的id var selectedIds = waf("#grid" ...
- hibernate 多对多双向关联
package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import javax.persistenc ...