企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
输入样例 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"); } } ```