前言部分:

在Map阶段,使用job.setInputFormatClass定义的InputFormat将输入的数据集分割成小数据块splites,同时InputFormat提供一个RecordReder的实现。本实验中使用的是TextInputFormat,他提供的RecordReder会将文本的字节偏移量作为key,这一行的文本作为value。这就是自定义Map的输入是<LongWritable, Text>的原因。然后调用自定义Map的map方法,将一个个<LongWritable, Text>键值对输入给Map的map方法。注意输出应该符合自定义Map中定义的输出<IntPair, IntWritable>。最终是生成一个List<IntPair, IntWritable>。在map阶段的最后,会先调用job.setPartitionerClass对这个List进行分区,每个分区映射到一个reducer。每个分区内又调用job.setSortComparatorClass设置的key比较函数类排序。可以看到,这本身就是一个二次排序。 如果没有通过job.setSortComparatorClass设置key比较函数类,则可以使用key实现的compareTo方法进行排序。 在本实验中,就使用了IntPair实现的compareTo方法。

在Reduce阶段,reducer接收到所有映射到这个reducer的map输出后,也是会调用job.setSortComparatorClass设置的key比较函数类对所有数据对排序。然后开始构造一个key对应的value迭代器。这时就要用到分组,使用job.setGroupingComparatorClass设置的分组函数类。只要这个比较器比较的两个key相同,他们就属于同一个组,它们的value放在一个value迭代器,而这个迭代器的key使用属于同一个组的所有key的第一个key。最后就是进入Reducer的reduce方法,reduce方法的输入是所有的(key和它的value迭代器)。同样注意输入与输出的类型必须与自定义的Reducer中声明的一致。

操作环境:

Centos 7  #安装了Hadoop集群

jdk 1.8

hadoop 3.2.0

IDEA 2019

操作场景:

在电商网站中,用户进入页面浏览商品时会产生访问日志,也就是浏览痕迹即点击次数,含有(goods_id, click_num)两个字段

  1. goods_id click_num
  2. 1010037 100
  3. 1010102 100
  4. 1010152 97
  5. 1010178 96
  6. 1010280 104
  7. 1010320 103
  8. 1010510 104
  9. 1010603 96
  10. 1010637 97

要求编写MapReduce代码,功能为根据点击次数进行降序,在根据商品信息进行升序,并输出所有商品

输出结果:

  1. 点击次数 商品id
  2. ------------------------------------------------
  3. 104 1010280
  4. 104 1010510
  5. ------------------------------------------------
  6. 103 1010320
  7. ------------------------------------------------
  8. 100 1010037
  9. 100 1010102
  10. ------------------------------------------------
  11. 97 1010152
  12. 97 1010637
  13. ------------------------------------------------
  14. 96 1010178
  15. 96 1010603

将提前处理好的数据集导入到hdfs中,将hdfs目标文件夹修改权限,否则会遇到无法修改文件信息的情况。

  1. hadoop fs -mkdir -p /mymapreduce8/in
    hadoop fs -put /data/mapreduce8/goods_visit2 /mymapreduce8/in

    hadoop fs -chmod 777 /mapreduce8/

在IDEA创建Java工程,将工程所需所有jar包全部导入

编写Java代码:

二次排序:在mapreduce中,所有的key是需要被比较和排序的,并且是二次,先根据partitioner,再根据大小。而本例中也是要比较两次。先按照第一字段排序,然后在第一字段相同时按照第二字段排序。根据这一点,我们可以构造一个复合类IntPair,他有两个字段,先利用分区对第一字段排序,再利用分区内的比较对第二字段排序。Java代码主要分为四部分:自定义key,自定义分区函数类,map部分,reduce部分。

