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单词计数的功能

package com.yc.hadoop42_003_mapreduce;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MyWordCount {

        //Mapper静态内部类
	public static class MyWordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

		public static final IntWritable ONE = new IntWritable(1);

		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			//按空格分割,map默认的value是每一行
			String[] words = value.toString().split("\\s");

			for (String word : words) {
				context.write(new Text(word), ONE);
			}
		}
	}

        //Reducer静态内部类
	public static class MyWordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

		@Override
		protected void reduce(Text key, Iterable<IntWritable> value,
				Reducer<Text, IntWritable, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			int count = 0;
			for (IntWritable v : value) {
				count += v.get(); // 统计单词个数
			}
			context.write(new Text(key), new IntWritable(count));
		}
	}

	public static void main(String[] args) throws Exception {

		Configuration conf = new Configuration(); // 配置文件对象
		Job job = Job.getInstance(conf, "mywordCount"); // mapreduce作业对象

		// 设置map操作
		job.setMapperClass(MyWordCountMapper.class);	//设置map处理类
		job.setMapOutputKeyClass(Text.class);	//设置拆分后,输出数据key的类型
		job.setMapOutputValueClass(IntWritable.class);	//设置拆分后,输入数据value的类型

		// 设置reduce操作
		job.setReducerClass(MyWordCountReducer.class);	//设置reduce处理类 
						//这里reduce输入输出格式一致,不需要再次设置

		// 设置输入输出
		FileInputFormat.setInputPaths(job, new Path("hdfs://master:9000/in/data03.txt"));// 设置处理数据文件的位置
		FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/result"));// 设置处理后文件的存放位置

		// 开始执行mapreduce作业
		job.waitForCompletion(true);
	}
}

【结果】



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值