MapReduce-多个Mapper
MapReduce的多输入、多mapper
虽然一个MapReduce作业的输入可能包含多个输入文件(由文件glob、过滤器和路径组成),但所有文件都由同一个InputFormat和同一个Mapper来解释。然而,数据格式往往会随时间而演变,所以必须写自己的mapper来处理应用中的遗留数据格式问题。或者,有些数据源会提供相同的数据,但是格式不同。
这些问题可以用MultipleInputs类来妥善处理,它允许为每条输入路径指定InputFormat和Mapper。
代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | 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(); } } } |
分类:
MapReduce
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示