自定义key的代码:

  1. public static class IntPair implements WritableComparable<IntPair>{
  2.  
  3. //第一个成员变量
  4. int first;
  5. //第二个成员变量
  6. int second;
  7. //get、set方法
  8. public void set(int left, int right){
  9. first = left;
  10. second = right;
  11. }
  12. public int getFirst(){
  13. return first;
  14. }
  15. public int getSecond(){
  16. return second;
  17. }
  18. //key的比较
  19. public int compareTo(IntPair o) {
  20. if (first != o.first){
  21. return first < o.first ? 1 : -1;
  22. }else if (second != o.second){
  23. return second < o.second ? -1 : 1;
  24. }else {
  25. return 0;
  26. }
  27. }
  28.  
  29. //序列化,将IntPair转化成使用流传送的二进制
  30. public void write(DataOutput out) throws IOException {
  31. out.writeInt(first);
  32. out.writeInt(second);
  33. }
  34.  
  35. //反序列化,从流中的二进制转换成IntPair
  36. public void readFields(DataInput in) throws IOException {
  37. first = in.readInt();
  38. second = in.readInt();
  39. }
  40. public int hashCode(){
  41. return first * 157 + second;
  42. }
  43. public boolean equals(Object right){
  44. if (right == null){
  45. return false;
  46. }
  47. if (this == right) {
  48. return true;
  49. }
  50. if (right instanceof IntPair){
  51. IntPair r = (IntPair) right;
  52. return r.first == first && r.second == second;
  53. }else {
  54. return false;
  55. }
  56. }
  57. }

所有自定义的key应该实现接口WritableComparable,因为是可序列的并且可比较的,并重载方法。该类中包含以下几种方法:1.反序列化,从流中的二进制转换成IntPair 方法为public void readFields(DataInput in) throws IOException 2.序列化,将IntPair转化成使用流传送的二进制 方法为public void write(DataOutput out)3. key的比较 public int compareTo(IntPair o) 另外新定义的类应该重写的两个方法 public int hashCode() 和public boolean equals(Object right) 。

  1. public static class FirstPartitioner extends Partitioner<IntPair, IntWritable>{
  2. @Override
  3. public int getPartition(IntPair intPair, IntWritable intWritable, int numPartitions) {
  4. return Math.abs(intPair.getFirst() * 127) % numPartitions;
  5. }
  6. }

对key进行分区,根据自定义key中first乘以127取绝对值在对numPartions取余来进行分区。这主要是为实现第一次排序。

分组函数类代码

  1. public static class GroupingComparator extends WritableComparator {
  2. protected GroupingComparator() {
  3. super(IntPair.class, true);
  4. }
  5. @Override
  6. public int compare(WritableComparable w1, WritableComparable w2) {
  7. IntPair ip1 = (IntPair) w1;
  8. IntPair ip2 = (IntPair) w2;
  9. int l = ip1.getFirst();
  10. int r = ip2.getFirst();
  11. return l == r ? 0 : (l < r ? -1 : 1);
  12. }
  13. }

分组函数类。在reduce阶段,构造一个key对应的value迭代器的时候,只要first相同就属于同一个组,放在一个value迭代器。这是一个比较器,需要继承WritableComparator。

map代码:

  1. public static class Map extends Mapper<LongWritable, Text, IntPair, IntWritable> {
  2. private final IntPair intkey = new IntPair();
  3. private final IntWritable intvalue = new IntWritable();
  4. public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  5. String line = value.toString();
  6. StringTokenizer tokenizer = new StringTokenizer(line);
  7. int left = 0;
  8. int right = 0;
  9. if (tokenizer.hasMoreTokens()) {
  10. left = Integer.parseInt(tokenizer.nextToken());
  11. if (tokenizer.hasMoreTokens()) {
  12. right = Integer.parseInt(tokenizer.nextToken());
  13. }
  14. intkey.set(right, left);
  15. intvalue.set(left);
  16. context.write(intkey, intvalue);
  17. }
  18. }
  19. }

在map阶段,使用job.setInputFormatClass定义的InputFormat将输入的数据集分割成小数据块splites,同时InputFormat提供一个RecordReder的实现。本例子中使用的是TextInputFormat,他提供的RecordReder会将文本的一行的行号作为key,这一行的文本作为value。这就是自定义Map的输入是<LongWritable, Text>的原因。然后调用自定义Map的map方法,将一个个<LongWritable, Text>键值对输入给Map的map方法。注意输出应该符合自定义Map中定义的输出<IntPair, IntWritable>。最终是生成一个List<IntPair, IntWritable>。在map阶段的最后,会先调用job.setPartitionerClass对这个List进行分区,每个分区映射到一个reducer。每个分区内又调用job.setSortComparatorClass设置的key比较函数类排序。可以看到,这本身就是一个二次排序。如果没有通过job.setSortComparatorClass设置key比较函数类,则使用key实现compareTo方法。在本例子中,使用了IntPair实现compareTo方法。

