Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 751|回复: 0

[默认分类] Hadoop基础-MapReduce的数据倾斜解决方案

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2020-8-9 13:01:53 | 显示全部楼层 |阅读模式
                         hadoop基础-MapReduce的数据倾斜解决方案
                                                  作者:尹正杰
    版权声明:原创作品,谢绝转载!否则将追究法律责任。
      
      
      
      
      
    一.数据倾斜简介
    1>.什么是数据倾斜
      答:大量数据涌入到某一节点,导致此节点负载过重,此时就产生了数据倾斜。
    2>.处理数据倾斜的两种方案
      第一:重新设计key;
      第二:设计随机分区;  
      
    二.模拟数据倾斜



      
    1. 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 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5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9
    复制代码

      
    screw.txt 文件内容

    1>.App端代码



      
    1. 1 /*
    2. 2 @author :yinzhengjie
    3. 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
    4. 4 EMAIL:y1053419035@qq.com
    5. 5 */
    6. 6 package cn.org.yinzhengjie.srew;
    7. 7
    8. 8 import org.apache.hadoop.conf.Configuration;
    9. 9 import org.apache.hadoop.fs.FileSystem;
    10. 10 import org.apache.hadoop.fs.Path;
    11. 11 import org.apache.hadoop.io.IntWritable;
    12. 12 import org.apache.hadoop.io.Text;
    13. 13 import org.apache.hadoop.mapreduce.Job;
    14. 14 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    15. 15 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    16. 16
    17. 17 public class ScrewApp {
    18. 18     public static void main(String[] args) throws Exception {
    19. 19         //实例化一个Configuration,它会自动去加载本地的core-site.xml配置文件的fs.defaultFS属性。(该文件放在项目的resources目录即可。)
    20. 20         Configuration conf = new Configuration();
    21. 21         //将hdfs写入的路径定义在本地,需要修改默认为文件系统,这样就可以覆盖到之前在core-site.xml配置文件读取到的数据。
    22. 22         conf.set("fs.defaultFS","file:///");
    23. 23         //代码的入口点,初始化HDFS文件系统,此时我们需要把读取到的fs.defaultFS属性传给fs对象。
    24. 24         FileSystem fs = FileSystem.get(conf);
    25. 25         //创建一个任务对象job,别忘记把conf穿进去哟!
    26. 26         Job job = Job.getInstance(conf);
    27. 27         //给任务起个名字
    28. 28         job.setJobName("WordCount");
    29. 29         //指定main函数所在的类,也就是当前所在的类名
    30. 30         job.setJarByClass(ScrewApp.class);
    31. 31         //指定map的类名,这里指定咱们自定义的map程序即可
    32. 32         job.setMapperClass(ScrewMapper.class);
    33. 33         //指定reduce的类名,这里指定咱们自定义的reduce程序即可
    34. 34         job.setReducerClass(ScrewReduce.class);
    35. 35         //设置输出key的数据类型
    36. 36         job.setOutputKeyClass(Text.class);
    37. 37         //设置输出value的数据类型
    38. 38         job.setOutputValueClass(IntWritable.class);
    39. 39         Path localPath = new Path("D:\\10.Java\\IDE\\yhinzhengjieData\\MyHadoop\\MapReduce\\out");
    40. 40         if (fs.exists(localPath)){
    41. 41             fs.delete(localPath,true);
    42. 42         }
    43. 43         //设置输入路径,需要传递两个参数,即任务对象(job)以及输入路径
    44. 44         FileInputFormat.addInputPath(job,new Path("D:\\10.Java\\IDE\\yhinzhengjieData\\MyHadoop\\MapReduce\\screw.txt"));
    45. 45         //设置输出路径,需要传递两个参数,即任务对象(job)以及输出路径
    46. 46         FileOutputFormat.setOutputPath(job,localPath);
    47. 47         //设置Reduce的个数为2.
    48. 48         job.setNumReduceTasks(2);
    49. 49         //等待任务执行结束,将里面的值设置为true。
    50. 50         job.waitForCompletion(true);
    51. 51     }
    52. 52 }
    复制代码

      
    ScrewApp.java 文件内容

    2>.Reduce端代码



      
    1. 1 /*
    2. 2 @author :yinzhengjie
    3. 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
    4. 4 EMAIL:y1053419035@qq.com
    5. 5 */
    6. 6 package cn.org.yinzhengjie.srew;
    7. 7
    8. 8 import org.apache.hadoop.io.IntWritable;
    9. 9 import org.apache.hadoop.io.Text;
    10. 10 import org.apache.hadoop.mapreduce.Reducer;
    11. 11
    12. 12 import java.io.IOException;
    13. 13
    14. 14 public class ScrewReduce extends Reducer<Text,IntWritable,Text,IntWritable> {
    15. 15     @Override
    16. 16     protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
    17. 17         int count = 0;
    18. 18         for (IntWritable value : values) {
    19. 19             count += value.get();
    20. 20         }
    21. 21         context.write(key,new IntWritable(count));
    22. 22     }
    23. 23 }
    复制代码

      
    ScrewReduce.java 文件内容

    3>.Mapper端代码



      
    1. 1 /*
    2. 2 @author :yinzhengjie
    3. 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
    4. 4 EMAIL:y1053419035@qq.com
    5. 5 */
    6. 6 package cn.org.yinzhengjie.srew;
    7. 7
    8. 8 import org.apache.hadoop.io.IntWritable;
    9. 9 import org.apache.hadoop.io.LongWritable;
    10. 10 import org.apache.hadoop.io.Text;
    11. 11 import org.apache.hadoop.mapreduce.Mapper;
    12. 12
    13. 13 import java.io.IOException;
    14. 14
    15. 15 public class ScrewMapper extends Mapper<LongWritable,Text,Text,IntWritable> {
    16. 16     @Override
    17. 17     protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
    18. 18         String line = value.toString();
    19. 19
    20. 20         String[] arr = line.split(" ");
    21. 21
    22. 22         for (String word : arr) {
    23. 23             context.write(new Text(word),new IntWritable(1));
    24. 24         }
    25. 25     }
    26. 26 }
    复制代码

      
    ScrewMapper.java 文件内容

      执行以上代码,查看数据如下:

      
    三.解决数据倾斜方案之重新设计key
    1>.具体代码如下



      
    1. /*
    2. @author :yinzhengjie
    3. Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
    4. EMAIL:y1053419035@qq.com
    5. */
    6. package cn.org.yinzhengjie.srew;
    7. import org.apache.hadoop.io.IntWritable;
    8. import org.apache.hadoop.io.LongWritable;
    9. import org.apache.hadoop.io.Text;
    10. import org.apache.hadoop.mapreduce.Mapper;
    11. import java.io.IOException;
    12. import java.util.Random;
    13. public class ScrewMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    14.     //定义一个reduce变量
    15.     int reduces;
    16.     //定义一个随机数生成器变量
    17.     Random r;
    18.     /**
    19.      * setup方法是用于初始化值
    20.      */
    21.     @Override
    22.     protected void setup(Context context) throws IOException, InterruptedException {
    23.         //通过context.getNumReduceTasks()方法获取到用户配置的reduce个数。
    24.         reduces = context.getNumReduceTasks();
    25.         //生成一个随机数生成器
    26.         r = new Random();
    27.     }
    28.     @Override
    29.     protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
    30.         String line = value.toString();
    31.         String[] arr = line.split(" ");
    32.         for (String word : arr) {
    33.             //从reducs的范围中获取一个int类型的随机数赋值给randVal
    34.             int randVal = r.nextInt(reduces);
    35.             //重新定义key
    36.             String newWord = word+"_"+ randVal;
    37.             //将自定义的key赋初始值为1发给reduce端
    38.             context.write(new Text(newWord), new IntWritable(1));
    39.         }
    40.     }
    41. }
    复制代码

      
    ScrewMapper.java 文件内容




      
    1. 1 package cn.org.yinzhengjie.srew;
    2. 2
    3. 3 import org.apache.hadoop.io.IntWritable;
    4. 4 import org.apache.hadoop.io.LongWritable;
    5. 5 import org.apache.hadoop.io.Text;
    6. 6 import org.apache.hadoop.mapreduce.Mapper;
    7. 7
    8. 8 import java.io.IOException;
    9. 9
    10. 10 public class ScrewMapper2 extends Mapper<LongWritable,Text,Text,IntWritable> {
    11. 11
    12. 12     //处理的数据类似于“1_1    677”
    13. 13     @Override
    14. 14     protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
    15. 15         String line = value.toString();
    16. 16         //
    17. 17         String[] arr = line.split("\t");
    18. 18
    19. 19         //newKey
    20. 20         String newKey = arr[0].split("_")[0];
    21. 21
    22. 22         //newVAl
    23. 23         int newVal = Integer.parseInt(arr[1]);
    24. 24
    25. 25         context.write(new Text(newKey), new IntWritable(newVal));
    26. 26
    27. 27
    28. 28     }
    29. 29 }
    复制代码

      
    ScrewMapper2.java 文件内容




      
    1. 1 /*
    2. 2 @author :yinzhengjie
    3. 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
    4. 4 EMAIL:y1053419035@qq.com
    5. 5 */
    6. 6 package cn.org.yinzhengjie.srew;
    7. 7
    8. 8 import org.apache.hadoop.io.IntWritable;
    9. 9 import org.apache.hadoop.io.Text;
    10. 10 import org.apache.hadoop.mapreduce.Reducer;
    11. 11
    12. 12 import java.io.IOException;
    13. 13
    14. 14 public class ScrewReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
    15. 15     @Override
    16. 16     protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
    17. 17         int count = 0;
    18. 18         for (IntWritable value : values) {
    19. 19             count += value.get();
    20. 20         }
    21. 21         context.write(key,new IntWritable(count));
    22. 22     }
    23. 23 }
    复制代码

      
    ScrewReducer.java 文件内容




      
    1. 1 /*
    2. 2 @author :yinzhengjie
    3. 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
    4. 4 EMAIL:y1053419035@qq.com
    5. 5 */
    6. 6 package cn.org.yinzhengjie.srew;
    7. 7
    8. 8 import org.apache.hadoop.conf.Configuration;
    9. 9 import org.apache.hadoop.fs.FileSystem;
    10. 10 import org.apache.hadoop.fs.Path;
    11. 11 import org.apache.hadoop.io.IntWritable;
    12. 12 import org.apache.hadoop.io.Text;
    13. 13 import org.apache.hadoop.mapreduce.Job;
    14. 14 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    15. 15 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    16. 16
    17. 17 public class ScrewApp {
    18. 18     public static void main(String[] args) throws Exception {
    19. 19         //实例化一个Configuration,它会自动去加载本地的core-site.xml配置文件的fs.defaultFS属性。(该文件放在项目的resources目录即可。)
    20. 20         Configuration conf = new Configuration();
    21. 21         //将hdfs写入的路径定义在本地,需要修改默认为文件系统,这样就可以覆盖到之前在core-site.xml配置文件读取到的数据。
    22. 22         conf.set("fs.defaultFS","file:///");
    23. 23         //代码的入口点,初始化HDFS文件系统,此时我们需要把读取到的fs.defaultFS属性传给fs对象。
    24. 24         FileSystem fs = FileSystem.get(conf);
    25. 25         //创建一个任务对象job,别忘记把conf穿进去哟!
    26. 26         Job job = Job.getInstance(conf);
    27. 27         //给任务起个名字
    28. 28         job.setJobName("WordCount");
    29. 29         //指定main函数所在的类,也就是当前所在的类名
    30. 30         job.setJarByClass(ScrewApp.class);
    31. 31         //指定map的类名,这里指定咱们自定义的map程序即可
    32. 32         job.setMapperClass(ScrewMapper.class);
    33. 33         //指定reduce的类名,这里指定咱们自定义的reduce程序即可
    34. 34         job.setReducerClass(ScrewReducer.class);
    35. 35         //设置输出key的数据类型
    36. 36         job.setOutputKeyClass(Text.class);
    37. 37         //设置输出value的数据类型
    38. 38         job.setOutputValueClass(IntWritable.class);
    39. 39         Path localPath = new Path("D:\\10.Java\\IDE\\yhinzhengjieData\\MyHadoop\\MapReduce\\out");
    40. 40         if (fs.exists(localPath)){
    41. 41             fs.delete(localPath,true);
    42. 42         }
    43. 43         //设置输入路径,需要传递两个参数,即任务对象(job)以及输入路径
    44. 44         FileInputFormat.addInputPath(job,new Path("D:\\10.Java\\IDE\\yhinzhengjieData\\MyHadoop\\MapReduce\\screw.txt"));
    45. 45         //设置输出路径,需要传递两个参数,即任务对象(job)以及输出路径
    46. 46         FileOutputFormat.setOutputPath(job,localPath);
    47. 47         //设置Reduce的个数为2.
    48. 48         job.setNumReduceTasks(2);
    49. 49         //等待任务执行结束,将里面的值设置为true。
    50. 50         if (job.waitForCompletion(true)) {
    51. 51             //当第一个MapReduce结束之后,我们这里又启动了一个新的MapReduce,逻辑和上面类似。
    52. 52             Job job2 = Job.getInstance(conf);
    53. 53             job2.setJobName("Wordcount2");
    54. 54             job2.setJarByClass(ScrewApp.class);
    55. 55             job2.setMapperClass(ScrewMapper2.class);
    56. 56             job2.setReducerClass(ScrewReducer.class);
    57. 57             job2.setOutputKeyClass(Text.class);
    58. 58             job2.setOutputValueClass(IntWritable.class);
    59. 59             Path p2 = new Path("D:\\10.Java\\IDE\\yhinzhengjieData\\MyHadoop\\MapReduce\\out2");
    60. 60             if (fs.exists(p2)) {
    61. 61                 fs.delete(p2, true);
    62. 62             }
    63. 63             FileInputFormat.addInputPath(job2, localPath);
    64. 64             FileOutputFormat.setOutputPath(job2, p2);
    65. 65             //我们将第一个MapReduce的2个reducer的处理结果放在新的一个MapReduce中只启用一个MapReduce。
    66. 66             job2.setNumReduceTasks(1);
    67. 67             job2.waitForCompletion(true);
    68. 68         }
    69. 69     }
    70. 70 }
    复制代码

      
    ScrewApp.java 文件内容

    2>.检测实验结果
      “D:\\10.Java\\IDE\\yhinzhengjieData\\MyHadoop\\MapReduce\\out” 目录内容如下:

      “D:\\10.Java\\IDE\\yhinzhengjieData\\MyHadoop\\MapReduce\\out2” 目录内容如下:

      
    四.解决数据倾斜方案之使用随机分区
    1>.具体代码如下



      
    1. 1 /*
    2. 2 @author :yinzhengjie
    3. 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
    4. 4 EMAIL:y1053419035@qq.com
    5. 5 */
    6. 6 package cn.org.yinzhengjie.screwpartition;
    7. 7
    8. 8 import org.apache.hadoop.io.IntWritable;
    9. 9 import org.apache.hadoop.io.LongWritable;
    10. 10 import org.apache.hadoop.io.Text;
    11. 11 import org.apache.hadoop.mapreduce.Mapper;
    12. 12
    13. 13 import java.io.IOException;
    14. 14
    15. 15 public class Screw2Mapper extends Mapper<LongWritable,Text,Text,IntWritable> {
    16. 16
    17. 17     @Override
    18. 18     protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
    19. 19
    20. 20         String line = value.toString();
    21. 21
    22. 22         String[] arr = line.split(" ");
    23. 23
    24. 24         for(String word : arr){
    25. 25             context.write(new Text(word), new IntWritable(1));
    26. 26
    27. 27         }
    28. 28
    29. 29
    30. 30     }
    31. 31 }
    复制代码

      
    Screw2Mapper.java 文件内容




      
    1. 1 /*
    2. 2 @author :yinzhengjie
    3. 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
    4. 4 EMAIL:y1053419035@qq.com
    5. 5 */
    6. 6 package cn.org.yinzhengjie.screwpartition;
    7. 7
    8. 8 import org.apache.hadoop.io.IntWritable;
    9. 9 import org.apache.hadoop.io.Text;
    10. 10 import org.apache.hadoop.mapreduce.Partitioner;
    11. 11
    12. 12 import java.util.Random;
    13. 13
    14. 14 public class Screw2Partition extends Partitioner<Text, IntWritable> {
    15. 15     @Override
    16. 16     public int getPartition(Text text, IntWritable intWritable, int numPartitions) {
    17. 17         Random r = new Random();
    18. 18         //返回的是分区的随机的一个ID
    19. 19         return r.nextInt(numPartitions);
    20. 20     }
    21. 21 }
    复制代码

      
    Screw2Partition.java 文件内容




      
    1. 1 /*
    2. 2 @author :yinzhengjie
    3. 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
    4. 4 EMAIL:y1053419035@qq.com
    5. 5 */
    6. 6 package cn.org.yinzhengjie.screwpartition;
    7. 7
    8. 8 import org.apache.hadoop.io.IntWritable;
    9. 9 import org.apache.hadoop.io.Text;
    10. 10 import org.apache.hadoop.mapreduce.Reducer;
    11. 11
    12. 12 import java.io.IOException;
    13. 13
    14. 14 public class Screw2Reducer extends Reducer<Text,IntWritable,Text,IntWritable> {
    15. 15     @Override
    16. 16     protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
    17. 17         int sum = 0;
    18. 18         for(IntWritable value : values){
    19. 19             sum += value.get();
    20. 20         }
    21. 21         context.write(key,new IntWritable(sum));
    22. 22     }
    23. 23 }
    复制代码

      
    Screw2Reducer.java 文件内容




      
    1. 1 /*
    2. 2 @author :yinzhengjie
    3. 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
    4. 4 EMAIL:y1053419035@qq.com
    5. 5 */
    6. 6 package cn.org.yinzhengjie.screwpartition;
    7. 7
    8. 8 import org.apache.hadoop.conf.Configuration;
    9. 9 import org.apache.hadoop.fs.FileSystem;
    10. 10 import org.apache.hadoop.fs.Path;
    11. 11 import org.apache.hadoop.io.IntWritable;
    12. 12 import org.apache.hadoop.io.Text;
    13. 13 import org.apache.hadoop.mapreduce.Job;
    14. 14 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    15. 15 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    16. 16
    17. 17 public class Screw2App {
    18. 18     public static void main(String[] args) throws Exception {
    19. 19         Configuration conf = new Configuration();
    20. 20         conf.set("fs.defaultFS", "file:///");
    21. 21         FileSystem fs = FileSystem.get(conf);
    22. 22         Job job = Job.getInstance(conf);
    23. 23         job.setJobName("Wordcount");
    24. 24         job.setJarByClass(Screw2App.class);
    25. 25         job.setMapperClass(Screw2Mapper.class);
    26. 26         job.setReducerClass(Screw2Reducer.class);
    27. 27         job.setPartitionerClass(Screw2Partition.class);
    28. 28         job.setOutputKeyClass(Text.class);
    29. 29         job.setOutputValueClass(IntWritable.class);
    30. 30         Path p = new Path("D:\\10.Java\\IDE\\yhinzhengjieData\\MyHadoop\\MapReduce\\out");
    31. 31         if (fs.exists(p)) {
    32. 32             fs.delete(p, true);
    33. 33         }
    34. 34         FileInputFormat.addInputPath(job, new Path("D:\\10.Java\\IDE\\yhinzhengjieData\\MyHadoop\\MapReduce\\screw.txt"));
    35. 35         FileOutputFormat.setOutputPath(job, p);
    36. 36         job.setNumReduceTasks(2);
    37. 37         job.waitForCompletion(true);
    38. 38     }
    39. 39 }
    复制代码

      
    Screw2App.java 文件内容

    2>.检测实验结果
      “D:\\10.Java\\IDE\\yhinzhengjieData\\MyHadoop\\MapReduce\\out” 目录内容如下:

       “D:\\10.Java\\IDE\\yhinzhengjieData\\MyHadoop\\MapReduce\\out2” 目录内容如下:
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-4-26 03:15 , Processed in 0.455397 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表