如何理解Mapper LongWritable, Text, Text, IntWritable 和Reducer Text, IntWritable, Text, IntWritable

案例:WorkCount
<key,value>
key:文件中的行的偏移量
value:行中的数据

  • Mapper
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;

/**
 * LongWritable 偏移量 long,表示该行在文件中的位置,而不是行号
 * Text map阶段的输入数据 一行文本信息 字符串类型 String
 * Text map阶段的数据字符串类型 String
 * IntWritable map阶段输出的value类型,对应java中的int型,表示行号
 */
public class WorkCountMap extends Mapper<LongWritable, Text, Text, IntWritable>{
	/**
	 * key 输入的 键
	 * value 输入的 值
	 * context 上下文对象
	 */
	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
		
		String line = value.toString();
		String[] words = line.split("/t");//分词
		for(String word : words) {
			Text wordText = new Text(word);
			IntWritable outValue = new IntWritable();
			//写出
			context.write(wordText, outValue);
		}
	}
}
  • Reduce
    reduce阶段的输入 是 mapper阶段的输出
import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
/**
 * Text  数据类型:字符串类型 String
 * IntWritable reduce阶段的输入类型 int 
 * Text reduce阶段的输出数据类型 String类型
 * IntWritable 输出词频个数 Int型
 */
public class WorkCountReduce extends Reducer<Text, IntWritable, Text, IntWritable>{
	/**
	 * key 输入的 键
	 * value 输入的 值
	 * context 上下文对象,用于输出键值对
	 */
	@Override
	protected void reduce(Text key, Iterable<IntWritable> value,
			Context context) throws IOException, InterruptedException {

		int sum=0;
		for (IntWritable number : value) {
			sum += number.get();
		}
		//单词  个数  hadoop,10
		context.write(key, new IntWritable(sum));
	}	
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值