Hadoop之道--MapReduce之Hello World实例wordcount

adoop版本:1.1.2

集成开发平台:Eclipse SDK 3.5.1

原创作品,转载请标明:http://blog.csdn.net/yming0221/article/details/9013381


1. 首先定义DFS Location(具体的环境搭建请看前面的博文)



2.下面即是Hello World实例

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

3. 运行结果

[plain]  view plain copy
  1. 13/06/03 14:45:52 INFO input.FileInputFormat: Total input paths to process : 2  
  2. 13/06/03 14:45:52 WARN snappy.LoadSnappy: Snappy native library not loaded  
  3. 13/06/03 14:45:52 INFO mapred.JobClient: Running job: job_local_0001  
  4. 13/06/03 14:45:52 INFO util.ProcessTree: setsid exited with exit code 0  
  5. 13/06/03 14:45:52 INFO mapred.Task:  Using ResourceCalculatorPlugin : org.apache.hadoop.util.LinuxResourceCalculatorPlugin@2b96021e  
  6. 13/06/03 14:45:52 INFO mapred.MapTask: io.sort.mb = 100  
  7. 13/06/03 14:45:53 INFO mapred.MapTask: data buffer = 79691776/99614720  
  8. 13/06/03 14:45:53 INFO mapred.MapTask: record buffer = 262144/327680  
  9. 13/06/03 14:45:53 INFO mapred.MapTask: Starting flush of map output  
  10. 13/06/03 14:45:53 INFO mapred.MapTask: Finished spill 0  
  11. 13/06/03 14:45:53 INFO mapred.Task: Task:attempt_local_0001_m_000000_0 is done. And is in the process of commiting  
  12. 13/06/03 14:45:53 INFO mapred.LocalJobRunner:   
  13. 13/06/03 14:45:53 INFO mapred.Task: Task 'attempt_local_0001_m_000000_0' done.  
  14. 13/06/03 14:45:53 INFO mapred.Task:  Using ResourceCalculatorPlugin : org.apache.hadoop.util.LinuxResourceCalculatorPlugin@3621767f  
  15. 13/06/03 14:45:53 INFO mapred.MapTask: io.sort.mb = 100  
  16. 13/06/03 14:45:53 INFO mapred.MapTask: data buffer = 79691776/99614720  
  17. 13/06/03 14:45:53 INFO mapred.MapTask: record buffer = 262144/327680  
  18. 13/06/03 14:45:53 INFO mapred.MapTask: Starting flush of map output  
  19. 13/06/03 14:45:53 INFO mapred.MapTask: Finished spill 0  
  20. 13/06/03 14:45:53 INFO mapred.Task: Task:attempt_local_0001_m_000001_0 is done. And is in the process of commiting  
  21. 13/06/03 14:45:53 INFO mapred.LocalJobRunner:   
  22. 13/06/03 14:45:53 INFO mapred.Task: Task 'attempt_local_0001_m_000001_0' done.  
  23. 13/06/03 14:45:53 INFO mapred.Task:  Using ResourceCalculatorPlugin : org.apache.hadoop.util.LinuxResourceCalculatorPlugin@76d6d675  
  24. 13/06/03 14:45:53 INFO mapred.LocalJobRunner:   
  25. 13/06/03 14:45:53 INFO mapred.Merger: Merging 2 sorted segments  
  26. 13/06/03 14:45:53 INFO mapred.Merger: Down to the last merge-pass, with 2 segments left of total size: 53 bytes  
  27. 13/06/03 14:45:53 INFO mapred.LocalJobRunner:   
  28. 13/06/03 14:45:53 INFO mapred.JobClient:  map 100% reduce 0%  
  29. 13/06/03 14:45:53 INFO mapred.Task: Task:attempt_local_0001_r_000000_0 is done. And is in the process of commiting  
  30. 13/06/03 14:45:53 INFO mapred.LocalJobRunner:   
  31. 13/06/03 14:45:53 INFO mapred.Task: Task attempt_local_0001_r_000000_0 is allowed to commit now  
  32. 13/06/03 14:45:53 INFO output.FileOutputCommitter: Saved output of task 'attempt_local_0001_r_000000_0' to output  
  33. 13/06/03 14:45:53 INFO mapred.LocalJobRunner: reduce > reduce  
  34. 13/06/03 14:45:53 INFO mapred.Task: Task 'attempt_local_0001_r_000000_0' done.  
  35. 13/06/03 14:45:54 INFO mapred.JobClient:  map 100% reduce 100%  
  36. 13/06/03 14:45:54 INFO mapred.JobClient: Job complete: job_local_0001  
  37. 13/06/03 14:45:54 INFO mapred.JobClient: Counters: 22  
  38. 13/06/03 14:45:54 INFO mapred.JobClient:   File Output Format Counters   
  39. 13/06/03 14:45:54 INFO mapred.JobClient:     Bytes Written=25  
  40. 13/06/03 14:45:54 INFO mapred.JobClient:   FileSystemCounters  
  41. 13/06/03 14:45:54 INFO mapred.JobClient:     FILE_BYTES_READ=18029  
  42. 13/06/03 14:45:54 INFO mapred.JobClient:     HDFS_BYTES_READ=63  
  43. 13/06/03 14:45:54 INFO mapred.JobClient:     FILE_BYTES_WRITTEN=213880  
  44. 13/06/03 14:45:54 INFO mapred.JobClient:     HDFS_BYTES_WRITTEN=25  
  45. 13/06/03 14:45:54 INFO mapred.JobClient:   File Input Format Counters   
  46. 13/06/03 14:45:54 INFO mapred.JobClient:     Bytes Read=25  
  47. 13/06/03 14:45:54 INFO mapred.JobClient:   Map-Reduce Framework  
  48. 13/06/03 14:45:54 INFO mapred.JobClient:     Reduce input groups=3  
  49. 13/06/03 14:45:54 INFO mapred.JobClient:     Map output materialized bytes=61  
  50. 13/06/03 14:45:54 INFO mapred.JobClient:     Combine output records=4  
  51. 13/06/03 14:45:54 INFO mapred.JobClient:     Map input records=2  
  52. 13/06/03 14:45:54 INFO mapred.JobClient:     Reduce shuffle bytes=0  
  53. 13/06/03 14:45:54 INFO mapred.JobClient:     Physical memory (bytes) snapshot=0  
  54. 13/06/03 14:45:54 INFO mapred.JobClient:     Reduce output records=3  
  55. 13/06/03 14:45:54 INFO mapred.JobClient:     Spilled Records=8  
  56. 13/06/03 14:45:54 INFO mapred.JobClient:     Map output bytes=41  
  57. 13/06/03 14:45:54 INFO mapred.JobClient:     CPU time spent (ms)=0  
  58. 13/06/03 14:45:54 INFO mapred.JobClient:     Total committed heap usage (bytes)=683409408  
  59. 13/06/03 14:45:54 INFO mapred.JobClient:     Virtual memory (bytes) snapshot=0  
  60. 13/06/03 14:45:54 INFO mapred.JobClient:     Combine input records=4  
  61. 13/06/03 14:45:54 INFO mapred.JobClient:     Map output records=4  
  62. 13/06/03 14:45:54 INFO mapred.JobClient:     SPLIT_RAW_BYTES=226  
  63. 13/06/03 14:45:54 INFO mapred.JobClient:     Reduce input records=4  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值