题目:
数字排序并加序号源数据:
2
32
654
32
15
756
65223
5956
22
650
92
26
54
6
最张结果:
1 2
2 6
3 15
4 22
5 26
6 32
7 32
8 54
9 92
10 650
11 654
12 756
13 5956
14 65223
一定要考虑 当数据量一大的时候, 你的实现思路能否使用。
解题思路:当有多个无序文件需要进行排序,并且在数字前面加入索引,首先考虑使用MapReduce的默认排序方法,在map里面进行排序,然后设置全局计数变量记录索引值,通过设置全局临时变量记录上个值的大小,如果当前值大于临时值,计数变量加1,否则不变,然后进行输出
代码如下:
/**
* @author: lpj
* @date: 2018年3月16日 下午7:16:47
* @Description:
*/
package lpj.reduceWork;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
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.security.token.Token.PrivateToken;
public class BigNumFileSortMR {
private static int countnum = 0;
private static int temNum = 0;
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
// conf.addResource("hdfs-site.xml");//使用配置文件
// System.setProperty("HADOOP_USER_NAME", "hadoop");//使用集群
FileSystem fs = FileSystem.get(conf);//默认使用本地
Job job = Job.getInstance(conf);
job.setJarByClass(BigNumFileSortMR.class);
job.setMapperClass(BigNumFileSortMR_Mapper.class);
job.setReducerClass(BigNumFileSortMR_Reducer.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(NullWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
Path inputPath = new Path("d:/a/homework9/input/");//读入多个文件
Path outputPath = new Path("d:/a/homework9/output/");//输出一个文件
if (fs.exists(inputPath)) {
fs.delete(outputPath, true);
}
FileInputFormat.setInputPaths(job, inputPath);
FileOutputFormat.setOutputPath(job, outputPath);
boolean isdone = job.waitForCompletion(true);
System.exit(isdone ? 0 : 1);
}
public static class BigNumFileSortMR_Mapper extends Mapper<LongWritable, Text, IntWritable, NullWritable>{
Text kout = new Text();
Text valueout = new Text();
@Override
protected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException {
int num = Integer.parseInt(value.toString());
context.write(new IntWritable(num), NullWritable.get());
}
}
public static class BigNumFileS