MapReduce HelloWorld

来源:互联网 发布:淘宝卖家骂人 编辑:程序博客网 时间:2024/05/22 03:43

MapReduce HelloWorld

    博客分类: 
  • Java
 

在开始之前,假设你已经配置好了Hadoop的环境。如果没有,可以参考http://www.linuxidc.com/Linux/2012-02/53927.htm

 

主要使用的软件:

1:java7u25

2:Hadoop1.2.0

3:Eclipse Kepler

4:OS:Ubuntu 12.0.4

 

先看一下项目的整体结构:



 

需要注意的是,项目需要导入Hadoop安装目录下和lib目录下所有的jar包。

 

另外需要在shell命令下开启Hadoop。



 

接下来编写简单的map函数和reduce函数,并执行这个作业。

Java代码  收藏代码
  1. package org.hadoop.tutorial;  
  2. import java.io.IOException;  
  3. import java.util.StringTokenizer;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.fs.Path;  
  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.output.FileOutputFormat;  
  14. import org.apache.hadoop.util.GenericOptionsParser;  
  15.   
  16. public class MyWordCount {  
  17.   
  18.   public static class TokenizerMapper   
  19.        extends Mapper<Object, Text, Text, IntWritable>{  
  20.       
  21.     private final static IntWritable one = new IntWritable(1);  
  22.     private Text word = new Text();  
  23.         
  24.     public void map(Object key, Text value, Context context  
  25.                     ) throws IOException, InterruptedException {  
  26.       StringTokenizer itr = new StringTokenizer(value.toString());  
  27.       while (itr.hasMoreTokens()) {  
  28.         word.set(itr.nextToken());  
  29.         context.write(word, one);  
  30.       }  
  31.     }  
  32.   }  
  33.     
  34.   public static class IntSumReducer   
  35.        extends Reducer<Text,IntWritable,Text,IntWritable> {  
  36.     private IntWritable result = new IntWritable();  
  37.   
  38.     public void reduce(Text key, Iterable<IntWritable> values,   
  39.                        Context context  
  40.                        ) throws IOException, InterruptedException {  
  41.       int sum = 0;  
  42.       for (IntWritable val : values) {  
  43.         sum += val.get();  
  44.       }  
  45.       result.set(sum);  
  46.       context.write(key, result);  
  47.     }  
  48.   }  
  49.   
  50.   public static void main(String[] args) throws Exception {  
  51.     Configuration conf = new Configuration();  
  52.     String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();  
  53.     if (otherArgs.length != 2) {  
  54.       System.err.println("Usage: wordcount <in> <out>");  
  55.       System.exit(2);  
  56.     }  
  57.     Job job = new Job(conf, "word count");  
  58.     job.setJarByClass(MyWordCount.class);  
  59.     job.setMapperClass(TokenizerMapper.class);  
  60.     job.setCombinerClass(IntSumReducer.class);  
  61.     job.setReducerClass(IntSumReducer.class);  
  62.     job.setOutputKeyClass(Text.class);  
  63.     job.setOutputValueClass(IntWritable.class);  
  64.     FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  65.     FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  66.     System.exit(job.waitForCompletion(true) ? 0 : 1);  
  67.   }  
  68. }  

 

点击Eclipse的Run Configuration,传入参数words.txt和output。words.txt是我们需要分析处理的文本文件。

Txt代码  收藏代码
  1. hello world hello hadoop hello ubuntu  
  2. This is a simple text file  

 output是输出目录。

 

执行这个main方法,下面是从控制台返回的信息:

