在当今的大数据时代,Java MapReduce教程成为许多开发者入门大数据处理的首选路径。MapReduce 是 Hadoop 生态系统中的核心计算模型,用于高效地处理海量数据。本文将带你从零开始,手把手教你编写第一个 MapReduce 程序,即使你是编程小白也能轻松上手!
MapReduce 是一种编程模型,由 Google 提出,主要用于大规模数据集的并行处理。它包含两个主要阶段:
在开始编码前,请确保你已安装以下工具:
我们将实现经典的 WordCount 示例——统计文本中每个单词出现的次数。这是学习 MapReduce编程入门 的最佳起点。
使用如下命令创建一个 Maven 项目:
mvn archetype:generate \ -DgroupId=com.example \ -DartifactId=wordcount \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DinteractiveMode=false
在 pom.xml 中添加 Hadoop 客户端依赖:
<dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>3.3.6</version> </dependency></dependencies>
Mapper 负责将一行文本拆分为单词,并输出 <单词, 1> 的键值对:
import java.io.IOException;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); @Override public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] words = line.split("\\s+"); for (String w : words) { if (!w.isEmpty()) { word.set(w.toLowerCase()); context.write(word, one); } } }} Reducer 接收相同 key 的所有 value,并求和:
import java.io.IOException;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Reducer;public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> { @Override 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)); }} 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;public class WordCountDriver { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCountDriver.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.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); }} 将程序打包成 JAR 文件后,可在本地或 Hadoop 集群上运行:
hadoop jar wordcount.jar com.example.WordCountDriver /input /output
其中 /input 是 HDFS 上的输入目录,/output 是输出目录(必须不存在)。
通过本篇 Java大数据处理 教程,你已经掌握了 MapReduce 的基本原理和开发流程。虽然现代大数据生态中 Spark 等框架更流行,但理解 MapReduce 对深入学习 Hadoop MapReduce示例 和分布式计算思想至关重要。建议你尝试修改代码,比如过滤停用词、统计字符数等,以加深理解。
提示:初学者可在本地 IDE 中使用 MiniCluster 模拟 Hadoop 环境进行调试,无需搭建完整集群。
本文由主机测评网于2025-12-23发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://vpshk.cn/20251211872.html