MapReduce计算框架

一,MapReduce的流程架构图:


二,MapReduce简单的word count的流程


三,MapReduce简单的word count的代码

1,主类

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

public class WCJob {
	
	public static void main(String[] args) throws Exception{
     Configuration conf = new Configuration();

     Job job = Job.getInstance(conf);
     job.setJarByClass(WCJob.class);
     
     job.setMapperClass(WCMapper.class);
     job.setMapOutputKeyClass(Text.class);
     job.setMapOutputValueClass(IntWritable.class);
     
     job.setReducerClass(WCReducer.class);
     job.setCombinerClass(WCReducer.class);
     FileInputFormat.addInputPath(job, new Path("/user/lyl/wc"));
     Path outpath = new Path("/user/lyl/wcout");
     
     FileSystem fs = FileSystem.get(conf);
     if(fs.exists(outpath)){
    	 fs.delete(outpath,true);
     }
     
     FileOutputFormat.setOutputPath(job, outpath);
     boolean flag = job.waitForCompletion(true);
     
     if(flag){
    	 System.out.println("job success!");
     }
  }
}

2,map类

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.util.StringUtils;

public class WCMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
	
	@Override
	protected void map(LongWritable key, Text value,
			Context context)
			throws IOException, InterruptedException {
		String str = value.toString();
		String[] strs = StringUtils.split(str ,' ');
		for (String s:strs) {
           context.write(new Text(s), new IntWritable(1));			
		}
	}
}

3,reduce类

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WCReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
	
	@Override
	protected void reduce(Text text, Iterable<IntWritable> iterable,
			Context context)
			throws IOException, InterruptedException {

		int sum = 0;
		for (IntWritable i :iterable) {
			sum +=i.get();
		}
		context.write(text, new IntWritable(sum));
	}
}
四,MapReduce的架构

一主多从架构
主 JobTracker: 负责调度分配每一个子任务task运行于TaskTracker上,如果发现有失败的task就重新分配其任务到其他节点。每一个hadoop集群中只一个 JobTracker,一般它运行在Master节点上。
从TaskTracker:TaskTracker主动与JobTracker通信,接收作业,并负责直接执行每一个任务,为了减少网络带宽TaskTracker最好运行在HDFS的DataNode上

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值