图解
准备工作
1、准备好一个sort.txt文件
代码段
SortMapper.java
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class SortMapper extends Mapper<LongWritable, Text,PairWritable,Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//1、对每一行数据进行拆分,然后封装到PairWritable对象中,作为k2
String[] split = value.toString().split("\t");
PairWritable pairWritable = new PairWritable();
pairWritable.setFirst(split[0]);
pairWritable.setSecond(Integer.parseInt(split[1]));
//2、将k2,v2写入到上下文中
context.write(pairWritable,value);
}
}
SortReducer.java
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class SortReducer extends Reducer<PairWritable, Text,PairWritable, NullWritable>{
@Override
protected void reduce(PairWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
//因为一样的k可能会形成这样的V <a 1,a 1>,所以要遍历一次写一次k,这样就不会漏了
for (Text value : values) {
context.write(key,NullWritable.get());
}
}
}
PairWritable.java
import org.apache.hadoop.io.WritableComparable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class PairWritable implements WritableComparable<PairWritable> {
//first是左边的单词 string a b a b a
//second是右边的单词 int 1 1 9 2 3
private String first;
private int second;
//实现排序规则
@Override
public int compareTo(PairWritable other) {
//先比较first,如果first相同,在比较second
int result = this.first.compareTo(other.first); //单词比较,如果a 大于 b就返回大于0,否则返回小于0,
// 用奥斯卡码计算
//如果first单词相等则比较second的单词
if (result == 0){
//因为second是int值所以可以直接相减返回int值给比较器
return this.second - other.second;
}
//如果result不是0那就返回int值给比较强
return result;
}
//实现序列化
@Override
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeUTF(first);
dataOutput.writeInt(second);
}
//实现反序列化
@Override
public void readFields(DataInput dataInput) throws IOException {
this.first = dataInput.readUTF();
this.second = dataInput.readInt();
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
@Override
public String toString() {
return first + '\t' + second ;
}
}
结果
排好了