您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Hadoop2.x WordCount MapReduce 使用指南
## 一、MapReduce 基础概念
### 1.1 什么是MapReduce
MapReduce是Hadoop的核心计算框架,用于大规模数据集的并行处理。它将计算过程分为两个主要阶段:
1. **Map阶段**:对输入数据进行分割和处理,生成键值对(key-value pairs)形式的中间结果
2. **Reduce阶段**:对Map输出的中间结果进行汇总和聚合
### 1.2 WordCount示例的意义
WordCount是MapReduce的"Hello World"程序,它完成以下功能:
- 统计文本文件中每个单词出现的次数
- 展示MapReduce的基本编程模型
- 验证Hadoop集群是否正常工作
## 二、环境准备
### 2.1 硬件要求
- 至少3节点Hadoop集群(1个NameNode,2个DataNode)
- 每个节点建议配置:
- 4核CPU
- 8GB内存
- 50GB硬盘空间
### 2.2 软件要求
| 组件 | 版本要求 |
|-------------|-----------|
| Hadoop | 2.x系列 |
| Java | JDK 1.8+ |
| 操作系统 | Linux推荐 |
### 2.3 集群配置检查
```bash
# 检查Hadoop版本
hadoop version
# 检查HDFS状态
hdfs dfsadmin -report
# 检查YARN状态
yarn node -list
import java.io.IOException;
import java.util.StringTokenizer;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCount {
// Mapper类
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
// Reducer类
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
// 主方法
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.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);
}
}
Mapper<Object, Text, Text, IntWritable>
基类map()
方法接收(key, value)对:
StringTokenizer
分割单词<word, 1>
键值对Reducer<Text, IntWritable, Text, IntWritable>
基类reduce()
方法接收:
<word, count>
键值对# 设置Hadoop classpath
export HADOOP_CLASSPATH=${JAVA_HOME}/lib/tools.jar
# 编译程序
hadoop com.sun.tools.javac.Main WordCount.java
# 生成jar包
jar cf wc.jar WordCount*.class
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
# 创建输入目录
hdfs dfs -mkdir -p /user/hadoop/wordcount/input
# 上传测试文件
hdfs dfs -put localfile.txt /user/hadoop/wordcount/input
# 查看上传的文件
hdfs dfs -ls /user/hadoop/wordcount/input
hadoop jar wc.jar WordCount \
/user/hadoop/wordcount/input \
/user/hadoop/wordcount/output
参数 | 描述 |
---|---|
wc.jar | 打包后的程序jar文件 |
WordCount | 主类名 |
第一个路径参数 | HDFS输入目录 |
第二个路径参数 | HDFS输出目录(必须不存在) |
# 查看运行中的作业
yarn application -list
# 查看特定作业详情
yarn application -status <Application_ID>
# 查看作业日志
yarn logs -applicationId <Application_ID>
hdfs dfs -cat /user/hadoop/wordcount/output/part-r-00000 | head -20
Apple 15
Banana 8
Cat 23
...
Output directory hdfs://... already exists
解决方案:删除或指定新的输出目录
hdfs dfs -rm -r /user/hadoop/wordcount/output
java.lang.ClassNotFoundException: WordCount
解决方案:确保正确打包包含所有类
jar cvf wc.jar WordCount*.class
Permission denied: user=..., access=WRITE, inode="..."
解决方案:检查HDFS权限或使用超级用户
hdfs dfs -chmod -R 777 /user/hadoop
实现InputFormat
接口处理特殊格式数据
FileInputFormat.addInputPath(job, new Path(input1));
FileInputFormat.addInputPath(job, new Path(input2));
// 添加缓存文件
job.addCacheFile(new URI("hdfs://path/to/file#linkname"));
// Mapper中获取
Path file = new Path("linkname");
数据量 | 文件大小 | 行数 |
---|---|---|
小 | 10MB | ~100K |
中 | 1GB | ~10M |
大 | 10GB | ~100M |
配置项 | 性能影响 |
---|---|
Map任务数 | 高 |
Reduce任务数 | 中 |
JVM内存 | 高 |
数据本地性 | 极高 |
WordCount示例展示了MapReduce编程的核心思想: 1. 分而治之:Map阶段分布式处理 2. 聚合计算:Reduce阶段汇总结果 3. 简单接口:只需实现map和reduce方法
通过本指南,您应该能够: - 理解MapReduce基本原理 - 编写完整的WordCount程序 - 在Hadoop集群上运行作业 - 分析处理结果和性能指标
”`
(注:实际字数约为2800字,完整执行代码和配置示例已包含在内。如需进一步扩展,可以增加性能调优章节或更复杂案例的详细分析。)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。