MapReduce是一种用于大规模数据集的并行计算编程模型,由Google提出,主要用于搜索领域,解决海量数据的计算问题。其主要思想Map(映射)和Reduce(规约)都是从函数是编程语言中借鉴而来的,它可以使程序员在不懂分布式底层的情况下轻松的将自己的程序运行在分布式系统上,极大地降低了分布式计算的门槛。

一、执行流程

1、执行步骤(“天龙八部”)

   1) map任务处理

   ① 读取数据文件内容,对每一行内容解析成<k1,v1>键值对,每个键值对调用一次map函数;

   ② 编写Map映射函数处理逻辑,将输入的<k1,v1>转换成新的<k2,v2>并输出;

   ③ 对输出的<k2、v2>按照reducer个数以及分区规则进行分区;

   ④ 对不同分区的数据,按照k2进行排序、分组,将相同的k2的v2放倒一个集合中,转化成<k2,{v2....}>;

   ⑤ (可选)将分组后的数据进行归约;

   2) reduce任务处理

   ① 对多个map任务的输出,按照不同的分区,通过网络copy到不同的reduce节点;

   ② 对copy过来的来自多个map任务输出的数据<k2,{v2...}>进行排序、合并,编写Reduce归约函数处理逻辑,将接收到的数据处理转化成<k3,v3>;

   ③ 将reduce输出的结果存储到HDFS文件中;

2、执行流程原理

   执行原理图与Map、Reduce详细执行过程如下所示。

图1.2.1 mapreduce执行原理图

图1.2.2 map与reduce过程示意图

   Map端处理流程分析:

   1) 每个输入分片会交给一个Map任务(是TaskTracker节点上运行的一个Java进程),默认情况下,系统会以HDFS的一个块大小作为一个分片(hadoop2默认128M,配置dfs.blocksize)。Map任务通过InputFormat将输入分片处理成可供Map处理的<k1,v1>键值对。

   2) 通过自己的Map处理方法将<k1,v1>处理成<k2,v2>,输出结果会暂时放在一个环形内存缓冲(缓冲区默认大小100M,由mapreduce.task.io.sort.mb属性控制)中,当缓冲区快要溢出时(默认为缓冲区大小的80%,由mapreduce.map.sort.spill.percent属性控制),会在本地操作系统文件系统中创建一个溢出文件(由mapreduce.cluster.local.dir属性控制,默认${hadoop.tmp.dir}/mapred/local),保存缓冲区的数据。溢写默认控制为内存缓冲区的80%,是为了保证在溢写线程把缓冲区那80%的数据写到磁盘中的同时,Map任务还可以继续将结果输出到缓冲区剩余的20%内存中,从而提高任务执行效率。

   3) 每次spill将内存数据溢写到磁盘时,线程会根据Reduce任务的数目以及一定的分区规则将数据进行分区,然后分区内再进行排序、分组,如果设置了Combiner,会执行规约操作。

   4) 当map任务结束后,可能会存在多个溢写文件,这时候需要将他们合并,合并操作在每个分区内进行,先排序再分组,如果设置了Combiner并且spill文件大于mapreduce.map.combine.minspills值(默认值3)时,会触发Combine操作。每次分组会形成新的键值对<k2,{v2...}>。

   5) 合并操作完成后,会形成map端的输出文件,等待reduce来拷贝。如果设置了压缩,则会将输出文件进行压缩,减少网络流量。是否进行压缩,mapreduce.output.fileoutputformat.compress,默认为false。设置压缩库,mapreduce.output.fileoutputformat.compress.codec,默认值org.apache.hadoop.io.compress.DefaultCodec。

   Reduce端处理流程分析:

   1) Reduce端会从AM那里获取已经执行完的map任务,然后以http的方法将map输出的对应数据拷贝至本地(拷贝最大线程数mapreduce.reduce.shuffle.parallelcopies,默认值5)。每次拷贝过来的数据都存于内存缓冲区中,当数据量大于缓冲区大小(由mapreduce.reduce.shuffle.input.buffer.percent控制,默认0.7)的一定比例(由mapreduce.reduce.shuffle.merge.percent控制,默认0.66)时,则将缓冲区的数据溢写到一个本地磁盘中。由于数据来自多个map的同一个分区,溢写时不需要再分区,但要进行排序和分组,如果设置了Combiner,还会执行Combine操作。溢写过程与map端溢写类似,输出写入可同时进行。

   2) 当所有的map端输出该分区数据都已经拷贝完毕时,本地磁盘可能存在多个spill文件,需要将他们再次排序、分组合并,最后形成一个最终文件,作为Reduce任务的输入。此时标志Shuffle阶段结束,然后Reduce任务启动,将最终文件中的数据处理形成新的键值对<k3,v3>。

   3) 将生成的数据<k3,v3>输出到HDFS文件中。

二、WordCount实例

