一、背景
排序对于MR来说是个核心内容,如何做好排序十分的重要,这几天写了一些,总结一下,以供以后读阅。
二、准备
1、hadoop版本是0.20.2
2、输入的数据格式(这个很重要,看清楚格式),名称是secondary.txt:
abc 123 acb 124 cbd 523 abc 234 nbc 563 fds 235 khi 234 cbd 675 fds 971 hka 862 ubd 621 khi 123 fds 321
仔细看下,数据文件第一列是字母,第二列是数字,我要做的就是结合这组数据进行一些排序的测试。
3、代码框架,因为接下来的测试改动都是针对部分代码的修改,框架的代码是不会改变的,所以先把主要代码贴在这里。
代码分为2部分:自定义的key和主框架代码(注意看下红色部分)。先贴上主框架代码:
MyGrouping.java
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.io.WritableComparator; 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; import com.run.lenged.business.TextPair; public class MyGrouping { /** * Map * * @author Administrator */ public static class MyGroupingMap extends Mapper<LongWritable, Text, TextPair, Text> { protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, TextPair, Text>.Context context) throws java.io.IOException, InterruptedException { String arr[] = value.toString().split("/t"); if (arr.length != 2) { return; } TextPair tp = new TextPair(); tp.set(new Text(arr[0]), new Text(arr[1])); context.write(tp