开发者学堂课程【Hadoop 分布式计算框架 MapReduce:WordCount 案例 Reducer】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/94/detail/1483
WordCount 案例 Reducer
编写 Reducer 类
package com.atguigu.mapreduce.wordcount ;
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 extenas
Intwritable, Text, Intwritable>{
int aum;
IntWritable v = new Intwritable ( );
@override
protected void reduce (Text key,Iterablevalues ,Contextcontext)
Throws IOException,
InterruptedException {
/ /1累加求和,
sum = 0;
for (Intwritable count : values) {
sum +=count.get ();
}
//2输出
@Suppresswarnings( "unchecked") protected void reduce(KEYIN key,Iterable<VALUEIN> values,Context contekt ) throws IOException,InterruptedException { for(VALUEIN value: values) { context.write((KEYOUT) key,(VALUEOUT) value); } |
protected void cleanup(Context context ) throws IOException,InterruptedException { // NOTHING } |
reduce(context.getCurrentKey(),context.getValues(),context);// If a back up store is used,reset it Iterator<VALUEIN> iter = context.getValues().iterator();if(iter instanceof ReduceContext.ValueIterator) { ((ReduceContext.ValueIterator<VALUEIN>)iter).resetBackupStore();} } }finally { cleanup(context); } |