MapReduce-多个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/test1", "hdfs://fz/user/hdfs/MapReduce/data/multipleInputs/output"};
			int exitCode = ToolRunner.run(new MultipleInputsTest(), params);
			System.exit(exitCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

  

MapReduce-多个Mapper

上一篇:OkHttp:NoClassDefFoundError


下一篇:dotnet consul 与 ocelot