Console代码  收藏代码
  1. 13/07/28 13:01:09 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable  
  2. 13/07/28 13:01:09 WARN mapred.JobClient: No job jar file set.  User classes may not be found. See JobConf(Class) or JobConf#setJar(String).  
  3. 13/07/28 13:01:09 INFO input.FileInputFormat: Total input paths to process : 1  
  4. 13/07/28 13:01:09 WARN snappy.LoadSnappy: Snappy native library not loaded  
  5. 13/07/28 13:01:09 INFO mapred.JobClient: Running job: job_local2121945460_0001  
  6. 13/07/28 13:01:09 INFO mapred.LocalJobRunner: Waiting for map tasks  
  7. 13/07/28 13:01:09 INFO mapred.LocalJobRunner: Starting task: attempt_local2121945460_0001_m_000000_0  
  8. 13/07/28 13:01:09 INFO util.ProcessTree: setsid exited with exit code 0  
  9. 13/07/28 13:01:09 INFO mapred.Task:  Using ResourceCalculatorPlugin : org.apache.hadoop.util.LinuxResourceCalculatorPlugin@1282b42  
  10. 13/07/28 13:01:09 INFO mapred.MapTask: Processing split: file:/home/tsw/workspace/FirstHadoopPrj/words.txt:0+64  
  11. 13/07/28 13:01:09 INFO mapred.MapTask: io.sort.mb = 100  
  12. 13/07/28 13:01:09 INFO mapred.MapTask: data buffer = 79691776/99614720  
  13. 13/07/28 13:01:09 INFO mapred.MapTask: record buffer = 262144/327680  
  14. 13/07/28 13:01:09 INFO mapred.MapTask: Starting flush of map output  
  15. 13/07/28 13:01:09 INFO mapred.MapTask: Finished spill 0  
  16. 13/07/28 13:01:09 INFO mapred.Task: Task:attempt_local2121945460_0001_m_000000_0 is done. And is in the process of commiting  
  17. 13/07/28 13:01:09 INFO mapred.LocalJobRunner:   
  18. 13/07/28 13:01:09 INFO mapred.Task: Task 'attempt_local2121945460_0001_m_000000_0' done.  
  19. 13/07/28 13:01:09 INFO mapred.LocalJobRunner: Finishing task: attempt_local2121945460_0001_m_000000_0  
  20. 13/07/28 13:01:09 INFO mapred.LocalJobRunner: Map task executor complete.  
  21. 13/07/28 13:01:09 INFO mapred.Task:  Using ResourceCalculatorPlugin : org.apache.hadoop.util.LinuxResourceCalculatorPlugin@50a6d4  
  22. 13/07/28 13:01:09 INFO mapred.LocalJobRunner:   
  23. 13/07/28 13:01:09 INFO mapred.Merger: Merging 1 sorted segments  
  24. 13/07/28 13:01:09 INFO mapred.Merger: Down to the last merge-pass, with 1 segments left of total size: 115 bytes  
  25. 13/07/28 13:01:09 INFO mapred.LocalJobRunner:   
  26. 13/07/28 13:01:09 INFO mapred.Task: Task:attempt_local2121945460_0001_r_000000_0 is done. And is in the process of commiting  
  27. 13/07/28 13:01:09 INFO mapred.LocalJobRunner:   
  28. 13/07/28 13:01:09 INFO mapred.Task: Task attempt_local2121945460_0001_r_000000_0 is allowed to commit now  
  29. 13/07/28 13:01:09 INFO output.FileOutputCommitter: Saved output of task 'attempt_local2121945460_0001_r_000000_0' to output  
  30. 13/07/28 13:01:09 INFO mapred.LocalJobRunner: reduce > reduce  
  31. 13/07/28 13:01:09 INFO mapred.Task: Task 'attempt_local2121945460_0001_r_000000_0' done.  
  32. 13/07/28 13:01:10 INFO mapred.JobClient:  map 100% reduce 100%  
  33. 13/07/28 13:01:10 INFO mapred.JobClient: Job complete: job_local2121945460_0001  
  34. 13/07/28 13:01:10 INFO mapred.JobClient: Counters: 20  
  35. 13/07/28 13:01:10 INFO mapred.JobClient:   File Output Format Counters   
  36. 13/07/28 13:01:10 INFO mapred.JobClient:     Bytes Written=85  
  37. 13/07/28 13:01:10 INFO mapred.JobClient:   File Input Format Counters   
  38. 13/07/28 13:01:10 INFO mapred.JobClient:     Bytes Read=64  
  39. 13/07/28 13:01:10 INFO mapred.JobClient:   FileSystemCounters  
  40. 13/07/28 13:01:10 INFO mapred.JobClient:     FILE_BYTES_READ=583  
  41. 13/07/28 13:01:10 INFO mapred.JobClient:     FILE_BYTES_WRITTEN=102109  
  42. 13/07/28 13:01:10 INFO mapred.JobClient:   Map-Reduce Framework  
  43. 13/07/28 13:01:10 INFO mapred.JobClient:     Reduce input groups=10  
  44. 13/07/28 13:01:10 INFO mapred.JobClient:     Map output materialized bytes=119  
  45. 13/07/28 13:01:10 INFO mapred.JobClient:     Combine output records=10  
  46. 13/07/28 13:01:10 INFO mapred.JobClient:     Map input records=2  
  47. 13/07/28 13:01:10 INFO mapred.JobClient:     Reduce shuffle bytes=0  
  48. 13/07/28 13:01:10 INFO mapred.JobClient:     Physical memory (bytes) snapshot=0  
  49. 13/07/28 13:01:10 INFO mapred.JobClient:     Reduce output records=10  
  50. 13/07/28 13:01:10 INFO mapred.JobClient:     Spilled Records=20  
  51. 13/07/28 13:01:10 INFO mapred.JobClient:     Map output bytes=113  
  52. 13/07/28 13:01:10 INFO mapred.JobClient:     Total committed heap usage (bytes)=292421632  
  53. 13/07/28 13:01:10 INFO mapred.JobClient:     CPU time spent (ms)=0  
  54. 13/07/28 13:01:10 INFO mapred.JobClient:     Virtual memory (bytes) snapshot=0  
  55. 13/07/28 13:01:10 INFO mapred.JobClient:     SPLIT_RAW_BYTES=114  
  56. 13/07/28 13:01:10 INFO mapred.JobClient:     Map output records=12  
  57. 13/07/28 13:01:10 INFO mapred.JobClient:     Combine input records=12  
  58. 13/07/28 13:01:10 INFO mapred.JobClient:     Reduce input records=10  

 

在你的项目里可以发现生成的output目录,在该目录里有两个文件:part-r-0000(r代表reduce,0000代表分块号)和_SUCCESS。

打开part-r-0000文件:



 

这就是Hadoop的HelloWorld-单词计数

(上述代码取自于hadoop-examples jar包的WordCount.java类)


转自:http://201211131343.iteye.com/blog/1914558