Mapper类、Reducer类等信息
发布时间:2025-06-24 18:56:58 作者:北方职教升学中心 阅读量:374
通过setOutputKeyClass和setOutputValueClass方法来指定输出键和值的类型。对于数组中的每个元素,我们将其转换为一个整数,并将其赋值给number变量。setCombinerClass和setReducerClass方法来指定Mapper、
3.3 Map过程的实现
public class MaxValueMapper extends Mapper<LongWritable, Text, IntWritable, IntWritable> {
private final IntWritable one = new IntWritable(1);
private IntWritable number = new IntWritable();
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] numbers = value.toString().split(“,”);
for (String s : numbers) {
number.set(Integer.parseInt(s));
context.write(one, number);
}
}
}
在Mapper类中,我们首先声明两个局部变量:一个IntWritable类型的one变量,用于作为键;一个IntWritable类型的number变量,用于作为值。Mapper类、Reducer类等信息。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、在作业配置之后,我们通过调用waitForCompletion方法来等待作业运行完毕。如果作业运行成功,输出"Job completed successfully."。实战项目、下面是一个示例代码:
Configuration conf = new Configuration();
conf.set(“fs.defaultFS”, “hdfs://localhost:9000”);
FileSystem fs = FileSystem.get(conf);
Path inputPath = new Path(“/input”);
Path outputPath = new Path(“/output”);
if (fs.exists(outputPath)) {
fs.delete(outputPath, true);
}
3.2 作业配置实现
在配置MapReduce作业时,我们需要指定作业的输入、
Job job = Job.getInstance(conf, “max value”);
job.setJarByClass(MaxValue.class);
job.setMapperClass(MaxValueMapper.class);
job.setCombinerClass(MaxValueReducer.class);
job.setReducerClass(MaxValueReducer.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, inputPath);
FileOutputFormat.setOutputPath(job, outputPath);
在配置作业时,我们需要通过Job类的getInstance方法来获取一个作业实例,并指定作业的名称和运行配置。在map函数中,我们首先将输入的一行文本转换为一个字符串数组,然后遍历该数组。
public class MaxValue {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set(“fs.defaultFS”, “hdfs://localhost:9000”);
FileSystem fs = FileSystem.get(conf);
Path inputPath = new Path(“/input”);
Path outputPath = new Path(“/output”);
if (fs.exists(outputPath)) {
fs.delete(outputPath, true);
}
Job job = Job.getInstance(conf, “max value”);
job.setJarByClass(MaxValue.class);
job.setMapperClass(MaxValueMapper.class);
job.setCombinerClass(MaxValueReducer.class);
job.setReducerClass(MaxValueReducer.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, inputPath);
FileOutputFormat.setOutputPath(job, outputPath);
boolean success = job.waitForCompletion(true);
if (success) {
System.out.println(“Job completed successfully.”);
}
}
}
在main函数中,我们首先创建一个Configuration对象,并设置默认文件系统为本地HDFS。通过setMapperClass、源码讲义、最后,我们输出作业运行结果。如果值大于当前最大值,则将该值赋值给最大值变量。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)
学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、实战项目、学习笔记、这样,Map函数就将每个输入数值作为值输出,而将固定的键1与每个数值组合。在本问题中,我们可以使用与Map阶段类似的方式对输出结果进行排序,即使用一个比较器类来指定按照键的升序排序。
public static class MaxReducer extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(IntWritable key, Iterable values, Context context)
throws IOException, InterruptedException {
int max = Integer.MIN_VALUE;
for (IntWritable val : values) {
max = Math.max(max, val.get());
}
result.set(max);
context.write(key, result);
}
}
完整的代码如下:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class MaxValue {
public static class MaxValueMapper extends Mapper<LongWritable, Text, LongWritable, LongWritable> {
private LongWritable lineNumber = new LongWritable();
private LongWritable maxNumber = new LongWritable();
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] numbers = line.split(“,”);
long max = Long.MIN_VALUE;
for (String number : numbers) {
long currentNumber = Long.parseLong(number.trim());
if (currentNumber > max) {
max = currentNumber;
}
}
lineNumber.set(key.get());
maxNumber.set(max);
context.write(lineNumber, maxNumber);
}
}
public static class MaxValueReducer extends Reducer<LongWritable, LongWritable, LongWritable, LongWritable> {
private LongWritable maxLineNumber = new LongWritable();
private LongWritable maxValue = new LongWritable(Long.MIN_VALUE);
@Override
public void reduce(LongWritable key, Iterable values, Context context)
throws IOException, InterruptedException {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、学习笔记、在reduce函数中,对于每个键值对,我们将值转换为一个整数,并与当前最大值进行比较。因此,在编写代码之前,我们需要确保Hadoop和HDFS已经安装并运行在本地环境中。最后,通过FileInputFormat和FileOutputFormat类的addInputPath和setOutputPath方法来指定作业的输入和输出路径。OPPO等大厂,18年进入阿里一直到现在。下面是一个示例代码:
job.setSortComparatorClass(MaxValueComparator.class);
- 基于Eclipse的MapReduce项目求解最大值
3.1 Eclipse访问HDFS
在基于Eclipse的MapReduce项目中,我们需要使用HDFS作为输入和输出的数据源。源码讲义、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)
[外链图片转存中…(img-yBVRANto-1712546093338)]