1、WordCount简介及hadoop1.1.2版本的写法

   WordCount程序被称作MapReduce的入门“Hello World”,要学好MapReduce必须先搞定WordCount。WordCount处理的目的是统计所有文档中不同单词出现的次数。样例代码如下所示(完整项目源码点此下载,使用hadoop版本1.1.2):

  1. package mapreduce;
  2.  
  3. import java.util.StringTokenizer;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.Path;
  6. import org.apache.hadoop.io.LongWritable;
  7. import org.apache.hadoop.io.Text;
  8. import org.apache.hadoop.mapreduce.Job;
  9. import org.apache.hadoop.mapreduce.Mapper;
  10. import org.apache.hadoop.mapreduce.Reducer;
  11. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  12. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  13. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  14. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  15. import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
  16.  
  17. public class MyWordCount {
  18. static final String INPUT_PATH = "hdfs://hadoop:9000/hello";
  19. static final String OUTPUT_PATH = "hdfs://hadoop:9000/out";
  20.  
  21. public static void main(String[] args) throws Exception {
  22. Configuration conf = new Configuration();
  23. Job job = new Job(conf, WordCountApp2.class.getSimpleName());
  24.  
  25. //1.1 输入目录
  26. FileInputFormat.setInputPaths(job, INPUT_PATH);
  27. //指定对输入数据进行格式化处理的类
  28. job.setInputFormatClass(TextInputFormat.class);
  29.  
  30. //1.2 指定自定义的Mapper类
  31. job.setMapperClass(MyMapper.class);
  32. //指定map输出的<k,v>类型。如果<k3,v3>类型与<k2,v2>类型一致,此处可以省略
  33. //job.setMapOutputKeyClass(Text.class);
  34. //job.setMapOutputValueClass(LongWritable.class);
  35.  
  36. //1.3 分区
  37. //job.setPartitionerClass(HashPartitioner.class);
  38. //job.setNumReduceTasks(3);
  39.  
  40. //1.4 排序、分组
  41.  
  42. //1.5(可选)归约
  43. //job.setCombinerClass(MyReducer.class);
  44.  
  45. //2.2指定自定义的Recude函数
  46. job.setReducerClass(MyReducer.class);
  47. //指定输出类型
  48. job.setOutputKeyClass(Text.class);
  49. job.setOutputValueClass(LongWritable.class);
  50.  
  51. //2.3指定输出路径
  52. FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH));
  53. //指定输出格式化类
  54. //job.setOutputFormatClass(TextOutputFormat.class);
  55.  
  56. //把作业提交给JobTracker运行
  57. job.waitForCompletion(true);
  58. }
  59.  
  60. /**
  61. * Map方法
  62. * <0,hello you> => <hello,1>,<you,1>
  63. * <10,hello me> => <hello,1>,<me,1>
  64. */
  65. static class MyMapper extends Mapper<LongWritable,Text,Text,LongWritable>{
  66. protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,LongWritable>.Context context) throws java.io.IOException ,InterruptedException {
  67. StringTokenizer st = new StringTokenizer(value.toString());
  68. while(st.hasMoreTokens()){
  69. Text word = new Text(st.nextToken());
  70. context.write(word, new LongWritable(1L));
  71. }
  72. };
  73. }
  74.  
  75. /**
  76. * Reduce方法
  77. * <hello,{1,1}>,<me,{1}>,<you,{1}> => <hello,2>,<me,1>,<you,1>
  78. */
  79. static class MyReducer extends Reducer<Text,LongWritable,Text,LongWritable>{
  80. protected void reduce(Text k2, java.lang.Iterable<LongWritable> v2s, org.apache.hadoop.mapreduce.Reducer<Text,LongWritable,Text,LongWritable>.Context context) throws java.io.IOException ,InterruptedException {
  81. long sum = 0L;
  82. for(LongWritable v2 : v2s){
  83. sum += v2.get();
  84. }
  85. context.write(k2, new LongWritable(sum));
  86. };
  87. }
  88. }

   假定输入文件(hdfs://hadoop:9000/hello)内容为:hello (\t) you (换行) hello (\t) me,则按照上述执行步骤,假设reduce个数为3,则处理过程如下:

   ① 步骤1.1,处理结果:<0,hello you>、<10,hello me>;

   ② 步骤1.2,处理结果:<hello,1>、<you,1>、<hello,1>、<me,1>;

   ③ 步骤1.3,处理结果:(分区1)<hello,1>、<hello,1>,(分区2)<me,1>,(分区3)<you,1>;

   ④ 步骤1.4,处理结果:(分区1)<hello,{1,1}>,(分区2)<me,{1}>,(分区3)<you,{1}>;

   ⑤ 步骤1.5(可选),处理结果:(分区1)<hello,2>,(分区2)<me,1>,(分区3)<you,1>;

   ④步骤2.2,处理结果:<hello,2>、<me,1>、<you,1>。

2、WordCount旧版(0.x)api的写法

   以上是版本1.1.2的MapReduce写法,与旧版(0.x)的MapReduce写法稍有区别,旧版写法如下所示:

  1. package old;
  2.  
  3. import java.io.IOException;
  4. import java.util.Iterator;
  5. import java.util.StringTokenizer;
  6.  
  7. import org.apache.hadoop.conf.Configuration;
  8. import org.apache.hadoop.fs.Path;
  9. import org.apache.hadoop.io.LongWritable;
  10. import org.apache.hadoop.io.Text;
  11. import org.apache.hadoop.mapred.FileInputFormat;
  12. import org.apache.hadoop.mapred.FileOutputFormat;
  13. import org.apache.hadoop.mapred.JobClient;
  14. import org.apache.hadoop.mapred.JobConf;
  15. import org.apache.hadoop.mapred.MapReduceBase;
  16. import org.apache.hadoop.mapred.Mapper;
  17. import org.apache.hadoop.mapred.OutputCollector;
  18. import org.apache.hadoop.mapred.Reducer;
  19. import org.apache.hadoop.mapred.Reporter;
  20. import org.apache.hadoop.mapred.TextInputFormat;
  21. import org.apache.hadoop.mapred.TextOutputFormat;
  22. import org.apache.hadoop.mapred.lib.HashPartitioner;
  23.  
  24. public class OldAPP {
  25.  
  26. private static final String INPUT_PATH = "hdfs://hadoop:9000/hello";
  27. private static final String OUTPUT_PATH = "hdfs://hadoop:9000/out";
  28.  
  29. /**
  30. * 旧api(0.x)与新api(1.x)写法区别:
  31. * 1、Job:旧api使用JobConf,新api使用Job
  32. * 2、包名:旧api类的包名为mapred,新api类的包名为mapreduce
  33. * 3、提交作业:旧api使用JobClient.runJob提交作业,新api使用Job.waitForCompletion提交作业
  34. * 4、Mapper和Reducer:旧api的Mapper和Reducer继承MapReduceBase类并实现分别实现接口Mapper和Reducer,使用OutputCollector.collect记录数据,新api的Mapper和Reducer分别继承Mapper和Reducer类,使用Context.write记录数据
  35. */
  36. public static void main(String[] args) throws InterruptedException, IOException {
  37. JobConf job = new JobConf(new Configuration(),OldAPP.class);
  38.  
  39. //1.1 设置输入数据格式化类
  40. job.setInputFormat(TextInputFormat.class);
  41. //设置输入文件路径
  42. FileInputFormat.setInputPaths(job, new Path(INPUT_PATH));
  43.  
  44. //1.2 设置自定Map函数
  45. job.setMapperClass(MyMapper.class);
  46. //设置出数据类型
  47. job.setMapOutputKeyClass(Text.class);
  48. job.setMapOutputValueClass(LongWritable.class);
  49.  
  50. //1.3 设置分区
  51. job.setPartitionerClass(HashPartitioner.class);
  52. //设置Reduce个数
  53. job.setNumReduceTasks(1);
  54.  
  55. //2.2 设置自定义Reduce函数
  56. job.setReducerClass(MyReducer.class);
  57. //设置输出数据类型
  58. job.setOutputKeyClass(Text.class);
  59. job.setOutputValueClass(LongWritable.class);
  60.  
  61. //2.3 设置输出路径
  62. FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH));
  63. //设置输出格式化处理类
  64. job.setOutputFormat(TextOutputFormat.class);
  65.  
  66. //提交作业
  67. JobClient.runJob(job);
  68. }
  69.  
  70. static class MyMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, LongWritable>{
  71. @Override
  72. public void map(LongWritable key, Text value,
  73. OutputCollector<Text, LongWritable> output, Reporter reporter)
  74. throws IOException {
  75. StringTokenizer st = new StringTokenizer(value.toString());
  76. while(st.hasMoreTokens()){
  77. Text word = new Text(st.nextToken());
  78. output.collect(word, new LongWritable(1L));
  79. }
  80. }
  81. }
  82.  
  83. static class MyReducer extends MapReduceBase implements Reducer<Text, LongWritable, Text, LongWritable>{
  84. @Override
  85. public void reduce(Text key, Iterator<LongWritable> values,
  86. OutputCollector<Text, LongWritable> output, Reporter reporter)
  87. throws IOException {
  88. long sum = 0l;
  89. while(values.hasNext()){
  90. sum += values.next().get();
  91. }
  92. output.collect(key, new LongWritable(sum));
  93. }
  94. }
  95. }

  新旧版本MapReduce写法上的区别:

  ① Job:旧api使用JobConf,新api使用Job;

  ② 包名:旧api类的包名为mapred,新api类的包名为mapreduce;

  ③ 提交作业:旧api使用JobClient.runJob提交作业,新api使用Job.waitForCompletion提交作业;

  ④ Mapper和Reducer:旧api的Mapper和Reducer继承MapReduceBase类并实现分别实现接口Mapper和Reducer,使用OutputCollector.collect记录数据,新api的Mapper和Reducer分别继承Mapper和Reducer类,使用Context.write记录数据。