Reduce代码:

  1. public static class Reduce extends Reducer<IntPair, IntWritable, Text, IntWritable> {
  2. private final Text left = new Text();
  3. private static final Text SEPARATOR = new Text("------------------------------------------------");
  4.  
  5. public void reduce(IntPair key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
  6. context.write(SEPARATOR, null);
  7. left.set(Integer.toString(key.getFirst()));
  8. System.out.println(left);
  9. for (IntWritable val : values) {
  10. context.write(left, val);
  11. }
  12. }
  13. }

在reduce阶段,reducer接收到所有映射到这个reducer的map输出后,也是会调用job.setSortComparatorClass设置的key比较函数类对所有数据对排序。然后开始构造一个key对应的value迭代器。这时就要用到分组,使用job.setGroupingComparatorClass设置的分组函数类。只要这个比较器比较的两个key相同,他们就属于同一个组,它们的value放在一个value迭代器,而这个迭代器的key使用属于同一个组的所有key的第一个key。最后就是进入Reducer的reduce方法,reduce方法的输入是所有的key和它的value迭代器。同样注意输入与输出的类型必须与自定义的Reducer中声明的一致。

完整代码:

  1. package mapreduce;
  2. import java.io.DataInput;
  3. import java.io.DataOutput;
  4. import java.io.IOException;
  5. import java.util.StringTokenizer;
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hadoop.fs.Path;
  8. import org.apache.hadoop.io.IntWritable;
  9. import org.apache.hadoop.io.LongWritable;
  10. import org.apache.hadoop.io.Text;
  11. import org.apache.hadoop.io.WritableComparable;
  12. import org.apache.hadoop.io.WritableComparator;
  13. import org.apache.hadoop.mapreduce.Job;
  14. import org.apache.hadoop.mapreduce.Mapper;
  15. import org.apache.hadoop.mapreduce.Partitioner;
  16. import org.apache.hadoop.mapreduce.Reducer;
  17. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  18. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  19. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  20. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  21. public class SecondarySort {
  22. public static class IntPair implements WritableComparable<IntPair>{
  23.  
  24. //第一个成员变量
  25. int first;
  26. //第二个成员变量
  27. int second;
  28. //get、set方法
  29. public void set(int left, int right){
  30. first = left;
  31. second = right;
  32. }
  33. public int getFirst(){
  34. return first;
  35. }
  36. public int getSecond(){
  37. return second;
  38. }
  39. //key的比较
  40. public int compareTo(IntPair o) {
  41. if (first != o.first){
  42. return first < o.first ? 1 : -1;
  43. }else if (second != o.second){
  44. return second < o.second ? -1 : 1;
  45. }else {
  46. return 0;
  47. }
  48. }
  49.  
  50. //序列化,将IntPair转化成使用流传送的二进制
  51. public void write(DataOutput out) throws IOException {
  52. out.writeInt(first);
  53. out.writeInt(second);
  54. }
  55.  
  56. //反序列化,从流中的二进制转换成IntPair
  57. public void readFields(DataInput in) throws IOException {
  58. first = in.readInt();
  59. second = in.readInt();
  60. }
  61. public int hashCode(){
  62. return first * 157 + second;
  63. }
  64. public boolean equals(Object right){
  65. if (right == null){
  66. return false;
  67. }
  68. if (this == right) {
  69. return true;
  70. }
  71. if (right instanceof IntPair){
  72. IntPair r = (IntPair) right;
  73. return r.first == first && r.second == second;
  74. }else {
  75. return false;
  76. }
  77. }
  78. }
  79. public static class FirstPartitioner extends Partitioner<IntPair, IntWritable>{
  80. @Override
  81. public int getPartition(IntPair intPair, IntWritable intWritable, int numPartitions) {
  82. return Math.abs(intPair.getFirst() * 127) % numPartitions;
  83. }
  84. }
  85. public static class GroupingComparator extends WritableComparator {
  86. protected GroupingComparator() {
  87. super(IntPair.class, true);
  88. }
  89. @Override
  90. public int compare(WritableComparable w1, WritableComparable w2) {
  91. IntPair ip1 = (IntPair) w1;
  92. IntPair ip2 = (IntPair) w2;
  93. int l = ip1.getFirst();
  94. int r = ip2.getFirst();
  95. return l == r ? 0 : (l < r ? -1 : 1);
  96. }
  97. }
  98. public static class Map extends Mapper<LongWritable, Text, IntPair, IntWritable> {
  99. private final IntPair intkey = new IntPair();
  100. private final IntWritable intvalue = new IntWritable();
  101. public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  102. String line = value.toString();
  103. StringTokenizer tokenizer = new StringTokenizer(line);
  104. int left = 0;
  105. int right = 0;
  106. if (tokenizer.hasMoreTokens()) {
  107. left = Integer.parseInt(tokenizer.nextToken());
  108. if (tokenizer.hasMoreTokens()) {
  109. right = Integer.parseInt(tokenizer.nextToken());
  110. }
  111. intkey.set(right, left);
  112. intvalue.set(left);
  113. context.write(intkey, intvalue);
  114. }
  115. }
  116. }
  117.  
  118. public static class Reduce extends Reducer<IntPair, IntWritable, Text, IntWritable> {
  119. private final Text left = new Text();
  120. private static final Text SEPARATOR = new Text("------------------------------------------------");
  121.  
  122. public void reduce(IntPair key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
  123. context.write(SEPARATOR, null);
  124. left.set(Integer.toString(key.getFirst()));
  125. System.out.println(left);
  126. for (IntWritable val : values) {
  127. context.write(left, val);
  128. }
  129. }
  130. }
  131. public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
  132.  
  133. Configuration conf = new Configuration();
  134. Job job = new Job(conf, "secondarysort");
  135. job.setJarByClass(SecondarySort.class);
  136. job.setMapperClass(Map.class);
  137. job.setReducerClass(Reduce.class);
  138. job.setPartitionerClass(FirstPartitioner.class);
  139.  
  140. job.setGroupingComparatorClass(GroupingComparator.class);
  141. job.setMapOutputKeyClass(IntPair.class);
  142.  
  143. job.setMapOutputValueClass(IntWritable.class);
  144.  
  145. job.setOutputKeyClass(Text.class);
  146.  
  147. job.setOutputValueClass(IntWritable.class);
  148.  
  149. job.setInputFormatClass(TextInputFormat.class);
  150.  
  151. job.setOutputFormatClass(TextOutputFormat.class);
  152. String[] otherArgs=new String[2];
  153. otherArgs[0]="hdfs://172.18.74.137:9000/mapreduce8/in/goods_visit2";
  154. otherArgs[1]="hdfs://172.18.74.137:9000/mapreduce8/out";
  155.  
  156. FileInputFormat.setInputPaths(job, new Path(otherArgs[0]));
  157.  
  158. FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  159.  
  160. System.exit(job.waitForCompletion(true) ? 0 : 1);
  161. }
  162. }

