Hadoop 生态与实战应用教程
1. 简介
Hadoop 是一个开源软件框架,主要用于存储和处理大规模数据集。它提供了分布式文件系统(HDFS)和计算框架(MapReduce)。Hadoop 的设计目的是为了在普通硬件上运行,通过分布式处理来实现高可靠性、高容错性和大规模数据处理能力。
定位:
- 解决的问题:大数据的存储和处理。
- 与生态系统的关系:Hadoop 是大数据生态系统的核心之一,与许多其他工具(如Hive、Spark、Pig等)集成使用。
优势与劣势:
- 优势:强大的扩展性、高容错性、易于部署和使用。
- 劣势:MapReduce 编程模型较复杂,不适合实时查询。
2. 核心概念
- HDFS (Hadoop Distributed File System):分布式文件系统,用于存储大量数据。
- MapReduce:一种编程模型,用于处理和生成大数据集。
- YARN (Yet Another Resource Negotiator):资源管理器,负责管理和调度集群资源。
- Hadoop Common:提供 Hadoop 核心库和工具。
- Hadoop MapReduce:基于 MapReduce 模型的编程框架。
- Hadoop YARN:新的资源管理平台,取代了旧的 MapReduce 框架。
3. 环境搭建
安装和配置 Hadoop:
- 下载和解压:
- wget https://downloads.apache.org/hadoop/common/hadoop-3.3.1/hadoop-3.3.1.tar.gz tar -xzvf hadoop-3.3.1.tar.gz
- 配置环境变量:
- export HADOOP_HOME=/path/to/hadoop-3.3.1 export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin
- 配置 Hadoop:
- core-site.xml:
- <configuration> <property> <name>fs.defaultFSname> <value>hdfs://localhost:9000value> property> configuration>
- hdfs-site.xml:
- <configuration> <property> <name>dfs.replicationname> <value>1value> property> configuration>
- mapred-site.xml:
- <configuration> <property> <name>mapreduce.framework.namename> <value>yarnvalue> property> configuration>
- yarn-site.xml:
- <configuration> <property> <name>yarn.nodemanager.aux-servicesname> <value>mapreduce_shufflevalue> property> configuration>
- 格式化 HDFS:
- hdfs namenode -format
- 启动 Hadoop 集群:
- start-dfs.sh start-yarn.sh
4. 基础到进阶
基础:Hello World 示例
- 创建输入文件:
- echo "Hello World" > input.txt
- 上传文件到 HDFS:
- hdfs dfs -put input.txt /input
- 编写 MapReduce 程序:
- 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 { public static class TokenizerMapper extends Mapper
- 编译并打包:
- javac -classpath $(hadoop classpath) -d WordCount_classes WordCount.java jar cf wc.jar -C WordCount_classes/ .
- 运行 MapReduce 作业:
- hadoop jar wc.jar WordCount /input /output
进阶:YARN 应用开发
- 编写 YARN 应用程序:
- import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.yarn.api.ApplicationConstants; import org.apache.hadoop.yarn.api.records.*; import org.apache.hadoop.yarn.client.api.YarnClient; import org.apache.hadoop.yarn.client.api.YarnClientApplication; import org.apache.hadoop.yarn.util.Records; public class YarnApp { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); YarnClient yarnClient = YarnClient.createYarnClient(); yarnClient.init(conf); yarnClient.start(); YarnClientApplication app = yarnClient.createApplication(); ApplicationSubmissionContext appContext = app.getApplicationSubmissionContext(); // Set the application name. appContext.setApplicationName("My YARN App"); // Set the container launch context as part of the application's master. ContainerLaunchContext ctx = Records.newRecord(ContainerLaunchContext.class); // Set up the container launch context ctx.setCommands(Collections.singletonList("${JAVA_HOME}/bin/java " + "org.myorg.MyYarnApp")); appContext.setAMContainerSpec(ctx); // Submit the application. ApplicationId appId = yarnClient.submitApplication(app); // Print the application id System.out.println("Application ID is " + appId); } }
- 编译并打包:
- javac -classpath $(yarn classpath) -d YarnApp_classes YarnApp.java jar cf yarnapp.jar -C YarnApp_classes/ .
- 提交 YARN 应用程序:
- hadoop jar yarnapp.jar YarnApp
5. 实战案例
- 日志分析:使用 MapReduce 分析服务器日志,统计访问量最高的页面。
- 推荐系统:使用 Hadoop 和 Spark 构建推荐系统,处理用户行为数据。
- 金融数据分析:处理股票市场数据,进行风险评估和预测。
6. 最佳实践
- 性能优化:合理设置 HDFS 的块大小、减少网络传输次数。
- 安全建议:启用 Kerberos 认证,限制用户权限。
- 常见错误与调试技巧:查看日志文件,使用 jstack 查看线程堆栈信息。
7. 资源推荐
- 官方文档:https://hadoop.apache.org/docs/current/
- 社区论坛:https://discuss.apache.org/c/hadoop
- 调试工具:Ambari、Ganglia
通过本教程的学习,你应该能够全面掌握 Hadoop 的基础和进阶知识,并能够在实际项目中灵活应用。