输入样例
file1.txt
```
2
32
654
32
15
756
65223
```
file2.txt
```
5956
22
650
92
```
file3.txt
```
26
54
6
26
```
代码实现
```
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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;
public class sort {
public static class MyMapper extends Mapper<LongWritable, Text, IntWritable, IntWritable>{
private static IntWritable data=new IntWritable();
protected void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {
String str=value.toString();
data.set(Integer.parseInt(str));
context.write(data, new IntWritable(1));
}
}
public static class MyReducer extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable>{
private static IntWritable linenum = new IntWritable(1);
protected void reduce(IntWritable key, Iterable<IntWritable> values, Context context)throws IOException, InterruptedException {
for(IntWritable val:values){
context.write(linenum, key);
linenum = new IntWritable(linenum.get()+1);
}
}
}
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
Job job = new Job(conf);
job.setJarByClass(sort.class);
job.setMapperClass(MyMapper.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
job.setReducerClass(MyReducer.class);
FileInputFormat.addInputPath(job, new Path("D:\\input2\\file1.txt"));
FileInputFormat.addInputPath(job, new Path("D:\\input2\\file2.txt"));
FileInputFormat.addInputPath(job, new Path("D:\\input2\\file3.txt"));
FileOutputFormat.setOutputPath(job, new Path("D:\\topoutput2"));
job.waitForCompletion(true);
System.out.println("ok");
}
}
```
- 空白目录
- 第一章 Linux虚拟机安装
- 第二章 SSH配置
- 第三章 jdk配置
- 第四章 Hadoop配置-单机
- 第五章 Hadoop配置-集群
- 第六章 HDFS
- 第七章 MapReduce
- 7.1 MapReduce(上)
- 7.2 MapReduce(下)
- 7.3 MapReduce实验1 去重
- 7.4 MapReduce实验2 单例排序
- 7.5 MapReduce实验3 TopK
- 7.6 MapReduce实验4 倒排索引
- 第八章 Hive
- Hive安装
- 数据定义
- 数据操作
- 第九章 HBase
- 第十章 SaCa RealRec数据科学平台
- 第十一章 Spark Core
- 第十二章 Spark Streaming
- 第十章 Spark测试题