三、自定义数据类型

   Hadoop内置了8中数据类型,在某些特殊的条件下,我们可能需要自定义数据类型方便MapReduce的处理。

1、内置的数据类型

   ①BooleanWritable,标准布尔型数值;②ByteWritable,单字节数值;③DoubleWritable,双字节数值;④FloatWritable,浮点数;⑤IntWritable,整型数;⑥LongWritable,长整型数;⑦Text,使用UTF8格式存储的文本;⑧NullWritable,当<key,value>中的key或value为空时使用。

   上述hadoop数据类型转换成java的基本数据类型方法:Text类型使用toString()方法,其他类型使用get()方法。

2、自定义数据类型

   1) 需继承成Writable接口,并实现方法write()和readFields(),以便数据能被序列化和反序列化,从而完成网络传输或文件输入输出;

   2) 如果该数据要作为主键key使用,或需要比较大小,则需要继承WritableComparable接口,并实现方法write()、readFiles()和CompareTo()。

3、一个实例--手机流量数据统计分析

  现有用户的上传下载日志记录如下所示,要求统计出不同手机的总上传下载总数据包数和上传下载总流量。

  1. 1363157985066 13726230503 120.196.100.82 24 27 2481 24681
  2. 1363157995052 13826544101 120.197.40.4 4 0 264 0
  3. 1363157991076 13926435656 120.196.100.99 2 4 132 1512
  4. 1363154400022 13926251106 120.197.40.4 4 0 240 0
  5. 1363157993044 13726230503 120.196.100.99 15 12 1527 2106
  6. 1363157995074 13826544101 120.197.40.4 20 16 4116 1432

  数据列说明:①reportTime,记录生成时间戳;②msisdn,手机号码;③host,访问网址;④upPackNum,上传数据包数;⑤downPackNum,下载数据包数;⑥upPayLoad,上传总流量;⑦downPayLoad,下载总流量。

   实现程序源码如下所示:

  1. package mapreduce;
  2.  
  3. import java.io.DataInput;
  4. import java.io.DataOutput;
  5. import java.io.IOException;
  6.  
  7. import javax.swing.JOptionPane;
  8.  
  9. import org.apache.hadoop.conf.Configuration;
  10. import org.apache.hadoop.fs.Path;
  11. import org.apache.hadoop.io.LongWritable;
  12. import org.apache.hadoop.io.Text;
  13. import org.apache.hadoop.io.Writable;
  14. import org.apache.hadoop.mapreduce.Job;
  15. import org.apache.hadoop.mapreduce.Mapper;
  16. import org.apache.hadoop.mapreduce.Reducer;
  17. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  18. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  19. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  20. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  21. import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
  22.  
  23. public class KpiApp {
  24. static String INPUT_PATH = "hdfs://hadoop:9000/mobile-in";
  25. static String OUT_PATH = "hdfs://hadoop:9000/mobile-out";
  26.  
  27. public static void main(String[] args) throws Exception {
  28. Job job = new Job(new Configuration(),KpiApp.class.getSimpleName());
  29.  
  30. //1.1 指定输入文件路径
  31. FileInputFormat.setInputPaths(job, new Path(INPUT_PATH));
  32. //指定格式化处理类(默认TextInputFormat)
  33. job.setInputFormatClass(TextInputFormat.class);
  34.  
  35. //1.2 指定自定义的Mapper类
  36. job.setMapperClass(MyMapper.class);
  37. //指定输出<k2,v2>类型
  38. job.setMapOutputKeyClass(Text.class);
  39. job.setMapOutputValueClass(KpiWritable.class);
  40.  
  41. //1.3 设置分区类
  42. job.setPartitionerClass(HashPartitioner.class);
  43. //设置分区数
  44. job.setNumReduceTasks(1);
  45.  
  46. //2.2 指定自定义的Reduce类
  47. job.setReducerClass(MyReduce.class);
  48. //设置输出类型
  49. job.setOutputKeyClass(Text.class);
  50. job.setOutputValueClass(KpiWritable.class);
  51.  
  52. //2.3 指定输出文件路径
  53. FileOutputFormat.setOutputPath(job, new Path(OUT_PATH));
  54. //指定输出文件的格式化类
  55. job.setOutputFormatClass(TextOutputFormat.class);
  56.  
  57. //提交作业到JobTracker
  58. job.waitForCompletion(true);
  59. }
  60.  
  61. static class MyMapper extends Mapper<LongWritable, Text, Text, KpiWritable>{
  62. protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper<LongWritable,Text,Text,KpiWritable>.Context context) throws IOException ,InterruptedException {
  63. String[] splits = value.toString().split("\t");
  64. Text k2 = new Text(splits[1]); //手机
  65. KpiWritable v2 = new KpiWritable(Long.parseLong(splits[3]), Long.parseLong(splits[4]), Long.parseLong(splits[5]), Long.parseLong(splits[6]));
  66. context.write(k2, v2);
  67. };
  68. }
  69.  
  70. static class MyReduce extends Reducer<Text, KpiWritable, Text, KpiWritable>{
  71. protected void reduce(Text k2, java.lang.Iterable<KpiWritable> v2s, org.apache.hadoop.mapreduce.Reducer<Text,KpiWritable,Text,KpiWritable>.Context context) throws IOException ,InterruptedException {
  72. long upPackNum = 0;
  73. long downPackNum = 0;
  74. long upPayLoad = 0;
  75. long downPayLoad = 0;
  76. for (KpiWritable v2 : v2s) {
  77. upPackNum += v2.upPackNum;
  78. downPackNum += v2.downPackNum;
  79. upPayLoad += v2.upPayLoad;
  80. downPayLoad += v2.downPayLoad;
  81. }
  82. context.write(k2, new KpiWritable(upPackNum, downPackNum, upPayLoad, downPayLoad));
  83. };
  84. }
  85. }
  86.  
  87. //自定义手机流量数据类型,便于统计计算
  88. class KpiWritable implements Writable{
  89. long upPackNum;
  90. long downPackNum;
  91. long upPayLoad;
  92. long downPayLoad;
  93.  
  94. public KpiWritable(){}
  95.  
  96. public KpiWritable(long upPackNum,long downPackNum,long upPayLoad,long downPayLoad){
  97. this.upPackNum = upPackNum;
  98. this.downPackNum = downPackNum;
  99. this.upPayLoad = upPayLoad;
  100. this.downPayLoad = downPayLoad;
  101. }
  102.  
  103. @Override
  104. public void write(DataOutput out) throws IOException {
  105. out.writeLong(upPackNum);
  106. out.writeLong(downPackNum);
  107. out.writeLong(upPayLoad);
  108. out.writeLong(downPayLoad);
  109. }
  110.  
  111. @Override
  112. public void readFields(DataInput in) throws IOException {
  113. this.upPackNum = in.readLong();
  114. this.downPackNum = in.readLong();
  115. this.upPayLoad = in.readLong();
  116. this.downPayLoad = in.readLong();
  117. }
  118.  
  119. @Override
  120. public String toString() {
  121. return upPackNum + "\t" + downPackNum + "\t" + upPayLoad + "\t" + downPayLoad;
  122. }
  123. }

   输出数据如下所示:

  1. 13726230503 39 39 4008 26787
  2. 13826544101 24 16 4380 1432
  3. 13926251106 4 0 240 0
  4. 13926435656 2 4 132 1512
