第一个Hadoop程序

编写和运行第一个 Hadoop 程序是学习 Hadoop 的重要步骤。以下是一个经典的"WordCount"程序示例,它统计文本文件中每个单词出现的次数。我们将使用 Java 编写 MapReduce 程序,并在 Hadoop 集群上运行它。

一、WordCount 程序概述

WordCount 是 Hadoop 的"Hello World"程序。它的基本逻辑如下:

  1. Mapper:读取输入文件,将每一行文本拆分为单词,并输出每个单词及其出现次数(初始为1)。
  2. Reducer:对每个单词的计数进行汇总,输出最终的单词频率。

二、编写 WordCount 程序

1. 创建 Java 项目

在本地开发环境中(如 IntelliJ IDEA 或 Eclipse)创建一个 Java 项目,并添加 Hadoop 的依赖库。你可以从 Hadoop 安装目录的 share/hadoop/commonshare/hadoop/mapreduce 中找到 JAR 文件。

2. 编写代码

以下是 WordCount 程序的完整代码:

java 复制代码
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;

import java.io.IOException;

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 {
            // 将输入的文本行拆分为单词
            String[] words = value.toString().split("\\s+");
            for (String w : words) {
                word.set(w);
                context.write(word, one); // 输出单词及其计数(初始为1)
            }
        }
    }

    // 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(); // 配置 Hadoop 环境
        Job job = Job.getInstance(conf, "word count"); // 创建一个作业
        job.setJarByClass(WordCount.class); // 设置作业的主类
        job.setMapperClass(TokenizerMapper.class); // 设置 Mapper 类
        job.setCombinerClass(IntSumReducer.class); // 设置 Combiner(可选)
        job.setReducerClass(IntSumReducer.class); // 设置 Reducer 类

        // 设置输出的键值对类型
        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); // 等待作业完成
    }
}
3. 编译和打包

将代码编译并打包为 JAR 文件。假设你的项目名为 WordCount,可以使用以下命令:

bash 复制代码
javac -classpath /path/to/hadoop-common.jar:/path/to/hadoop-mapreduce-client-core.jar -d ./out WordCount.java
jar -cvf WordCount.jar -C ./out/ .

三、运行 WordCount 程序

1. 上传输入文件

将一个文本文件上传到 HDFS,作为输入数据。例如:

bash 复制代码
hdfs dfs -mkdir /input
hdfs dfs -put /path/to/your/input.txt /input
2. 运行程序

使用 Hadoop 命令运行 WordCount 程序:

bash 复制代码
hadoop jar WordCount.jar WordCount /input /output
  • WordCount.jar 是打包后的 JAR 文件。
  • WordCount 是主类的名称。
  • /input 是输入目录。
  • /output 是输出目录(运行前确保该目录不存在)。
3. 查看结果

运行完成后,查看输出结果:

bash 复制代码
hdfs dfs -cat /output/part-r-00000

四、程序解释

  1. Mapper

    • 读取输入的文本行,将其拆分为单词。
    • 对每个单词输出键值对(单词,1)。
  2. Reducer

    • 对每个单词的计数进行汇总。
    • 输出最终的单词及其出现次数。
  3. Combiner(可选):

    • 在 Mapper 节点上对局部结果进行汇总,减少传输到 Reducer 的数据量。

五、常见问题

  1. 输入文件格式

    • 输入文件应为纯文本文件,每行包含一段文本。
  2. 输出目录

    • 输出目录不能预先存在,否则 Hadoop 会报错。
  3. 依赖冲突

    • 确保编译时的 Hadoop JAR 文件版本与集群中的 Hadoop 版本一致。

六、扩展

你可以尝试以下扩展:

  1. 优化性能:使用 Combiner 和 Partitioner。
  2. 处理更多文件:将多个文件放入输入目录。
  3. 自定义数据类型 :使用 Hadoop 提供的其他数据类型(如 LongWritable)。

通过运行这个简单的 WordCount 程序,你可以熟悉 Hadoop 的 MapReduce 编程模型和运行流程。

相关推荐
24k小善2 小时前
flink集成tidb cdc
大数据·flink·tidb
TiDB_PingCAP2 小时前
海量数据融合互通丨TiDB 在安徽省住房公积金监管服务平台的应用实践
分布式·tidb·htap
StableAndCalm3 小时前
什么是hive
数据仓库·hive·hadoop
程序员的世界你不懂3 小时前
Kafka 推送消息,移动端自动化测试,数据驱动测试
分布式·kafka·linq
kngines4 小时前
【实战ES】实战 Elasticsearch:快速上手与深度实践-3.2.3 案例:新闻搜索引擎的相关性优化
大数据·elasticsearch·搜索引擎
秦南北5 小时前
国内领先的宠物类电商代运营公司品融电商
大数据·人工智能·电商
problc7 小时前
Manus AI 全球首款通用型 Agent,中国制造
大数据·人工智能·制造
Demons_kirit7 小时前
Dubbo+Zookeeper
分布式·zookeeper·dubbo
码农liuxin9 小时前
Dubbo 与 Eureka 深度对比:服务通信与发现的核心差异与选型指南
分布式·后端·dubbo
*星星之火*10 小时前
【Flink银行反欺诈系统设计方案】3.欺诈的7种场景和架构方案、核心表设计
大数据·架构·flink