点击项目运行,查看结果:

Mapreduce实例--二次排序的更多相关文章

  1. MapReduce自定义二次排序流程

    每一条记录开始是进入到map函数进行处理,处理完了之后立马就入自定义分区函数中对其进行分区,当所有输入数据经过map函数和分区函数处理完之后,就调用自定义二次排序函数对其进行排序. MapReduce ...

  2. MapReduce的二次排序

    附录之前总结的一个例子: http://www.cnblogs.com/DreamDrive/p/7398455.html 另外两个有价值的博文: http://www.cnblogs.com/xux ...

  3. MapReduce二次排序

    默认情况下,Map 输出的结果会对 Key 进行默认的排序,但是有时候需要对 Key 排序的同时再对 Value 进行排序,这时候就要用到二次排序了.下面让我们来介绍一下什么是二次排序. 二次排序原理 ...

  4. Hadoop MapReduce 二次排序原理及其应用

    关于二次排序主要涉及到这么几个东西: 在0.20.0 以前使用的是 setPartitionerClass setOutputkeyComparatorClass setOutputValueGrou ...

  5. MapReduce 二次排序

    默认情况下,Map 输出的结果会对 Key 进行默认的排序,但是有时候需要对 Key 排序的同时再对 Value 进行排序,这时候就要用到二次排序了.下面让我们来介绍一下什么是二次排序. 二次排序原理 ...

  6. Hadoop Mapreduce分区、分组、二次排序过程详解[转]

    原文地址:Hadoop Mapreduce分区.分组.二次排序过程详解[转]作者: 徐海蛟 教学用途 1.MapReduce中数据流动   (1)最简单的过程:  map - reduce   (2) ...

  7. Hadoop.2.x_高级应用_二次排序及MapReduce端join

    一.对于二次排序案例部分理解 1. 分析需求(首先对第一个字段排序,然后在对第二个字段排序) 杂乱的原始数据 排序完成的数据 a,1 a,1 b,1 a,2 a,2 [排序] a,100 b,6 == ...

  8. Hadoop学习笔记: MapReduce二次排序

    本文给出一个实现MapReduce二次排序的例子 package SortTest; import java.io.DataInput; import java.io.DataOutput; impo ...

  9. (转)MapReduce二次排序

    一.概述 MapReduce框架对处理结果的输出会根据key值进行默认的排序,这个默认排序可以满足一部分需求,但是也是十分有限的.在我们实际的需求当中,往往有要对reduce输出结果进行二次排序的需求 ...

