MapReduce WordCount

本文介绍了一个基于Hadoop MapReduce框架实现的WordCount程序,该程序能够从大量文本中统计每个单词出现的次数。文章详细展示了WordCount的Mapper和Reducer类的实现过程,并提供了完整的代码示例。
package com.hadoop.mr;

import java.io.IOException;
import java.util.StringTokenizer;

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 WordCountMapReduce {
    //Mapper Class
	public static class WordCountMapper extends	 Mapper{
// hello world ==> input <0,'hello world'>  ===>  
		private Text outputkey = new Text();
		public static final IntWritable outputvalue = new IntWritable(1);f
		@Override
		protected void map(LongWritable key, Text value,Context context)
				throws IOException, InterruptedException {
            // key 0  value hello world
			String lineValue = value.toString();
			StringTokenizer st = new StringTokenizer(lineValue); // hello world hdfs 
			while(st.hasMoreTokens()){
				String str = st.nextToken();
				outputkey.set(str);
				context.write(outputkey, outputvalue);
			}
		}
		
	}
	
	//Reducer Class key (hello,list(1,5,6))
	public static class WordCountReducer extends Reducer{

		private IntWritable outputvalue = new IntWritable();
		@Override
		protected void reduce(Text key, Iterable values,Context context)
				throws IOException, InterruptedException {
			int sum = 0;
			for(IntWritable value :values){
//				int a = value.get();
//				sum = sum+a;
				sum+=value.get();
			}
			outputvalue.set(sum);
			context.write(key, outputvalue);
		}
		
	}
	public static void main(String[] args) throws Exception{
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf, WordCountMapReduce.class.getSimpleName());
		job.setJarByClass(WordCountMapReduce.class);
		Path inpath = new Path(args[0]);
		FileInputFormat.addInputPath(job,inpath);
		Path outpath = new Path(args[1]);
		FileOutputFormat.setOutputPath(job, outpath);
		job.setMapperClass(WordCountMapper.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		//3.5 set reducer
		job.setReducerClass(WordCountReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		//###选择true 会将mr运行的日志信息打印出来
		boolean flag = job.waitForCompletion(true);
		System.exit(flag?0:1);
	}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值