Hadoop2.7.3 mapreduce(一)原理及"hello world"实例

MapReduce编程模型

【1】先对输入的信息进行切片处理。

【2】每个map函数对所划分的数据并行处理,产生不同的中间结果输出。

【3】对map的中间结果数据进行收集整理(aggregate & shuffle)处理,交给reduce。

【4】reduce进行计算最终结果。

【5】汇总所有reduce的输出结果。



【名词解释】

ResourceManager:是YARN资源控制框架的中心模块,负责集群中所有的资源的统一管理和分配。它接收来自NM(NodeManager)的汇报,建立AM,并将资源派送给AM(ApplicationMaster)。

NodeManager:简称NM,NodeManager是ResourceManager在每台机器的上代理,负责容器的管理,并监控他们的资源使用情况(cpu,内存,磁盘及网络等),以及向 ResourceManager提供这些资源使用报告。

ApplicationMaster:以下简称AM。YARN中每个应用都会启动一个AM,负责向RM申请资源,请求NM启动container,并告诉container做什么事情。

Container:资源容器。YARN中所有的应用都是在container之上运行的。AM也是在container上运行的,不过AM的container是RM申请的。


【用Java来实现WordCount单词计数的功能】

[java]  view plain  copy
  1. package com.yc.hadoop42_003_mapreduce;  
  2.   
  3. import java.io.IOException;  
  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.LongWritable;  
  9. import org.apache.hadoop.io.Text;  
  10. import org.apache.hadoop.mapreduce.Job;  
  11. import org.apache.hadoop.mapreduce.Mapper;  
  12. import org.apache.hadoop.mapreduce.Reducer;  
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  15.   
  16. public class MyWordCount {  
  17.   
  18.         //Mapper静态内部类  
  19.     public static class MyWordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {  
  20.   
  21.         public static final IntWritable ONE = new IntWritable(1);  
  22.   
  23.         @Override  
  24.         protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)  
  25.                 throws IOException, InterruptedException {  
  26.             //按空格分割,map默认的value是每一行  
  27.             String[] words = value.toString().split("\\s");  
  28.   
  29.             for (String word : words) {  
  30.                 context.write(new Text(word), ONE);  
  31.             }  
  32.         }  
  33.     }  
  34.   
  35.         //Reducer静态内部类  
  36.     public static class MyWordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {  
  37.   
  38.         @Override  
  39.         protected void reduce(Text key, Iterable<IntWritable> value,  
  40.                 Reducer<Text, IntWritable, Text, IntWritable>.Context context)  
  41.                 throws IOException, InterruptedException {  
  42.             int count = 0;  
  43.             for (IntWritable v : value) {  
  44.                 count += v.get(); // 统计单词个数  
  45.             }  
  46.             context.write(new Text(key), new IntWritable(count));  
  47.         }  
  48.     }  
  49.   
  50.     public static void main(String[] args) throws Exception {  
  51.   
  52.         Configuration conf = new Configuration(); // 配置文件对象  
  53.         Job job = Job.getInstance(conf, "mywordCount"); // mapreduce作业对象  
  54.   
  55.         // 设置map操作  
  56.         job.setMapperClass(MyWordCountMapper.class);    //设置map处理类  
  57.         job.setMapOutputKeyClass(Text.class);   //设置拆分后,输出数据key的类型  
  58.         job.setMapOutputValueClass(IntWritable.class);  //设置拆分后,输入数据value的类型  
  59.   
  60.         // 设置reduce操作  
  61.         job.setReducerClass(MyWordCountReducer.class);  //设置reduce处理类   
  62.                         //这里reduce输入输出格式一致,不需要再次设置  
  63.   
  64.         // 设置输入输出  
  65.         FileInputFormat.setInputPaths(job, new Path("hdfs://master:9000/in/data03.txt"));// 设置处理数据文件的位置  
  66.         FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/result"));// 设置处理后文件的存放位置  
  67.   
  68.         // 开始执行mapreduce作业  
  69.         job.waitForCompletion(true);  
  70.     }  
  71. }  

【结果】



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值