WordCount-JAVA版

Posted jomini

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WordCount-JAVA版相关的知识,希望对你有一定的参考价值。

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版的主要内容,如果未能解决你的问题,请参考以下文章

Streaming执行Python版WordCount

Hadoop版Helloworld之wordcount运行示例

WordCount 优化版测试小程序实现

Spark 加强版WordCount ,统计日志中文件访问数量

[0012] Hadoop 版hello word mapreduce wordcount 运行

[0004] Hadoop 版hello word mapreduce wordcount 运行