四、编写MR程序

1、MapReduce获取命令行参数

   通常为了程序运行的灵活性,MapReduce的输入和输出文件路径会通过args参数获取。这时候程序会通过Jar包在命令行中执行(程序必须设置Job.setJarByClass(XXX.class)),同时指定输入和输出路径(执行语句:hadoop jar xxx.jar [args0] [args1])。通常有以下几种设置方法:

   1) 在程序中直接验证args参数并获取值

  1. String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  2. if (otherArgs.length != 2) {
  3. System.err.println("Usage: wordcount <in> <out>");
  4. System.exit(2);
  5. }
  6. FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  7. FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

   2) 通过继承类Configured和实现接口Tool来实现

  1. public class CacheFileApp extends Configured implements Tool{
  2.  
  3. public static void main(String[] args) throws Exception {
  4. ToolRunner.run(new Configuration(), new CacheFileApp(), args);
  5. }
  6.  
  7. @Override
  8. public int run(String[] args) throws Exception {
  9. //..............................
  10. Path in = new Path(args[0]);
  11. Path out = new Path(args[1]);
  12. //..............................
  13. }
  14. }

2、自定义计数器

   为了便于监测MapReduce程序工作的情况,Hadoop允许自定义计数器来统计某些情况的发生次数,例如分析输入数据中多少记录存在敏感词等。其用法如下所示(setValue设置初始值,increment增加计数):

  1. protected void map(... context) throws ... {
  2. //此处sensitive word为分组名称,hello为计数器名称
  3. Counter helloCounter = context.getCounter("sensitive word","hello");
  4. Counter meCounter = context.getCounter("sensitive word","me");
  5. String line = value.toString();
  6. if(line.contains("hello")) {
  7. helloCounter.increment(1);
  8. }
  9. if(line.contains("me")) {
  10. meCounter.increment(1);
  11. }
  12.  
  13. ...
  14. };
  15.  
  16. //假设输入文件内容为:
  17. //hello you
  18. //hello me
  19. //则MapReduce在控制台输出的计数器为:
  20. //senstivite word
  21. // hello=2
  22. // me=1

