码迷,mamicode.com
首页 > 编程语言 > 详细

WordCount-JAVA版

时间:2019-09-05 00:49:05      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:count   row   hadoop   mapred   throw   over   oid   reducer   imp   

WordCountMapper

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;

public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
	Text k = new Text();
	IntWritable v = new IntWritable(1);

	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

		String line = value.toString();

		String[] words = line.split(" ");

		for (String word : words) {
			k.set(word);
			context.write(k, v);
		}
	}
}

 WordCountMapper

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;

public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

	Text k = new Text();
	IntWritable v = new IntWritable(1);

	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

		String line = value.toString();

		String[] words = line.split(" ");

		for (String word : words) {
			k.set(word);
			context.write(k, v);
		}

	}
}

  WordCountReducer

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

public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
	
	int sum;
	IntWritable v = new IntWritable();

	@Override
	protected void reduce(Text key, Iterable<IntWritable> value, Context context)
			throws IOException, InterruptedException {
		// 1 累加求和
		sum = 0;
		for (IntWritable count : value) {
			sum += count.get();
		}
		// 2 输出
		v.set(sum);
		context.write(key, v);
	}

}

  

 

 

 

WordCount-JAVA版

标签:count   row   hadoop   mapred   throw   over   oid   reducer   imp   

原文地址:https://www.cnblogs.com/Jomini/p/11462513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!