MapReduce编程实例:

MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析

MapReduce编程实例(二),计算学生平均成绩

MapReduce编程实例(三),数据去重

MapReduce编程实例(四),排序

MapReduce编程实例(五),MapReduce实现单表关联

 

排序,比较简单,上代码,代码中有注释,欢迎交流。

总体是利用MapReduce本身对Key进行排序的特性和按key值有序的分配到不同的partition。Mapreduce默认会对每个reduce按text类型key按字母顺序排序,对intwritable类型按大小进行排序。

    1. package com.t.hadoop;
    2. import java.io.IOException;
    3. import org.apache.hadoop.conf.Configuration;
    4. import org.apache.hadoop.fs.Path;
    5. import org.apache.hadoop.io.IntWritable;
    6. import org.apache.hadoop.io.Text;
    7. import org.apache.hadoop.mapreduce.Job;
    8. import org.apache.hadoop.mapreduce.Mapper;
    9. import org.apache.hadoop.mapreduce.Partitioner;
    10. import org.apache.hadoop.mapreduce.Reducer;
    11. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    12. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    13. import org.apache.hadoop.util.GenericOptionsParser;
    14. /**
    15. * 排序
    16. * 利用MapReduce默认的对Key进行排序
    17. * 继承Partitioner类,重写getPartition使Mapper结果整体有序分到相应的Partition,输入到Reduce分别排序。
    18. * 利用全局变量统计位置
    19. * @author daT dev.tao@gmail.com
    20. *
    21. */
    22. public class Sort {
    23. public static class SortMapper extends Mapper<Object, Text, IntWritable, IntWritable>{
    24. //直接输出key,value,key为需要排序的值,value任意
    25. @Override
    26. protected void map(Object key, Text value,
    27. Context context)throws IOException, InterruptedException {
    28. System.out.println("Key: "+key+"  "+"Value: "+value);
    29. context.write(new IntWritable(Integer.valueOf(value.toString())),new IntWritable(1));
    30. }
    31. }
    32. public static class SortReducer extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable>{
    33. public static IntWritable lineNum = new IntWritable(1);//记录该数据的位置
    34. //查询value的个数,有多少个就输出多少个Key值。
    35. @Override
    36. protected void reduce(IntWritable key, Iterable<IntWritable> value,
    37. Context context) throws IOException, InterruptedException {
    38. System.out.println("lineNum: "+lineNum);
    39. for(IntWritable i:value){
    40. context.write(lineNum, key);
    41. }
    42. lineNum = new IntWritable(lineNum.get()+1);
    43. }
    44. }
    45. public static class SortPartitioner extends Partitioner<IntWritable, IntWritable>{
    46. //根据key对数据进行分派
    47. @Override
    48. public int getPartition(IntWritable key, IntWritable value, int partitionNum) {
    49. System.out.println("partitionNum: "+partitionNum);
    50. int maxnum = 23492;//输入的最大值,自己定义的。mapreduce 自带的有采样算法和partition的实现可以用,此例没有用。
    51. int bound = maxnum/partitionNum;
    52. int keyNum = key.get();
    53. for(int i=0;i<partitionNum;i++){
    54. if(keyNum>bound*i&&keyNum<=bound*(i+1)){
    55. return i;
    56. }
    57. }
    58. return -1;
    59. }
    60. }
    61. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
    62. Configuration conf = new Configuration();
    63. String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    64. if(otherArgs.length<2){
    65. System.out.println("input parameters errors");
    66. System.exit(2);
    67. }
    68. Job job= new Job(conf);
    69. job.setJarByClass(Sort.class);
    70. job.setMapperClass(SortMapper.class);
    71. job.setPartitionerClass(SortPartitioner.class);//此例不需要combiner,需要设置Partitioner
    72. job.setReducerClass(SortReducer.class);
    73. job.setOutputKeyClass(IntWritable.class);
    74. job.setOutputValueClass(IntWritable.class);
    75. FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
    76. FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
    77. System.exit(job.waitForCompletion(true)?0:1);
    78. }
    79. }

MapReduce编程实例4的更多相关文章

  1. MapReduce编程实例6

    前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...

  2. MapReduce编程实例5

    前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...

  3. MapReduce编程实例3

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  4. MapReduce编程实例2

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  5. 三、MapReduce编程实例

    前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...

  6. hadoop2.2编程:使用MapReduce编程实例(转)

    原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...

  7. MapReduce编程实例

    MapReduce常见编程实例集锦. WordCount单词统计 数据去重 倒排索引 1. WordCount单词统计 (1) 输入输出 输入数据: file1.csv内容 hellod world ...

  8. hadoop之mapreduce编程实例(系统日志初步清洗过滤处理)

    刚刚开始接触hadoop的时候,总觉得必须要先安装hadoop集群才能开始学习MR编程,其实并不用这样,当然如果你有条件有机器那最好是自己安装配置一个hadoop集群,这样你会更容易理解其工作原理.我 ...

  9. Hadoop--mapreduce编程实例1

    前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...

随机推荐

  1. ylbtech-LanguageSamples-OperatorOverLoading(运算符重载)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-OperatorOverLoading(运算符重载) 1.A,示例(Sample) 返回 ...

  2. maven工程小红叉处理方法

    搞了个Maven工程在Eclipse上,刚开始说JDK版本不对,编译的时候老报错误,很容易搞明白, 本地JDK版本为1.7.0_79: diamond operator is not supporte ...

  3. Caused by: org.apache.ibatis.reflection.ReflectionException我碰到的情况,原因不唯一

    映射文件: <select id="selectKeyByUserId"  resultMap="Xxx">        <![CDATA[ ...

  4. 开源 SHOPNC B2B2C结算营运版 wap IM客服 API 手机app 短信通知

    源码我们这里简单的测试了下,具体的请自行下载测试.这套源码官方售价很高,在这里完全免费分享,无任何限制,安装也简单. 源码下载后请自行检测安全,在使用过程中发生的任何问题请自行处理,本站不承担任何责任 ...

  5. [React] Detect user activity with a custom useIdle React Hook

    If the user hasn't used your application for a few minutes, you may want to log them out of the appl ...

  6. Quartz JobStore管理Job

    Quartz提供了RAMJobStore和JDBC JobStore两种方式用来Job,RAMJobStore将Job任务存入内存中,速度快:JobStore采用数据库的方式管理中,本文介绍JobSt ...

  7. linux中升级jdk的方法

    先查看java的安装情况: # rpm -qa|grep java 卸载: rpm -e --nodeps ******* 然后再安装就可以了

  8. c# 句柄数不断攀升的解决方案

    句柄只是用来标识应用程序中的不同对象和同类中的不同的实例的一个数字,通常情况下,句柄值对普通用户毫无用处,但是句柄数量却可以间接反映出一个程序里产生的对象实例的多少.句柄数越多,代表程序里new 出来 ...

  9. CosmosEngine - Unity3D /2D 轻量级游戏开发框架

    CosmosEngine https://github.com/mr-kelly/CosmosEngine 快速入门 简介 特性 约定 整体架构图 使用经验 工作流 未来功能 快速入门 1.将NGUI ...

  10. SQL数据库有阻塞就自动发邮件警报

    1.建查询是否有阻塞的视图 create view [dbo].[VW_WaitingCount] as SELECT s.session_id, r.blocking_session_id, s.h ...