3、自定义分区函数

   自定义分区单数必须继承Partitioner,并重写方法getPartition()。注意,返回的分区编号必须处于当前设置的Reduce个数范围内,此外,当前应用程序必须以Jar包的形式使用Hadoop jar xxx.jar命令来运行(原因待了解)。

  1. public static void main(String[] args) throws Exception {
  2. ...
  3.  
  4. //1.3 分区
  5. job.setNumReduceTasks(2);
  6. job.setPartitionerClass(MyPartition.class);
  7.  
  8. ...
  9. }
  10.  
  11. static class MyPartition extends Partitioner<Text, LongWritable>{
  12. @Override
  13. public int getPartition(Text key, LongWritable value, int numReduceTasks) {
  14. //测试样例 此处将key长度小于3和大于等于3的分别放在不同的Reduce任务中进行处理
  15. return key.toString().length()<3?0:1;
  16. }
  17. }

4、自定义排序

   在Map任务阶段map函数的输出给Reduce节点前会进行排序、分区和分组,Reduce阶段接收到来自不同map函数的输出记录也会进行排序、分组。MapReduce默认是按照k2类型进行排序,在实际开发中,如果想同时结合k2和v2进行排序,就需要自定义k2类型或通过方法setSortComparatorClass()设置排序函数。

   这是一个通过自定义k2类型来实现自定义排序的例子,假如输入如下数据:

  1. 3 3
  2. 3 1
  3. 3 2
  4. 2 2
  5. 2 1
  6. 1 1

   要求按照第一列升序、第二列降序排序并输出mr结果,我们可以自定义MyK2类型并实现k2的内部比较方法,并且建议重写Myk2的hashCode()与equals()方法。由于自定义K2类型后,为了不改变MapReduce之前的分区结果,我们需要自定义分区函数,让它根据MyK2.first来进行分区。具体代码如下所示:

  1. package sort;
  2.  
  3. import java.io.DataInput;
  4. import java.io.DataOutput;
  5. import java.io.IOException;
  6. import java.net.URI;
  7. import java.util.StringTokenizer;
  8.  
  9. import org.apache.hadoop.conf.Configuration;
  10. import org.apache.hadoop.fs.FileSystem;
  11. import org.apache.hadoop.fs.Path;
  12. import org.apache.hadoop.io.LongWritable;
  13. import org.apache.hadoop.io.RawComparator;
  14. import org.apache.hadoop.io.Text;
  15. import org.apache.hadoop.io.WritableComparable;
  16. import org.apache.hadoop.io.WritableComparator;
  17. import org.apache.hadoop.mapreduce.Job;
  18. import org.apache.hadoop.mapreduce.Mapper;
  19. import org.apache.hadoop.mapreduce.Partitioner;
  20. import org.apache.hadoop.mapreduce.Reducer;
  21. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  22. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  23. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  24. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  25. import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
  26.  
  27. public class SortDemo {
  28.  
  29. private static String INPUT_PATH = "hdfs://hadoop:9000/sort-in";
  30. private static String OUTPUT_PATH = "hdfs://hadoop:9000/sort-out";
  31.  
  32. public static void main(String[] args) throws Exception {
  33. Configuration conf = new Configuration();
  34. Job job = new Job(conf, SortDemo.class.getSimpleName());
  35.  
  36. // 删除已存在的输出文件
  37. FileSystem fs = FileSystem.get(new URI(OUTPUT_PATH), conf);
  38. if (fs.exists(new Path(OUTPUT_PATH))) {
  39. fs.delete(new Path(OUTPUT_PATH), true);
  40. }
  41.  
  42. // 设置通过Jar运行
  43. job.setJarByClass(SortDemo.class);
  44.  
  45. // 1.1 设置输入文件路径
  46. FileInputFormat.setInputPaths(job, new Path(INPUT_PATH));
  47. // 设置输入数据格式化处理类
  48. job.setInputFormatClass(TextInputFormat.class);
  49.  
  50. // 1.2 设置自定义map函数
  51. job.setMapperClass(MyMapper.class);
  52. // 设置输出数据类型
  53. job.setMapOutputKeyClass(MyK2.class);
  54. job.setMapOutputValueClass(LongWritable.class);
  55.  
  56. // 1.3 设置分区
  57. job.setPartitionerClass(MyHashPartition.class);
  58. // 设置分区数
  59. job.setNumReduceTasks(1);
  60. // 设置自定义recude函数
  61. job.setReducerClass(MyReducer.class);
  62.  
  63. // 2.3 设置输出文件路径
  64. FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH));
  65. // 设置输出数据格式化处理类
  66. job.setOutputFormatClass(TextOutputFormat.class);
  67. // 设置输出数据格式化类型
  68. job.setOutputKeyClass(LongWritable.class);
  69. job.setOutputValueClass(LongWritable.class);
  70.  
  71. // 提交作业
  72. job.waitForCompletion(true);
  73. }
  74.  
  75. static class MyMapper extends Mapper<LongWritable, Text, MyK2, LongWritable> {
  76. protected void map(LongWritable key,Text value,org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, MyK2, LongWritable>.Context context) throws java.io.IOException, InterruptedException {
  77. StringTokenizer st = new StringTokenizer(value.toString());
  78. long token1 = Long.parseLong(st.nextToken());
  79. long token2 = Long.parseLong(st.nextToken());
  80. MyK2 k2 = new MyK2(token1, token2);
  81. LongWritable v2 = new LongWritable(token2);
  82. context.write(k2, v2);
  83. };
  84. }
  85.  
  86. static class MyReducer extends Reducer<MyK2, LongWritable, LongWritable, LongWritable> {
  87. protected void reduce(MyK2 k2,java.lang.Iterable<LongWritable> v2s,org.apache.hadoop.mapreduce.Reducer<MyK2, LongWritable, LongWritable, LongWritable>.Context context) throws IOException, InterruptedException {
  88. for(LongWritable v2:v2s){
  89. context.write(new LongWritable(k2.first), v2);
  90. }
  91. };
  92. }
  93.  
  94. // 自定义分区函数 因为Map输出的k2为自定义类型 根据k2进行分区要按照k2.first才能不改变原分区结果
  95. static class MyHashPartition extends HashPartitioner<MyK2, LongWritable> {
  96. @Override
  97. public int getPartition(MyK2 key, LongWritable value, int numReduceTasks) {
  98. return (key.first.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
  99. }
  100. }
  101.  
  102. //自定义k2类型
  103. static class MyK2 implements WritableComparable<MyK2> {
  104. Long first;
  105. Long second;
  106.  
  107. public MyK2() {}
  108.  
  109. public MyK2(long first, long second) {
  110. this.first = first;
  111. this.second = second;
  112. }
  113.  
  114. @Override
  115. public void write(DataOutput out) throws IOException {
  116. out.writeLong(first);
  117. out.writeLong(second);
  118. }
  119.  
  120. @Override
  121. public void readFields(DataInput in) throws IOException {
  122. first = in.readLong();
  123. second = in.readLong();
  124. }
  125.  
  126. //此处按照要求 第一列升序、第二列降序排序
  127. @Override
  128. public int compareTo(MyK2 o) {
  129. long diff = this.first - o.first;
  130. if (diff != 0) {
  131. return (int) diff;
  132. }
  133. return (int) (o.second - this.second);
  134. }
  135.  
  136. // 根据各种资料 此处最好重写hashCode和equals方法
  137. @Override
  138. public int hashCode() {
  139. return this.first.hashCode() + this.second.hashCode();
  140. }
  141.  
  142. @Override
  143. public boolean equals(Object obj) {
  144. if (!(obj instanceof MyK2)) {
  145. return false;
  146. }
  147. MyK2 oK2 = (MyK2) obj;
  148. return (this.first == oK2.first) && (this.second == oK2.second);
  149. }
  150. }
  151.  
  152. }

