20150812-Mapreduce笔记

来源:互联网 发布:编程入门书籍下载 编辑:程序博客网 时间:2024/03/28 19:47
Mapreduce笔记


1、map函数可以作为一个数据准备阶段(去除已损记录,筛选掉错误的数据),reduce在该准备数据上继续处理。


2、Mapreduce数据处理样例:

Map阶段:



对数据进行筛选


根据键对键/值对进行排序和分组


Reduce阶段(找出其中最大的数):



Mapreduce完整的流程:




说明:下面的例子采用了Hadoop的0.20.0版本的API(上下文对象-context object)旨在是API在今后更容易扩展。新的API在类型上不兼容先前的API,所以,需要重写以前的应用程序才能使得新的API发挥作用。
  • 新的API倾向于使用虚类,而不是接口,因为更容易扩展。例如,可以无需修改类的实现而在虚类中添加一个方法。在新的API中,mapper和reducer都是虚类。
  • 新的API放在org.apache.hadoop.mapreduce包中。之前版本在API依旧放在org.apache.hadoop.mapred中。
  • 新的API充分使用了上下文对象,是用户代码能与MapReduce系统光通信。例如,MapContext基本具备了JobConf、OutputCollector和Reporter的功能。


  1. package jtlyuan.csdn;  
  2. import java.io.IOException;  
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.conf.Configured;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.io.LongWritable;  
  7. import org.apache.hadoop.io.IntWritable;  
  8. import org.apache.hadoop.io.Text;  
  9. import org.apache.hadoop.mapreduce.Job;  
  10. import org.apache.hadoop.mapreduce.Mapper;  
  11. import org.apache.hadoop.mapreduce.Reducer;  
  12. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  13. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  15. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  16. import org.apache.hadoop.util.Tool;  
  17. import org.apache.hadoop.util.ToolRunner;  
  18. //求最大值  
  19. public class MaxNum extends Configured implements Tool {  
  20. public static class MapClass extends Mapper<LongWritable, Text, IntWritable, IntWritable> {  
  21. private int maxNum = 0;  
  22. public void map(LongWritable key, Text value, Context context)  
  23. throws IOException, InterruptedException {  
  24. String[] str = value.toString().split(","2);  
  25. try {// 对于非数字字符我们忽略掉  
  26. int temp = Integer.parseInt(str[0]);  
  27. if (temp > maxNum) {  
  28. maxNum = temp;  
  29. }  
  30. catch (NumberFormatException e) {  
  31. }  
  32. }  
  33. @Override  
  34. protected void cleanup(Context context) throws IOException,  
  35. InterruptedException {  
  36. context.write(new IntWritable(maxNum), new IntWritable(maxNum));  
  37. }  
  38. }  
  39. public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {  
  40. private int maxNum = 0;  
  41. public void reduce(IntWritable key, Iterable<IntWritable> values, Context context)  
  42. throws IOException, InterruptedException {  
  43. for (IntWritable val : values) {  
  44. if ( val.get() > maxNum) {  
  45. maxNum = val.get();  
  46. }  
  47. }  
  48. }  
  49. @Override  
  50. protected void cleanup(Context context) throws IOException,  
  51. InterruptedException {  
  52. context.write(new IntWritable(maxNum), new IntWritable(maxNum));  
  53. }  
  54. }  
  55. public int run(String[] args) throws Exception {  
  56. Configuration conf = getConf();  
  57. Job job = new Job(conf, "MaxNum");  
  58. job.setJarByClass(MaxNum.class);  
  59. FileInputFormat.setInputPaths(job, new Path(args[0]));  
  60. FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  61. job.setMapperClass(MapClass.class);  
  62. job.setCombinerClass(Reduce.class);  
  63. job.setReducerClass(Reduce.class);  
  64. job.setInputFormatClass(TextInputFormat.class);  
  65. job.setOutputFormatClass(TextOutputFormat.class);  
  66. job.setOutputKeyClass(IntWritable.class);  
  67. job.setOutputValueClass(IntWritable.class);  
  68. System.exit(job.waitForCompletion(true) ? 0 : 1);  
  69. return 0;  
  70. }  
  71. public static void main(String[] args) throws Exception {  
  72. long start = System.nanoTime();  
  73. int res = ToolRunner.run(new Configuration(), new MaxNum(), args);  
  74. System.out.println(System.nanoTime()-start);  
  75. System.exit(res);  
  76. }  
  77. }  
  78. /* 
  79. * 最终输出:6009554 6009554 
  80. * 
  81. */  


3、MapReduce的数据流

一个reduce:



多个reduce:





扩展阅读:
http://www.tuicool.com/articles/aqU7Rb










0 0
原创粉丝点击