MapReduce在Driver中没有指定Reducer类

当在MapReduce的Driver中未指定Reducer类时,系统会使用默认的Reducer并设置数量为1。即使没有明确配置,程序仍能运行,Reducer将Map阶段的输出直接传递。若不希望使用Reducer,可通过设置ReduceTask数量为0来实现,此时输出不再经过Reduce阶段,而是直接由MapTask输出,每个MapTask的输出对应一个文件,不涉及排序和shuffle过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果在Driver中没有指定Reducer,MapReduce会默认将Reducer的数量设置为1,并且使用默认的 IdentityReducer 类作为Reducer类。

因此,即使您没有在Driver中指定Reducer,MapReduce程序仍然可以正常运行,只不过使用的是默认的Reducer类和数量。

image-20230320121358360

上面函数为Hadoop中实现的默认Reducer类,可以看到实现逻辑很简单,就是将Map阶段的输出原封不动再输出出去

下面假设我们有两个文件如下:

image-20230320121544863

然后定义的Driver类如下:

// 1.获取配置信息
Configuration conf = new 
Mapper Reducer Driver Hadoop MapReduce 中的三个重要组件。下面是它们的基本代码实现: Mapper : ```java public class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] words = line.split(" "); for (String w : words) { word.set(w); context.write(word, one); } } } ``` Reducer : ```java public class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } context.write(key, new IntWritable(sum)); } } ``` Driver : ```java public class MyDriver { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(MyDriver.class); job.setMapperClass(MyMapper.class); job.setCombinerClass(MyReducer.class); job.setReducerClass(MyReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 以上代码实现了一个简单的 WordCount 示例,其中 MyMapper 和 MyReducer 分别实现了 Mapper 和 Reducer 接口,并重写了 map 和 reduce 方法。MyDriver 则是程序的入口,它配置了 Job 的各种参数,并提交运行任务。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海洋 之心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值