分享

Eclipse下使用Hadoop单机模式调试MapReduce程序

 埃德温会馆 2015-03-09

在单机模式下Hadoop不会使用HDFS,也不会开启任何Hadoop守护进程,所有程序将在一个JVM上运行并且最多只允许拥有一个reducer

在Eclipse中新创建一个hadoop-test的Java工程(特别要注意的是Hadoop需要1.6或1.6以上版本的JDK

在Hadoop的官网http://apache./apache-mirror/hadoop/common/下载hadoop-1.2.1.tar.gz

解压hadoop-1.2.1.tar.gz得到hadoop-1.2.1目录

将hadoop-1.2.1目录下和hadoop-1.2.1\lib目录下的jar包导入到hadoop-test工程中

接下来编写MapReduce程序(该程序用来统计每月收支结余)

Map:

  1. import java.io.IOException;  
  2. import org.apache.hadoop.io.LongWritable;  
  3. import org.apache.hadoop.io.Text;  
  4. import org.apache.hadoop.mapred.MapReduceBase;  
  5. import org.apache.hadoop.mapred.Mapper;  
  6. import org.apache.hadoop.mapred.OutputCollector;  
  7. import org.apache.hadoop.mapred.Reporter;  
  8.   
  9. public class MapBus extends MapReduceBase   
  10.         implements Mapper<LongWritable, Text, Text, LongWritable> {  
  11.     @Override  
  12.     public void map(LongWritable key, Text date,   
  13.             OutputCollector<Text, LongWritable> output,  
  14.             Reporter reporter) throws IOException {  
  15.         //2013-01-11,-200  
  16.         String line = date.toString();  
  17.         if(line.contains(",")){  
  18.             String[] tmp = line.split(",");  
  19.             String month = tmp[0].substring(5, 7);  
  20.             int money = Integer.valueOf(tmp[1]).intValue();  
  21.             output.collect(new Text(month), new LongWritable(money));  
  22.         }  
  23.     }  
  24. }  

Reduce:

  1. import java.io.IOException;  
  2. import java.util.Iterator;  
  3. import org.apache.hadoop.io.LongWritable;  
  4. import org.apache.hadoop.io.Text;  
  5. import org.apache.hadoop.mapred.MapReduceBase;  
  6. import org.apache.hadoop.mapred.OutputCollector;  
  7. import org.apache.hadoop.mapred.Reducer;  
  8. import org.apache.hadoop.mapred.Reporter;  
  9.   
  10. public class ReduceBus extends MapReduceBase   
  11.         implements Reducer<Text, LongWritable, Text, LongWritable> {  
  12.     @Override  
  13.     public void reduce(Text month, Iterator<LongWritable> money,  
  14.             OutputCollector<Text, LongWritable> output, Reporter reporter)  
  15.             throws IOException {  
  16.         int total_money = 0;  
  17.         while(money.hasNext()){  
  18.             total_money += money.next().get();  
  19.         }  
  20.         output.collect(month, new LongWritable(total_money));  
  21.     }  
  22. }  

Main:

  1. import org.apache.hadoop.fs.Path;  
  2. import org.apache.hadoop.io.LongWritable;  
  3. import org.apache.hadoop.io.Text;  
  4. import org.apache.hadoop.mapred.FileInputFormat;  
  5. import org.apache.hadoop.mapred.FileOutputFormat;  
  6. import org.apache.hadoop.mapred.JobClient;  
  7. import org.apache.hadoop.mapred.JobConf;  
  8.   
  9. public class Wallet {  
  10.     public static void main(String[] args){  
  11.         if(args.length != 2){  
  12.             System.err.println("param error!");  
  13.             System.exit(-1);  
  14.         }  
  15.           
  16.         JobConf jobConf = new JobConf(Wallet.class);  
  17.         jobConf.setJobName("My Wallet");  
  18.           
  19.         FileInputFormat.addInputPath(jobConf, new Path(args[0]));  
  20.         FileOutputFormat.setOutputPath(jobConf, new Path(args[1]));  
  21.         jobConf.setMapperClass(MapBus.class);  
  22.         jobConf.setReducerClass(ReduceBus.class);  
  23.         jobConf.setOutputKeyClass(Text.class);  
  24.         jobConf.setOutputValueClass(LongWritable.class);  
  25.           
  26.         try{  
  27.             JobClient.runJob(jobConf);  
  28.         }catch(Exception e){  
  29.             e.printStackTrace();  
  30.         }  
  31.     }  
  32. }  

还需准备待分析的文件,在E:\cygwin_root\home\input路径下创建2个文件,一个文件名为:2013-01.txt,另一个文件名为:2013-02.txt

2013-01.txt:

  1. 2013-01-01,100  
  2. 2013-01-02,-100  
  3. 2013-01-07,100  
  4. 2013-01-10,-100  
  5. 2013-01-11,100  
  6. 2013-01-21,-100  
  7. 2013-01-22,100  
  8. 2013-01-25,-100  
  9. 2013-01-27,100  
  10. 2013-01-18,-100  
  11. 2013-01-09,500  

2013-02.txt:

  1. 2013-02-01,100  

设置好运行参数后,就可以通过Run As -> Java Application运行MapReduce程序了

  1. java.io.IOException: Failed to set permissions of path:   
  2. \tmp\hadoop-linkage\mapred\staging\linkage1150562408\.staging to 0700  

报这个错误的主要原因是后期的hadoop版本增加了对文件路径的校验,修改方式比较简单,将hadoop-core-1.2.1.jar替换为hadoop-0.20.2-core.jar即可

下面是MapReduce程序运行时打印的日志

  1. 14/02/11 10:54:16 INFO jvm.JvmMetrics: Initializing JVM Metrics with processName=JobTracker, sessionId=  
  2. 14/02/11 10:54:16 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.  
  3. 14/02/11 10:54:16 WARN mapred.JobClient: No job jar file set.  User classes may not be found. See JobConf(Class) or JobConf#setJar(String).  
  4. 14/02/11 10:54:16 INFO mapred.FileInputFormat: Total input paths to process : 2  
  5. 14/02/11 10:54:17 INFO mapred.JobClient: Running job: job_local_0001  
  6. 14/02/11 10:54:17 INFO mapred.FileInputFormat: Total input paths to process : 2  
  7. 14/02/11 10:54:17 INFO mapred.MapTask: numReduceTasks: 1  
  8. 14/02/11 10:54:17 INFO mapred.MapTask: io.sort.mb = 100  
  9. 14/02/11 10:54:17 INFO mapred.MapTask: data buffer = 79691776/99614720  
  10. 14/02/11 10:54:17 INFO mapred.MapTask: record buffer = 262144/327680  
  11. 14/02/11 10:54:17 INFO mapred.MapTask: Starting flush of map output  
  12. 14/02/11 10:54:18 INFO mapred.MapTask: Finished spill 0  
  13. 14/02/11 10:54:18 INFO mapred.TaskRunner: Task:attempt_local_0001_m_000000_0 is done. And is in the process of commiting  
  14. 14/02/11 10:54:18 INFO mapred.LocalJobRunner: file:/E:/cygwin_root/home/input/2013-01.txt:0+179  
  15. 14/02/11 10:54:18 INFO mapred.TaskRunner: Task 'attempt_local_0001_m_000000_0' done.  
  16. 14/02/11 10:54:18 INFO mapred.MapTask: numReduceTasks: 1  
  17. 14/02/11 10:54:18 INFO mapred.MapTask: io.sort.mb = 100  
  18. 14/02/11 10:54:18 INFO mapred.MapTask: data buffer = 79691776/99614720  
  19. 14/02/11 10:54:18 INFO mapred.MapTask: record buffer = 262144/327680  
  20. 14/02/11 10:54:18 INFO mapred.MapTask: Starting flush of map output  
  21. 14/02/11 10:54:18 INFO mapred.MapTask: Finished spill 0  
  22. 14/02/11 10:54:18 INFO mapred.TaskRunner: Task:attempt_local_0001_m_000001_0 is done. And is in the process of commiting  
  23. 14/02/11 10:54:18 INFO mapred.LocalJobRunner: file:/E:/cygwin_root/home/input/2013-02.txt:0+16  
  24. 14/02/11 10:54:18 INFO mapred.TaskRunner: Task 'attempt_local_0001_m_000001_0' done.  
  25. 14/02/11 10:54:18 INFO mapred.LocalJobRunner:   
  26. 14/02/11 10:54:18 INFO mapred.Merger: Merging 2 sorted segments  
  27. 14/02/11 10:54:18 INFO mapred.Merger: Down to the last merge-pass, with 2 segments left of total size: 160 bytes  
  28. 14/02/11 10:54:18 INFO mapred.LocalJobRunner:   
  29. 14/02/11 10:54:18 INFO mapred.TaskRunner: Task:attempt_local_0001_r_000000_0 is done. And is in the process of commiting  
  30. 14/02/11 10:54:18 INFO mapred.LocalJobRunner:   
  31. 14/02/11 10:54:18 INFO mapred.TaskRunner: Task attempt_local_0001_r_000000_0 is allowed to commit now  
  32. 14/02/11 10:54:18 INFO mapred.FileOutputCommitter: Saved output of task 'attempt_local_0001_r_000000_0' to file:/E:/cygwin_root/home/output  
  33. 14/02/11 10:54:18 INFO mapred.LocalJobRunner: reduce > reduce  
  34. 14/02/11 10:54:18 INFO mapred.TaskRunner: Task 'attempt_local_0001_r_000000_0' done.  
  35. 14/02/11 10:54:18 INFO mapred.JobClient:  map 100% reduce 100%  
  36. 14/02/11 10:54:18 INFO mapred.JobClient: Job complete: job_local_0001  
  37. 14/02/11 10:54:18 INFO mapred.JobClient: Counters: 13  
  38. 14/02/11 10:54:18 INFO mapred.JobClient:   FileSystemCounters  
  39. 14/02/11 10:54:18 INFO mapred.JobClient:     FILE_BYTES_READ=39797  
  40. 14/02/11 10:54:18 INFO mapred.JobClient:     FILE_BYTES_WRITTEN=80473  
  41. 14/02/11 10:54:18 INFO mapred.JobClient:   Map-Reduce Framework  
  42. 14/02/11 10:54:18 INFO mapred.JobClient:     Reduce input groups=2  
  43. 14/02/11 10:54:18 INFO mapred.JobClient:     Combine output records=0  
  44. 14/02/11 10:54:18 INFO mapred.JobClient:     Map input records=12  
  45. 14/02/11 10:54:18 INFO mapred.JobClient:     Reduce shuffle bytes=0  
  46. 14/02/11 10:54:18 INFO mapred.JobClient:     Reduce output records=2  
  47. 14/02/11 10:54:18 INFO mapred.JobClient:     Spilled Records=24  
  48. 14/02/11 10:54:18 INFO mapred.JobClient:     Map output bytes=132  
  49. 14/02/11 10:54:18 INFO mapred.JobClient:     Map input bytes=195  
  50. 14/02/11 10:54:18 INFO mapred.JobClient:     Combine input records=0  
  51. 14/02/11 10:54:18 INFO mapred.JobClient:     Map output records=12  
  52. 14/02/11 10:54:18 INFO mapred.JobClient:     Reduce input records=12  

运行完成后将在E:\cygwin_root\home\output路径下生成2个文件:.part-00000.crc和part-00000。.part-00000.crc为一二进制文件,是一个保存了part-00000文件校验和的内部文件;part-00000文件中保存了最终的统计结果

  1. 01  500  
  2. 02  100  

特别要注意的是每次运行前都需要先将输出路径删掉,否则会报

  1. org.apache.hadoop.mapred.FileAlreadyExistsException:   
  2. Output directory file:/E:/cygwin_root/home/output already exists  

Hadoop做这个校验的目的是为了避免上一次MapReduce程序没有完成时,再次运行MapReduce程序所产生的中间文件会覆盖掉上一次运行产生的中间文件

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多