MapReduce-多个Mapper

MapReduce多输入多mapper处理方法
博客介绍了MapReduce的多输入、多mapper相关内容。一个MapReduce作业输入可能含多个文件,但通常由同一InputFormat和Mapper解释。面对数据格式演变或数据源格式不同问题,可用MultipleInputs类处理,它能为每条输入路径指定InputFormat和Mapper。

MapReduce的多输入、多mapper

虽然一个MapReduce作业的输入可能包含多个输入文件(由文件glob、过滤器和路径组成),但所有文件都由同一个InputFormat和同一个Mapper来解释。然而,数据格式往往会随时间而演变,所以必须写自己的mapper来处理应用中的遗留数据格式问题。或者,有些数据源会提供相同的数据,但是格式不同。
这些问题可以用MultipleInputs类来妥善处理,它允许为每条输入路径指定InputFormat和Mapper。

代码如下

package com.zhen.mapreduce.multipleInput;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * @author FengZhen
 * @date 2018年8月25日
 * 多输入、多mapper
 */
public class MultipleInputsTest extends Configured implements Tool{

	/**
	 * 根据 ` 分隔字符串
	 * @author FengZhen
	 *
	 */
	static class SplitMapper1 extends Mapper<LongWritable, Text, Text, IntWritable>{
		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			String[] values = value.toString().split("`");
			for (String string : values) {
				context.write(new Text(string), new IntWritable(1));
			}
		}
	}
	
	/**
	 * 根据 , 分隔字符串
	 * @author FengZhen
	 *
	 */
	static class SplitMapper2 extends Mapper<LongWritable, Text, Text, IntWritable>{
		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
				throws IOException, InterruptedException {
			String[] values = value.toString().split(",");
			for (String string : values) {
				context.write(new Text(string), new IntWritable(1));
			}
		}
	}

	/**
	 * 同一个reduce
	 * @author FengZhen
	 *
	 */
	static class SplitReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
		@Override
		protected void reduce(Text key, Iterable<IntWritable> value,
				Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
			int sum = 0;
			for (IntWritable intWritable : value) {
				sum += intWritable.get();
			}
			context.write(key, new IntWritable(sum));
		}
	}

	public int run(String[] args) throws Exception {
		
		Configuration configuration = new Configuration();
		
		Job job = Job.getInstance(configuration);
		job.setJobName("MultipleInputs");
		job.setJarByClass(MultipleInputsTest.class);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);

		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		
		job.setReducerClass(SplitReducer.class);
		
		//设置多输入、多mapper
		MultipleInputs.addInputPath(job, new Path(args[0]), TextInputFormat.class, SplitMapper1.class);
		MultipleInputs.addInputPath(job, new Path(args[1]), TextInputFormat.class, SplitMapper2.class);
		
		job.setOutputFormatClass(TextOutputFormat.class);
		TextOutputFormat.setOutputPath(job, new Path(args[2]));
		
		return job.waitForCompletion(true) ? 0 : 1;
	}
	
	public static void main(String[] args) {
		try {
			String[] params = {"hdfs://fz/user/hdfs/MapReduce/data/multipleInputs/test1","hdfs://fz/user/hdfs/MapReduce/data/multipleInputs/test2", "hdfs://fz/user/hdfs/MapReduce/data/multipleInputs/output"};
			int exitCode = ToolRunner.run(new MultipleInputsTest(), params);
			System.exit(exitCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

  

转载于:https://www.cnblogs.com/EnzoDin/p/9534636.html

hadoop-mapreduce-client-core是Hadoop分布式计算框架中的核心模块之一。它主要包含了Hadoop MapReduce的核心功能和API接口,是实现MapReduce编程模型的必备组件。 Hadoop MapReduce是一种用于大规模数据处理的编程模型,其核心思想是将大规模数据集分解成多个较小的数据块,分别在集群中的不同机器上进行处理,最后将结果整合。hadoop-mapreduce-client-core模块提供了与MapReduce相关的类和方法,方便开发者实现自定义的Map和Reduce任务。 具体来说,hadoop-mapreduce-client-core模块包含了以下重要组件和功能: 1. Job:Job表示一个MapReduce任务的定义和描述,包括输入路径、输出路径、Mapper和Reducer等。 2. MapperMapperMapReduce任务中的映射函数,它负责将输入数据转换成<key, value>键值对的形式。 3. Reducer:Reducer是MapReduce任务中的归约函数,它按照相同的key将所有Mapper输出的value进行聚合处理。 4. InputFormat:InputFormat负责将输入数据切分成多个InputSplit,每个InputSplit由一个Mapper负责处理。 5. OutputFormat:OutputFormat负责将Reducer的输出结果写入指定的输出路径中。 使用hadoop-mapreduce-client-core模块,开发者可以基于Hadoop分布式计算框架快速开发并行处理大规模数据的应用程序。通过编写自定义的Mapper和Reducer,可以实现各种类型的分布式计算,如数据清洗、聚合分析、机器学习等。 总之,hadoop-mapreduce-client-core是Hadoop分布式计算框架中的核心模块,提供了实现MapReduce编程模型所需的基本功能和API接口。使用该模块,开发者可以利用Hadoop的分布式计算能力,高效地处理和分析大规模数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值