数据排序
public static class Map extends Mapper<Object, Text, IntWritable, IntWritable>{
//map将输入中的value转化成IntWritable类型,作为输出的key
private static IntWritable data = new IntWritable();
//实现map函数
public void map(Object key,Text value,Context context)throws IOException, InterruptedException{
String line = value.toString();
data.set(Integer.parseInt(line));
context.write(data, new IntWritable(1));
}
}
//reduce将输入中的key复制到输出数据的key上
//然后根据输入的value-list中的元素的个数决定key的输出次数
//用全局linenum来代表key的位次
public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
private static IntWritable linenum = new IntWritable(1);
//实现reduce函数
protected void reduce(IntWritable key, Iterable values,Context context) throws IOException, InterruptedException {
for (IntWritable val : values) {
context.write(linenum, key);
linenum = new IntWritable(linenum.get()+1);
}
}
}