(十)Mapper多输入源及Reduce多输出文件

目的

Mapper多个输入源,处理后变成一个输出。

reduce多输出源,根据输出的数据,按照自己的要求来决定,输出到不同的文件里。

案例

有以下两个文件,作为输入源(Mapper处理)

最后把每个人的成绩打印到不同的文件里(Reduce处理)

tom
math 90
english 98
jary
math 78
english 87
rose
math 87
english 90
tom math 67 english 87
jary math 59 english 80
rose math 79 english 60

 代码实现

  • 写自定义输入输出组件
  • 写两个mapper,分别处理不同的文件
  • Reduce代码实现
package hadoop04;

import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;

public class ReduceDemo extends Reducer<Text, Text, Text, Text>{
	
	private MultipleOutputs<Text, Text> mos;
	
	@Override
	protected void setup(Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
		mos = new MultipleOutputs<>(context);
	}
	
	@Override
	protected void reduce(Text key, Iterable<Text> value, Reducer<Text, Text, Text, Text>.Context context)
			throws IOException, InterruptedException {
		
		for(Text txt : value) {
			if(key.toString().equals("jary")){
				mos.write("jary", key, txt);
			}
			if(key.toString().equals("rose")){
				mos.write("rose", key, txt);
			}
			if(key.toString().equals("tom")){
				mos.write("tom", key, txt);
			}
		}
	}

}
  • Driver类实现
package hadoop04;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;

import hadoop03.AuthOutputFormat;


public class DriverDemo {
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf, "JobName");
		
		job.setJarByClass(DriverDemo.class);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		
		//如果一个Mapper代码不能通用的解决,则需要分别指定。此时,就不能去设置 setMapperClass()了
		MultipleInputs.addInputPath(job, new Path("hdfs://192.168.80.100:9000/input/test1.txt"), 
				AuthInputFomat.class, MapperDemo01.class);
		
		MultipleInputs.addInputPath(job, new Path("hdfs://192.168.80.100:9000/input/test2.txt"), 
				TextInputFormat.class, MapperDemo02.class);
		
		MultipleOutputs.addNamedOutput(job, "jary", AuthOutputFormat.class, Text.class, Text.class);
		MultipleOutputs.addNamedOutput(job, "tom", AuthOutputFormat.class, Text.class, Text.class);
		MultipleOutputs.addNamedOutput(job, "rose", AuthOutputFormat.class, Text.class, Text.class);
		
		FileOutputFormat.setOutputPath(job, new Path("hdfs://192.168.80.100:9000/result"));
		
		job.waitForCompletion(true);
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值