5、自定义分组函数

   在上一个自定义排序的例子中,我们自定义了k2的类型,导致了Map将输出数据按k2分成了6组,这是因为MapReduce默认按照k2进行分组,实际上只要求按照k2.first进行分组,因此我们必须自定义分组函数。自定义分组函数必须实现接口RawComparator<T>,或者继承类WritableComparator,并实现对应的比较方法。在上一个例子中,自定义分组函数实现如下所示:

  1. /**
  2. * 自定义分组类 根据MyK2的第一个字段进行分组
  3. * 通过Job.setGroupingComparatorClass(MyGroupingComparator.class)设置分组函数
  4. */
  5. static class MyGroupingComparator implements RawComparator<MyK2>{
  6.  
  7. /**
  8. * 此方法是将流反序列化成对象,再进行比较,性能开销较大
  9. */
  10. @Override
  11. public int compare(MyK2 o1, MyK2 o2) {
  12. return (int)(o1.first-o2.first);
  13. }
  14.  
  15. /**
  16. * 该方法允许直接比较数据流中的记录,无需反序列化为对象,RawComparator是一个原生的优化接口类,它只是简单的提供了用于数据流中简单的数据对比方法,从而提供优化
  17. * @param arg0 表示第一个参与比较的字节数组
  18. * @param arg1 表示第一个参与比较的字节数组的起始位置
  19. * @param arg2 表示第一个参与比较的字节数组的偏移量
  20. *
  21. * @param arg3 表示第二个参与比较的字节数组
  22. * @param arg4 表示第二个参与比较的字节数组的起始位置
  23. * @param arg5 表示第二个参与比较的字节数组的偏移量
  24. *
  25. * 这里第三个参数取值为8,是因为MyK2类型中只有两个Long字段,Long类型占8个字节,所以我们只需取前8个字节进行比较
  26. */
  27. @Override
  28. public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
  29. return WritableComparator.compareBytes(b1, s1, 8, b2, s2, 8);
  30. }
  31. }