随机推荐

  1. 解决IDEA更新为最新的2020.3版后,右键运行居然没有以xml形式运行的Run显示

    一.前言 个人一直喜欢用IDEA最新版,结果更新后,发现TestNg批量执行,选中testng.xml右键没Run,如下图: 刚开始以为是配置错误呢,下载了2018.2版本的IDEA,还能正常运行,于 ...

  2. golang拾遗:嵌入类型

    这里是golang拾遗系列的第三篇,前两篇可以点击此处链接跳转: golang拾遗:为什么我们需要泛型 golang拾遗:指针和接口 今天我们要讨论的是golang中的嵌入类型(embedding t ...

  3. 第三十七章、PyQt输入部件:QAbstractSlider派生类QScrollBar滚动条、QSlider滑动条、QDial刻度盘功能介绍

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.引言 Designer中的输入部件Horizo ...

  4. 第11.22节 Python 中re模块的字符串分割器:split函数

    一. 引言 在<第11.2节 Python 正则表达式支持函数概览>介绍了re模块的主要函数,在<第11.3节 Python正则表达式搜索支持函数search.match.fullm ...

  5. PyQt(Python+Qt)学习随笔:Designer中的QDialogButtonBox的StandardButtons标准按钮

    在Qt Designer中,可以在界面中使用QDialogButtonBox来配置一组按钮进行操作,Qt中为QDialogButtonBox定义了一组常用的标准按钮,可以在Designer中直接在St ...

  6. 【C/C++】C和C++11之enum枚举的使用细节

    作者:李春港 出处:https://www.cnblogs.com/lcgbk/p/14101271.html 目录 一.前言 二.C中的枚举(enum) 2.1 C中枚举的大小 2.2 C中枚举的取 ...

  7. Java基础学习之流程控制语句(5)

    目录 1.顺序结构 2.选择结构 2.1.if else结构 2.2.switch case结构 3.循环结构 3.1.while结构 3.2.do while结构 3.3.for结构 3.3.1.普 ...

  8. 添加和读取Resources嵌入资源文件(例如.dll和.ssk文件)

    前言:有些程序运行的时候,可能调用外部的dll,用户使用时可能会不小心丢失这些dll,导致程序无法正常运行,因此可以考虑将这些dll嵌入到资源中,启动时自动释放.对于托管的dll,我们可以用打包软件合 ...

  9. Redis存储对象(序列化和反序列化)

    代码以及实例: package com.hp.test; import redis.clients.jedis.Jedis; import java.io.*; public class Test3 ...

  10. 浏览器小程序(Browser Applet)闪亮登场

    2017 年 1 月 9 日,微信小程序横空出世.随后,支付宝小程序.今日头条小程序.百度智能小程序.360小程序等纷纷推出,自此国内软件功能扩展领域进入到了小程序时代,小程序为丰富其宿主软件的功能和 ...