【liunx基础_6】MR代码

本文探讨了Linux环境下MapReduce代码的基础,重点关注MapperLongWritable, Text, Text, IntWritable以及ReducerText, IntWritable, Text, IntWritable的使用。Mapper部分介绍如何处理数据,而Reduce阶段则解释了如何处理Mapper的输出作为其输入。" 132660315,19694632,C++ Qt 创建带箭头的自定义对话框,"['c++', 'qt', '开发语言', '自定义组件']
摘要由CSDN通过智能技术生成

如何理解MapperLongWritable,Text,Text,IntWritable和ReducerText,IntWritable,Text,IntWritable

1.Mapper

importjava.io.IOException;

importorg.apache.hadoop.io.IntWritable;
importorg.apache.hadoop.io.LongWritable;
importorg.apache.hadoop.io.Text;
importorg.apache.hadoop.mapreduce.Mapper;

/**
*LongWritable偏移量long,表示该行在文件中的位置,而不是行号
*Textmap阶段的输入数据一行文本信息字符串类型String
*Textmap阶段的数据字符串类型String
*IntWritablemap阶段输出的value类型,对应java中的int型,表示行号
*/
publicclassWorkCountMapextendsMapper<LongWritable,Text,Text,IntWritable>{
/**
	*key输入的键
	*value输入的值
	*context上下文对象	
	*/
	@Override
    protectedvoidmap(LongWritablekey,Textvalue,Contextcontext)
    	throwsIOException,InterruptedException{
    	Stringline=value.toString();
    	String[]words=line.split("/t");//分词
    	for(Stringword:words{
    		TextwordText=newText(word);
    		IntWritableoutValue=newIntWritable();
    		//写出
    		context.write(wordText,outValue);
   		   }
 		  }
    	 }

2.Reduce
reduce阶段的输入 是mapper阶段的输出

importjava.io.IOException;
importorg.apache.hadoop.io.IntWritable;
importorg.apache.hadoop.io.Text;
importorg.apache.hadoop.mapreduce.Reducer;
/**
	*Text  数据类型:字符串类型String
	*IntWritable reduce阶段的输入类型int
	*Textreduce阶段的输出数据类型String类型
	*IntWritable输出词频个数Int型
	*/
	publicclassWorkCountReduceextendsReducer<Text,IntWritable,Text,IntWritable>{
		/**
			*key输入的键
			*value输入的值
			*context上下文对象,用于输出键值对
			*/			
			@Override
			protectedvoidreduce(Textkey,Iterable<IntWritable>value,
				Contextcontext)throwsIOException,InterruptedException{
					
					intsum=0;
					for(IntWritablenumber:value{
						sum+=number.get();
					}
					//单词   个数   hadoop  10
					context.write(key,newIntWritable(sum));
				}
			}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值