MapReduce-Reduce端join操作-Reducer阶段代码

定义 Reducer

package cn.learn.mapreduce_reduce_join;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;


public class ReduceJoinReducer extends Reducer<Text,Text,Text,Text> {
    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws 
IOException, InterruptedException {

        String first = "";
        String second = "";
        for (Text value : values) {
            if(value.toString().startsWith("p")){
                first = value.toString();
            }else{
                second  = value.toString();
            }
        }

        if(first.equals("")){
            context.write(key, new Text("NULL"+"\t"+second));
        }else{
            context.write(key, new Text(first+"\t"+second));
        }

    }
}
发布了2226 篇原创文章 · 获赞 51 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/Leon_Jinhai_Sun/article/details/104571618