介绍
hadoop 开发环境,以及阅读源码
环境
hadoop building 所需环境, 参考 hadoop-3.4.0-src\BUILDING.txt 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Requirements: * Unix System 类 unix 系统 * JDK 1.8 java8版本,hadoop 由 java 编写 * Maven 3.3 or later 提供 hadoop 编译依赖包 * Boost 1.72 (if compiling native code) 提供 C++ 库,增强 C++ 的功能 * Protocol Buffers 3.7.1 (if compiling native code) 用于序列化结构化数据 * CMake 3.19 or newer (if compiling native code) 跨平台的构建系统,用于管理编译过程 * Zlib devel (if compiling native code) 用于数据压缩的库,处理压缩数据 * Cyrus SASL devel (if compiling native code) 用于认证和数据安全 * One of the compilers that support thread_local storage: GCC 9.3.0 or later, Visual Studio, Clang (community version), Clang (version for iOS 9 and later) (if compiling native code) 支持 thread_local 存储的编译器,如 GCC 9.3.0 或更高版本,使用线程局部存储来管理多线程环境中的数据 * openssl devel (if compiling native hadoop-pipes and to get the best HDFS encryption performance) OpenSSL 开发库,用于加密和解密 * Linux FUSE (Filesystem in Userspace) version 2.6 or above (if compiling fuse_dfs) FUSE 允许用户在用户空间创建文件系统。Hadoop 的 fuse_dfs 模块需要它来实现 HDFS 与 FUSE 的集成 * Doxygen ( if compiling libhdfspp and generating the documents ) 文档生成器,用于从源代码生成文档,libhdfspp 模块的文档生成需要 * Internet connection for first build (to fetch all Maven and Hadoop dependencies) 初次构建需要互联网连接以获取所有的 Maven 和 Hadoop 依赖项 * python (for releasedocs) Python 用于自动化生成发布文档的过程 * bats (for shell code testing) 用于测试 Bash 脚本的工具,Hadoop 使用它来测试 shell 代码 * Node.js / bower / Ember-cli (for YARN UI v2 building) 构建 YARN 的 UI v2;Node.js 是一个 JavaScript 运行环境;Bower 是一个前端包管理工具;Ember-cli 是一个用于构建 Ember.js 应用的工具
1 2 3 4 5 6 7 8 9 10 11 12 CentOS Linux release 7.9.2009 (Core) java 8 maven 3.9.8 python2 Protocol 3.7.1 GCC 9.3.0 CMake 3.19 Boost 1.72.0 snappy-devel libzstd-devel isa-l Hadoop 3.4.0 源码
配置基本环境
操作均使用 root 用户,若不是 root 用户,确保拥有 sudo 权限
所涉及应用,均默认在 /opt 目录下
1. Yum 配置
配置 yum 源
1 2 3 4 5 6 7 8 # 备份原配置 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup # 使用国内 yum 源 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo # 生成完整缓存 yum clean all && yum makecache # 查看 yum 源是否添加成功 yum repolist
配置 epel
1 2 3 4 5 6 7 # 安装 epel 源 yum install epel-release -y curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # 生成完整缓存 yum clean all && yum makecache # 查看 yum 源是否添加成功 yum repolist
配置 scl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 # 安装 scl 源 yum install -y centos-release-scl centos-release-scl-rh scl-utils scl-utils-build # 备份 cp CentOS-SCLo-scl.repo{,.bak} cp CentOS-SCLo-scl-rh.repo{,.bak} # 修改 scl 配置 vi /etc/yum.repos.d/CentOS-SCLo-scl.repo [centos-sclo-sclo] name=CentOS-7 - SCLo sclo baseurl=https://mirrors.aliyun.com/centos/7/sclo/$basearch/sclo/ gpgcheck=0 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo vi /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo [centos-sclo-rh] name=CentOS-7 - SCLo rh baseurl=https://mirrors.aliyun.com/centos/7/sclo/$basearch/rh/ gpgcheck=0 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo # 刷新缓存 yum clean all && yum makecache # 查看 yum 源是否添加成功 yum repolist
2. 安装开发工具
1 yum -y groupinstall 'Development Tools'
3. 安装 Python2
安装 python2 去构建文档
4. 安装 Protocol Buffers
1 2 3 4 5 6 7 8 9 cd /opt git clone https://github.com/protocolbuffers/protobuf cd protobuf git checkout v3.7.1 autoreconf -i ./configure --prefix=/usr/local make make install cd ..
5. 安装必须包
1 yum -y install libtirpc-devel zlib-devel lz4-devel bzip2-devel openssl-devel cyrus-sasl-devel libpmem-devel libffi-devel
6. 安装 GCC
1 2 3 4 5 6 yum install devtoolset-9 -y source /opt/rh/devtoolset-9/enable # 检查版本 gcc --version g++ --version
7. 安装 CMAKE
1 2 3 4 5 6 7 cd /opt curl -L https://cmake.org/files/v3.19/cmake-3.19.0.tar.gz > cmake-3.19.0.tar.gz tar -zxvf cmake-3.19.0.tar.gz && cd cmake-3.19.0 ./bootstrap make -j$(nproc) make install cd ..
8. 安装 Boost
1 2 3 4 5 6 7 8 cd /opt curl -L -o boost_1_72_0.tar.bz2 https://sourceforge.net/projects/boost/files/boost/1.72.0/boost_1_72_0.tar.bz2/download tar xjf boost_1_72_0.tar.bz2 cd boost_1_72_0 ./bootstrap.sh --prefix=/usr/local ./b2 ./b2 install cd ..
9. 其他
snappy-devel : Snappy 压缩库的开发文件,用于 Hadoop 的高效压缩。
libzstd-devel : Zstandard 压缩库的开发文件,提供另一种压缩选项。
isa-l : Intel 提供的一套高效数据处理库,用于优化 Hadoop 的性能。
1 yum install snappy-devel libzstd-devel autoconf automake libtool wget -y
安装 nasm
1 2 3 4 5 6 7 8 cd /opt wget https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.bz2 tar -xvf nasm-2.15.05.tar.bz2 cd nasm-2.15.05 ./configure make make install cd ..
安装 yasm
1 2 3 4 5 6 7 8 cd /opt wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz tar -xvf yasm-1.3.0.tar.gz cd yasm-1.3.0 ./configure make make install cd ..
安装 isa-l
1 2 3 4 5 6 7 8 cd /opt git clone https://github.com/intel/isa-l cd isa-l/ ./autogen.sh ./configure make make install cd ..
10. 安装 Java
jdk 下载地址 ,选择 JDK8
1 2 3 cd /opt tar zxvf jdk-8u421-linux-x64.tar.gz mv jdk1.8.0_421 jdk
11. 安装 Maven
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 cd /opt wget https://dlcdn.apache.org/maven/maven-3/3.9.8/binaries/apache-maven-3.9.8-bin.tar.gz tar zxvf apache-maven-3.9.8-bin.tar.gz mv apache-maven-3.9.8 maven vim /opt/maven/conf/settings.xml # 配置本地仓库 <localRepository>/opt/repository</localRepository> # 配置阿里云服务器镜像 <!-- 阿里云仓库 --> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> </mirror> # 配置JDK <!-- java版本 --> <profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile>
12. 配置环境变量
1 2 3 4 5 6 7 8 9 10 11 12 # 编辑环境变量 vim ~/.bashrc export JAVA_HOME=/opt/jdk export MAVEN_HOME=/opt/maven export PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH # 刷新配置 source ~/.bashrc # 检查java和maven版本 java -version mvn -version
编译 Hadoop
1 2 3 4 cd /opt wget https://dlcdn.apache.org/hadoop/common/hadoop-3.4.0/hadoop-3.4.0-src.tar.gz tar zxvf hadoop-3.4.0-src.tar.gz cd /opt/hadoop-3.4.0-src
修改 pom 文件
maven-enforcer-plugin 插件是为了检查当前系统是否正确,可能因为我用的是 wsl2 的 CentOS7 系统,无法正确识别,这里根据的情况自行进行修改
修改 maven-enforcer-plugin 插件配置
修改 hadoop-3.4.0-src\hadoop-common-project\hadoop-common\pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 vi hadoop-common-project\hadoop-common\pom.xml <plugin> <groupId> org.apache.maven.plugins</groupId> <artifactId> maven-enforcer-plugin</artifactId> <executions> <execution> <id> enforce-os</id> <goals> <goal> enforce</goal> </goals> <configuration> <rules> <requireOS> <family> mac</family> <family> unix</family> <message> native build only supported on Mac or Unix</message> </requireOS> </rules> <fail> true</fail> <skip> true</skip> ### 在配置中加入这一行,来跳过这个插件 </configuration> </execution> </executions> </plugin>
修改 hadoop-3.4.0-src\hadoop-mapreduce-project\hadoop-mapreduce-client\hadoop-mapreduce-client-nativetask\pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 vi hadoop-mapreduce-project\hadoop-mapreduce-client\hadoop-mapreduce-client-nativetask\pom.xml <plugin> <groupId> org.apache.maven.plugins</groupId> <artifactId> maven-enforcer-plugin</artifactId> <executions> <execution> <id> enforce-os</id> <goals> <goal> enforce</goal> </goals> <configuration> <rules> <requireOS> <family> mac</family> <family> unix</family> <message> native build only supported on Mac or Unix</message> </requireOS> </rules> <fail> true</fail> <skip> true</skip> ### 在配置中加入这一行,来跳过这个插件 </configuration> </execution> </executions> </plugin>
编译
1 2 3 4 5 6 7 8 9 10 11 12 13 14 cd /opt/hadoop-3.4.0-src mvn clean package -Pdist,native,src -DskipTests -Dtar -X -e # 命令说明 # mvn 构建 java 项目 # clean 清理之前的构建,可省略 # package 编译项目的源代码并打包成一个 JAR 文件 # -Pdist,native,src 激活构建配置文件的选项 dist、native 和 src 是三个不同的 Maven 构建配置文件 # dist 用于构建分发版 # native 用于构建包含本地代码(C/C++代码)的版本 # src 用于包含源码的版本 # -DskipTests 构建过程中跳过运行测试 # -Dtar 构建过程中要生成一个.tar 包 # -X 调试输出模式,有助于排查问题 # -e 详细错误输出模式,显示完整的堆栈跟踪信息