当前位置:   article > 正文

MapReduce输入文件排序_运用mapreduce的思想,实现对输入文件的排序用伪代码

运用mapreduce的思想,实现对输入文件的排序用伪代码

 

 

  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.fs.Path;
  4. import org.apache.hadoop.io.IntWritable;
  5. import org.apache.hadoop.io.Text;
  6. import org.apache.hadoop.mapreduce.Job;
  7. import org.apache.hadoop.mapreduce.Mapper;
  8. import org.apache.hadoop.mapreduce.Reducer;
  9. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  10. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  11. public class Sort {
  12. //map
  13. public static class SortMapper extends Mapper<Object,Text,IntWritable,IntWritable>{
  14. public void map(Object key,Text value,Context context)throws IOException,InterruptedException{
  15. String text = value.toString();
  16. int t = Integer.parseInt(text);
  17. context.write(new IntWritable(t),new IntWritable(1));
  18. }
  19. }
  20. //reduce
  21. public static class SortReducer extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable>{
  22. int row=1;
  23. public void reduce(IntWritable key,Iterable<IntWritable> values,Context context)throws IOException,InterruptedException{
  24. for(IntWritable value:values) {
  25. context.write(new IntWritable(row),key);
  26. row=row+1;
  27. }
  28. }
  29. }
  30. //main
  31. public static void main(String[] args)throws Exception{
  32. Configuration conf=new Configuration();
  33. Job job=Job.getInstance(conf,"sort");
  34. job.setJarByClass(Sort.class);
  35. job.setMapperClass(SortMapper.class);
  36. job.setReducerClass(SortReducer.class);
  37. job.setOutputKeyClass(IntWritable.class);
  38. job.setOutputValueClass(IntWritable.class);
  39. FileInputFormat.addInputPath(job,new Path("input"));
  40. FileOutputFormat.setOutputPath(job,new Path("output"));
  41. System.exit(job.waitForCompletion(true)?0:1);
  42. }
  43. }

 代码部分完成后对文件进行打包,打包步骤详细内容在上一篇文章MapReduce文件合并与去重中。

按照要求在usr/local/hadoop目录下创建两个文本文件A.txt  B.txt ,之后进行如下操作:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/781062
推荐阅读
相关标签
  

闽ICP备14008679号