目录
概述
MapReduce是Hadoop系统核心组件之一,它是一种可用于大数据并行处理的计算模型、框架和平台,主要解决海量数据的计算,是目前分布式计算模型中应用较为广泛的一种。
MapReduce核心思想
MapReduce的核心思想是“分而治之”。所谓“分而治之”就是把一个复杂的问题,按照一定的“分解”方法分为等价的规模较小的若干部分,然后逐个解决,分别找出各部分的结果,把各部分的结果组成整个问题的结果,这种思想来源于日常生活与工作时的经验,同样也完全适合技术领域。
本地模式
新建一个maven项目
修改maven仓库为本地镜像
添加maven依赖
复制源码
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.4</version>
</dependency>
</dependencies>
新建三个包
写mapper组件
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class WordCountMapper 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 line = value.toString();
// 把一行文本分割成一个个的单词
String[] words = line.split(" ");
// 把每个单词组成<单词,1>二元组k-v格式输出,作为reducer的输入
for (String word : words) {
context.write(new Text(word), new IntWritable(1));
}
}
}
Reducer组件
在写reduce组件时protect void reducer应该没有r
package cn.edu.hgu.mapreduce.reducer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.io.Text;
import java.io.IOException;
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
protected void reduce(Text key, Iterable<IntWritable> value, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
// 定义一个计数器
int count = 0;
// 对迭代器做累加的操作
for (IntWritable i : value) {
// get()获取IntWritable整型值
count = count + i.get();
}
// 输出
context.write(key, new IntWritable(count));
}
}
Driver组件
package cn.edu.hgu.mapreduce.driver;
import cn.edu.hgu.mapreduce.mapper.WordCountMapper;
import cn.edu.hgu.mapreduce.reducer.WordCountReducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class WordCountDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
// 设置mapreduce的运行模式,为本地模式
conf.set("mapreduce.framework.name","local");
// 创建mapreduce的job
Job job = Job.getInstance(conf);
// 开始装配各个组件
// 指定job的运行主类
job.setJarByClass(WordCountDriver.class);
// 指定job的mapper组件
job.setMapperClass(WordCountMapper.class);
// 指定job的reduce组件
job.setReducerClass(WordCountReducer.class);
// 设置Mapper类的key和value的数据类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
// 设置reduce类的输出阶段的key和value的数据类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 指定本地模式的数据源
FileInputFormat.setInputPaths(job, "E:/wordcount/input");
// 指定本地模式的输出目的的
FileOutputFormat.setOutputPath(job,new Path("E:/wordcount/output"));
// 提交程序运行
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}
运行结果
集群模式
要把项目打成jar包,然后提交到hadoop集群上运行