6、其他方法

   ▶ 获取分片所在路径:String filePath = ((FileSplit)reporter.getInputSplit()).getPath().toString();

五、系统参数配置

1、Configuration

   Configuration是MR的资源配置类,包含了一个作业的运行配置参数。Configuration常见的配置方式如下所示:

  1. Configuration conf = new Configuration();
  2. //配置某个参数值
  3. conf.setLong("io.file.buffer.size", 4096);
  4. /**
  5. * 加载自定义资源配置 后一个配置回覆盖前一个配置的相同项
  6. * 若前一个配置某项不想被覆盖,可设置<final>true</final>
  7. * 如
  8. * <property>
  9. * <name>io.file.buffer.size</name>
  10. * <value>4096</value>
  11. * <final>true</final>
  12. *</property>
  13. */
  14. conf.addResource("configuration-default.xml");
  15. conf.addResource("|configuration-site.xml");

2、远程调试配置

   1) 在hadoop-env.sh中添加配置:

   ▶ 调试NameNode:export HADOOP_NAMENODE_OPTS="-agentlib:jdwp=transport=dt_socket,address=8888,server=y,suspend=y"

   ▶ 调试DataNode:export HADOOP_DATANODE_OPTS="-agentlib:jdwp=transport=dt_socket,address=9888,server=y,suspend=y"

   ▶ 调试ResourceManager:export YARN_RESOURCEMANAGER_OPTS="-agentlib:jdwp=transport=dt_socket,address=7888,server=y,suspend=y"

   ▶ 调试NodeManager:export YARN_NODEMANAGER_OPTS="-agentlib:jdwp=transport=dt_socket,address=6888,server=y,suspend=y"  

   2) 通过hadoop-daemon.sh启动需要调试的服务,如:hadoop-daemon.sh start namenode

   3) 在调试端Eclipse中,执行Debug Configuration,选择Remote Java Application,配置远程的ip和端口,再启动调试即可

六、部分源码分析

