ChainMapper和ChainReducer

hadoop api提供有ChainMapper和ChainReducer链式处理数据的接口,ChainMapper在一个map节点可以执行多个mapper逻辑,他们想unix管道一样,前一个map的输出作为后一个map的输入,知道最后一个map输出作为partition的输入,然后到reduce节点。而ChainReducer则不能从字面意义上理解为多个reduce连接起来处理数据,ChainReducer是在reducer处理完成后添加map逻辑,实现reduce函数处理完成后的其它数据处理逻辑。

ChainReducer的优点:更好利用reduce节点,方便实现reduce输出后的其它数据处理逻辑。

ChainMapper的优点:api上描述为更好利用io,不过多个map连接起来的逻辑不能在同一个map节点里面实现吗?不很确定。


实现了一个简单示例,使用ChainMapper和ChainReducer


package test.com.cn;

import java.io.IOException;
import java.util.Random;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Cluster;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.JobPriority;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.chain.ChainMapper;
import org.apache.hadoop.mapreduce.lib.chain.ChainReducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class ChainMapperJob {
	
	static class Mapper01 extends Mapper<LongWritable,Text,Text,Text>
	{
		Random randGenerator = new Random();
		Text key = new Text();
		public void map(LongWritable inKey, Text inValue, Context context) throws IOException, InterruptedException 
		{
			int rand = randGenerator.nextInt(10000);
			key.set(String.valueOf(rand));
            context.write(key, inValue);
		}
	}
	
	static class Mapper02 extends Mapper<Text,Text,Text,Text>
	{
		public void map(Text inKey, Text inValue, Context context) throws IOException, InterruptedException 
		{
			int key = Integer.valueOf(inKey.toString());
			if (key <= 2000)
				return;
			else
                context.write(inKey, inValue);
		}
	}
	
	static class Reducer01 extends Reducer<Text,Text,Text,Text>
	{
		public void reduce(Text key, Iterable<Text> values, Context context)throws IOException, InterruptedException
		{
			for(Text value: values)
			    context.write(key, value);
		}
	}
	
	static class Mapper03 extends Mapper<Text,Text,Text,Text>
	{
		public void map(Text inKey, Text inValue, Context context)throws IOException, InterruptedException
		{
			int key = Integer.valueOf(inKey.toString());
			if (key <= 5000)
				return;
			context.write(inKey, inValue);
		}
	}

	/**
	 * @param args
	 * @throws IOException 
	 * @throws InterruptedException 
	 * @throws ClassNotFoundException 
	 */
	@SuppressWarnings("static-access")
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		// TODO Auto-generated method stub
		
		Job job = Job.getInstance(new Cluster(new Configuration()));
		
		@SuppressWarnings("rawtypes")
		ChainMapper chainMapper = new ChainMapper() ;
		
		Configuration map01Conf = new Configuration(false);
		chainMapper.addMapper(job, Mapper01.class, LongWritable.class, Text.class, Text.class, Text.class, map01Conf);
		Configuration map02Conf = new Configuration(false);
		chainMapper.addMapper(job, Mapper02.class, Text.class, Text.class, Text.class, Text.class, map02Conf);
		
		job.setJarByClass(ChainMapperJob.class);
		job.setReducerClass(Reducer01.class);
		job.setNumReduceTasks(1);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		job.setPriority(JobPriority.HIGH);
		
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		System.exit(job.waitForCompletion(true)? 0: 1);
		
		/*
		Job job = Job.getInstance(new Cluster(new Configuration()));
		
		@SuppressWarnings("rawtypes")
		ChainMapper chainMapper = new ChainMapper() ;
		@SuppressWarnings("rawtypes")
		ChainReducer chainReducer = new ChainReducer();
		
		Configuration map01Conf = new Configuration(false);
		chainMapper.addMapper(job, Mapper01.class, LongWritable.class, Text.class, Text.class, Text.class, map01Conf);
		Configuration map02Conf = new Configuration(false);
		chainMapper.addMapper(job, Mapper02.class, Text.class, Text.class, Text.class, Text.class, map02Conf);
		
		Configuration reduce01Conf = new Configuration(false);
		chainReducer.setReducer(job, Reducer01.class, Text.class, Text.class, Text.class, Text.class, reduce01Conf);
		Configuration map03Conf = new Configuration(false);
		chainReducer.addMapper(job, Mapper03.class, Text.class, Text.class, Text.class, Text.class, map03Conf);
		
		job.setJarByClass(ChainMapperJob.class);
		job.setNumReduceTasks(1);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		job.setPriority(JobPriority.HIGH);
		
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		System.exit(job.waitForCompletion(true)? 0: 1);
		 */
		
		
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值