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. }

最新文章

  1. 探讨webapp的SEO难题(上)
  2. Python笔记总结week8
  3. sersync
  4. ccc this 指针
  5. android studio1.0 for Mac环境搭建与demo运行(手动下载gradle,科学上google) 转载
  6. PHP7正式版测试,性能惊艳!
  7. 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问
  8. js插件动态加载js、css解决方案
  9. Win7+vs2010下安装boost_1_46_1库
  10. c++界面设计皮肤工具
  11. Vue H5 History 部署IIS上404问题
  12. JWT是什么鬼
  13. linux服务基础(一)之CentOS6编译安装httpd2.4
  14. 【css】正确的让文本换行
  15. PostgreSQL安装和创建用户和创建数据库
  16. 第一本docker书,,持续更新中
  17. Android GetMethodID 函数的说明
  18. php 截取字符串指定长度
  19. vim segment fault when i upgrade to macOS Mojave 103_PollServerReady
  20. jdk1.6 eclipse kepler 中安装jda

热门文章

  1. 寻找i*j=m的个数
  2. ispy 编译笔记
  3. jstat的用法
  4. 爪哇国新游记之十三----XML文件读写
  5. AngularJS监听DOM加载完毕
  6. ES6 对象扩展
  7. IT痴汉的工作现状36-做好准备再上路
  8. 学会Git玩转Github笔记(三)—— Github Pages 搭建个人网站
  9. 使用swagger实现在线api文档自动生成 在线测试api接口
  10. 使用Parallel实现简单的并行操作