1、TextInputFormat--如何计算Map输入分片大小

   TextInputFormat继承FileInputFormat,FileInputFormat继承InputFormat接口。在版本1.1.2中,InputFormat接口只有两个方法:getSplits()和createRecordReader(),分别用来将输入数据划分分片和分别将分片格式化处理形成可供Mapper处理的<k1,v1>键值对。

   FileInputFormat类实现的getSplits方法体源码如下所示:

  1. public List<InputSplit> getSplits(JobContext job) throws IOException {
  2. //getFormatMinSplitSize方法返回1
  3. //getMinSplitSize方法读取配置mapreduce.input.fileinputformat.split.minsize(默认配置0),无配置则返回默认值1L
  4. //也即是说,如果配置了mapreduce.input.fileinputformat.split.minsize并且值X大于1,minSize=X;否则,minSize=1
  5. long minSize = Math.max(getFormatMinSplitSize(), getMinSplitSize(job));
  6. //getMaxSplitSize方法读取配置mapreduce.input.fileinputformat.split.maxsize(默认无此配置),无配置则返回默认值Long.MAX_VALUE
  7. long maxSize = getMaxSplitSize(job);
  8.  
  9. // generate splits
  10. List<InputSplit> splits = new ArrayList<InputSplit>();
  11. List<FileStatus> files = listStatus(job);
  12. for (FileStatus file: files) {
  13. ...
  14. if (isSplitable(job, path)) {
  15. long blockSize = file.getBlockSize();
  16.  
  17. //此处分片大小计算方式computeSplitSize返回Math.max(minSize, Math.min(maxSize, blockSize))
  18. long splitSize = computeSplitSize(blockSize, minSize, maxSize);
  19. //由computeSplitSize方法可得出结论:
  20. //minSize = Math.max(mapreduce.input.fileinputformat.split.minsize(默认配置0),1L),也即是minSize默认值为1L
  21. //maxSize = Math.max(mapreduce.input.fileinputformat.split.minsize(默认无此配置),Long.MAX_VALUE),也即是maxSize默认值为Long.MAX_VALUE
  22. //1)正常情况,minSize<maxSize,此时分片大小取值区间为[minSize,maxSize],HDFS一个数据库大小为blockSize(默认64M)
  23. // 若minSize<=blockSize<=maxSize,则分片大小等于blockSize;
  24. // 若blockSize<minSize,则分片大小等于minSize;
  25. // 若maxSize<blockSize,则分片大小等于maxSize;
  26. //2)极端情况,若minSize>maxSize(即mapreduce.input.fileinputformat.split.minsize配置项大于mapreduce.input.fileinputformat.split.minsize),则无论blockSize为何值,分片大小恒等于minSize(mapred.min.split.size)值
  27.  
  28. ...
  29. }

   由此源码方法可知FileInputFormat处理分片大小有如下逻辑(假设mapreduce.input.fileinputformat.split.minsize配置为X,mapreduce.input.fileinputformat.split.maxsize配置为Y):

   ① (默认)无覆盖配置X与Y,分片大小默认为一个HDFS的block大小(目的是保证尽量MapReduce数据处理的本地化,多次跨网络处理会降低MapReduce处理性能);

   ② 配置了X与Y

   a、(正常情况)X<Y,则分片大小取值范围为[X,Y],若blockSize处于此区间,则分片大小为blockSize;若blockSize在区间外,则分片大小取区间内最接近blockSize大小的那个边界值;

   b、(异常配置)X>Y,则无论blockSize为何值,分片大小恒等于X;

Hadoop学习(4)-- MapReduce的更多相关文章

  1. hadoop学习(七)----mapReduce原理以及操作过程

    前面我们使用HDFS进行了相关的操作,也了解了HDFS的原理和机制,有了分布式文件系统我们如何去处理文件呢,这就的提到hadoop的第二个组成部分-MapReduce. MapReduce充分借鉴了分 ...

  2. Hadoop学习笔记—MapReduce的理解

    我不喜欢照搬书上的东西,我觉得那样写个blog没多大意义,不如直接把那本书那一页告诉大家,来得省事.我喜欢将我自己的理解.所以我会说说我对于Hadoop对大量数据进行处理的理解.如果有理解不对欢迎批评 ...

  3. Hadoop学习之Mapreduce执行过程详解

    一.MapReduce执行过程 MapReduce运行时,首先通过Map读取HDFS中的数据,然后经过拆分,将每个文件中的每行数据分拆成键值对,最后输出作为Reduce的输入,大体执行流程如下图所示: ...

  4. 【尚学堂·Hadoop学习】MapReduce案例2--好友推荐

    案例描述 根据好友列表,推荐好友的好友 数据集 tom hello hadoop cat world hadoop hello hive cat tom hive mr hive hello hive ...

  5. 【尚学堂·Hadoop学习】MapReduce案例1--天气

    案例描述 找出每个月气温最高的2天 数据集 -- :: 34c -- :: 38c -- :: 36c -- :: 32c -- :: 37c -- :: 23c -- :: 41c -- :: 27 ...

  6. hadoop学习day3 mapreduce笔记

    1.对于要处理的文件集合会根据设定大小将文件分块,每个文件分成多块,不是把所有文件合并再根据大小分块,每个文件的最后一块都可能比设定的大小要小 块大小128m a.txt 120m 1个块 b.txt ...

  7. Hadoop学习(3)-mapreduce快速入门加yarn的安装

    mapreduce是一个运算框架,让多台机器进行并行进行运算, 他把所有的计算都分为两个阶段,一个是map阶段,一个是reduce阶段 map阶段:读取hdfs中的文件,分给多个机器上的maptask ...

  8. Hadoop学习(4)-mapreduce的一些注意事项

    关于mapreduce的一些注意细节 如果把mapreduce程序打包放到了liux下去运行, 命令java  –cp  xxx.jar 主类名 如果报错了,说明是缺少相关的依赖jar包 用命令had ...

  9. Hadoop 学习之MapReduce

    MapReduce充分利用了分而治之,主要就是将一个数据量比较大的作业拆分为多个小作业的框架,而用户需要做的就是决定拆成多少份,以及定义作业本身,用户所要做的操作少了又少,真是Very Good! 一 ...

  10. Hadoop学习之旅三:MapReduce

    MapReduce编程模型 在Google的一篇重要的论文MapReduce: Simplified Data Processing on Large Clusters中提到,Google公司有大量的 ...

随机推荐

  1. eclipse中project->clean的作用是什么

    1.由于eclipse的编译是基于时间戳的判断机制的.因此当你按build   all的时候有些eclipse认为时间戳没有改变的类不会被编译.因此你可以先clean一下再编译.这个时候eclipse ...

  2. 合金装备V 幻痛 制作技术特辑

    合金装备V:幻痛 制作特辑 资料原文出自日版CGWORLD2015年10月号   在[合金装备4(Metal Gear Solid IV)]7年后,序章作品[合金装备5 :原爆点 (Metal Gea ...

  3. 异步调试神器Slog,“从此告别看日志,清日志文件了”

    微信调试.API调试和AJAX的调试的工具,能将日志通过WebSocket输出到Chrome浏览器的console中  — Edit 92 commits 4 branches 3 releases ...

  4. spm完成dmp在windows系统上导入详细过程

    --查询dmp字符集 cat spmprd_20151030.dmp ','xxxx')) from dual; spm完成dmp在windows系统上导入详细过程 create tablespace ...

  5. 蓝牙BLE ATT剖析(二)-- PDU

    一.Error Handling Error Response The Error Responseis used to state that a given request cannot be pe ...

  6. 编写category时的便利宏(用于解决category方法从静态库中加载需要特别设置的问题)

    代码摘录自YYKit:https://github.com/ibireme/YYKit /** Add this macro before each category implementation, ...

  7. C code 字符串与整数的相互转化

    #include<stdio.h> int str_to_int(const char *str,int *num); void int_to_str(char str[],const i ...

  8. Nodejs电影建站开发实例(下)

    作为一个真正的网站,不能没有数据的支持,下面使用的数据库为mongodb,电影可能有的数据:电影名称.导演.国家.语言.上映时间.图片.简介.视频 4.使用路由 app.js var express ...

  9. 20145211 《Java程序设计》实验报告四: Android开发基础

    实验内容 基于Android Studio开发简单的Android应用并部署测试; 了解Android组件.布局管理器的使用: 掌握Android中事件处理机制. Android Studio安装 实 ...

  10. 动态创建地图文档MXD并发布地图服务

    原文:动态创建地图文档MXD并发布地图服务 1.动态创建MXD private bool CreateMxd(string MxdPath, string MxdName) { IMapDocumen ...