如何编译Apache Hadoop2.6.0源代码
1.安装CentOS
我使用的是CentOS6.5,下载地址是,选择 下载,注意是64位的,大小是4GB,需要下载一段时间的。其实6.x的版本都可以,不一定是6.5。
我使用的是VMWare虚拟机,分配了2GB内存,20GB磁盘空间。内存太小,会比较慢;磁盘太小,编译时可能会出现空间不足的情况。上述不是最低配置,根据自己的机器配置修改吧。还有,一定要保持linux联网状态。
以下是按照各种软件,我把软件下载后全部复制到/usr/local目录下,以下命令执行的路径是在/usr/local目录下。请读者在阅读时,一定要注意路径。
2.安装JDK
hadoop是java写的,编译hadoop必须安装jdk。
从oracle官网下载jdk,下载地址是,选择 jdk-7u45-linux-x64.tar.gz下载。
执行以下命令解压缩jdk
tar -zxvf jdk-7u45-linux-x64.tar.gz
会生成一个文件夹jdk1.7.0_45,然后设置环境变量中。
执行命令 vi /etc/profile,增加以下内容到配置文件中,结果显示如下
3.安装maven
hadoop源码是使用maven组织管理的,必须下载maven。从maven官网下载,下载地址是,选择 apache-maven-3.3.3-bin.tar.gz 下载。
执行以下命令解压缩jdk
tar -zxvf apache-maven-3.3.3-bin.tar.gz
会生成一个文件夹apache-maven-3.3.3,然后设置环境变量中。
执行命令vi /etc/profile,编辑结果如下图所示
4.安装protoc
hadoop使用protocol buffer通信,从protoc官网下载protoc,下载地址是,选择protobuf-2.5.0.tar.gz 下载。
为了编译安装protoc,需要下载几个工具,顺序执行以下命令
yum -y install gcc
yum -y intall gcc-c++
yum -y install make
如果操作系统是CentOS6.5那么gcc和make已经安装了。其他版本不一定。
然后执行以下命令解压缩protobuf
tar -zxvf protobuf-2.5.0.tar.gz
会生成一个文件夹protobuf-2.5.0,执行以下命令编译protobuf。
cd protobuf-2.5.0
./configure --prefix=/usr/local/protoc/
make && make install
只要不出错就可以了。
执行完毕后,编译后的文件位于/usr/local/protoc/目录下,我们设置一下环境变量
执行命令vi /etc/profile,编辑结果如下图所示
5.安装其他依赖
顺序执行以下命令
yum -y install cmake
yum -y install openssl-devel
yum -y install ncurses-devel
安装完毕即可。
6.编译hadoop2.6源码
从hadoop官网下载2.6稳定版,下载地址是http://www.apache.org/dyn/closer.cgi/hadoop/common/hadoop-2.6.0/hadoop-2.6.0-src.tar.gz。
执行以下命令解压缩jdk
tar -zxvf hadoop-2.6.0-src.tar.gz
会生成一个文件夹 hadoop-2.6.0-src。
好了,现在进入到目录/usr/local/hadoop-2.6.0-src中,执行命令
cd /usr/local/hadoop-2.6.0-src
mvn package -DskipTests -Pdist,native
该命令会从外网下载依赖的jar,编译hadoop源码,需要花费很长时间,你可以吃饭了。
在等待N久之后,可以看到如下的结果:
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Apache Hadoop Main ................................. SUCCESS [ 4.414 s]
[INFO] Apache Hadoop Project POM .......................... SUCCESS [ 3.132 s]
[INFO] Apache Hadoop Annotations .......................... SUCCESS [ 5.377 s]
[INFO] Apache Hadoop Assemblies ........................... SUCCESS [ 0.623 s]
[INFO] Apache Hadoop Project Dist POM ..................... SUCCESS [ 3.624 s]
[INFO] Apache Hadoop Maven Plugins ........................ SUCCESS [ 7.253 s]
[INFO] Apache Hadoop MiniKDC .............................. SUCCESS [ 5.040 s]
[INFO] Apache Hadoop Auth ................................. SUCCESS [ 9.449 s]
[INFO] Apache Hadoop Auth Examples ........................ SUCCESS [ 5.894 s]
[INFO] Apache Hadoop Common ............................... SUCCESS [02:35 min]
[INFO] Apache Hadoop NFS .................................. SUCCESS [ 9.395 s]
[INFO] Apache Hadoop KMS .................................. SUCCESS [ 12.661 s]
[INFO] Apache Hadoop Common Project ....................... SUCCESS [ 0.064 s]
[INFO] Apache Hadoop HDFS ................................. SUCCESS [02:58 min]
[INFO] Apache Hadoop HttpFS ............................... SUCCESS [ 20.099 s]
[INFO] Apache Hadoop HDFS BookKeeper Journal .............. SUCCESS [ 8.216 s]
[INFO] Apache Hadoop HDFS-NFS ............................. SUCCESS [ 5.086 s]
[INFO] Apache Hadoop HDFS Project ......................... SUCCESS [ 0.061 s]
[INFO] hadoop-yarn ........................................ SUCCESS [ 0.091 s]
[INFO] hadoop-yarn-api .................................... SUCCESS [01:45 min]
[INFO] hadoop-yarn-common ................................. SUCCESS [ 38.766 s]
[INFO] hadoop-yarn-server ................................. SUCCESS [ 0.131 s]
[INFO] hadoop-yarn-server-common .......................... SUCCESS [ 14.831 s]
[INFO] hadoop-yarn-server-nodemanager ..................... SUCCESS [ 25.612 s]
[INFO] hadoop-yarn-server-web-proxy ....................... SUCCESS [ 6.043 s]
[INFO] hadoop-yarn-server-applicationhistoryservice ....... SUCCESS [ 8.443 s]
[INFO] hadoop-yarn-server-resourcemanager ................. SUCCESS [ 29.911 s]
[INFO] hadoop-yarn-server-tests ........................... SUCCESS [ 8.606 s]
[INFO] hadoop-yarn-client ................................. SUCCESS [ 10.038 s]
[INFO] hadoop-yarn-applications ........................... SUCCESS [ 0.118 s]
[INFO] hadoop-yarn-applications-distributedshell .......... SUCCESS [ 3.389 s]
[INFO] hadoop-yarn-applications-unmanaged-am-launcher ..... SUCCESS [ 2.003 s]
[INFO] hadoop-yarn-site ................................... SUCCESS [ 0.056 s]
[INFO] hadoop-yarn-registry ............................... SUCCESS [ 6.715 s]
[INFO] hadoop-yarn-project ................................ SUCCESS [ 3.798 s]
[INFO] hadoop-mapreduce-client ............................ SUCCESS [ 0.218 s]
[INFO] hadoop-mapreduce-client-core ....................... SUCCESS [ 40.412 s]
[INFO] hadoop-mapreduce-client-common ..................... SUCCESS [ 24.370 s]
[INFO] hadoop-mapreduce-client-shuffle .................... SUCCESS [ 10.642 s]
[INFO] hadoop-mapreduce-client-app ........................ SUCCESS [ 12.325 s]
[INFO] hadoop-mapreduce-client-hs ......................... SUCCESS [ 13.119 s]
[INFO] hadoop-mapreduce-client-jobclient .................. SUCCESS [ 6.762 s]
[INFO] hadoop-mapreduce-client-hs-plugins ................. SUCCESS [ 1.958 s]
[INFO] Apache Hadoop MapReduce Examples ................... SUCCESS [ 8.129 s]
[INFO] hadoop-mapreduce ................................... SUCCESS [ 3.937 s]
[INFO] Apache Hadoop MapReduce Streaming .................. SUCCESS [ 5.881 s]
[INFO] Apache Hadoop Distributed Copy ..................... SUCCESS [ 10.755 s]
[INFO] Apache Hadoop Archives ............................. SUCCESS [ 2.511 s]
[INFO] Apache Hadoop Rumen ................................ SUCCESS [ 8.135 s]
[INFO] Apache Hadoop Gridmix .............................. SUCCESS [ 5.524 s]
[INFO] Apache Hadoop Data Join ............................ SUCCESS [ 3.702 s]
[INFO] Apache Hadoop Ant Tasks ............................ SUCCESS [ 2.582 s]
[INFO] Apache Hadoop Extras ............................... SUCCESS [ 3.400 s]
[INFO] Apache Hadoop Pipes ................................ SUCCESS [ 7.537 s]
[INFO] Apache Hadoop OpenStack support .................... SUCCESS [ 7.347 s]
[INFO] Apache Hadoop Amazon Web Services support .......... SUCCESS [ 8.864 s]
[INFO] Apache Hadoop Client ............................... SUCCESS [ 5.480 s]
[INFO] Apache Hadoop Mini-Cluster ......................... SUCCESS [ 0.084 s]
[INFO] Apache Hadoop Scheduler Load Simulator ............. SUCCESS [ 5.272 s]
[INFO] Apache Hadoop Tools Dist ........................... SUCCESS [ 6.860 s]
[INFO] Apache Hadoop Tools ................................ SUCCESS [ 0.026 s]
[INFO] Apache Hadoop Distribution ......................... SUCCESS [ 31.834 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15:44 min
[INFO] Finished at: 2015-07-13T00:23:42-07:00
[INFO] Final Memory: 101M/326M
[INFO] ------------------------------------------------------------------------
[root@crxy96 hadoop-2.6.0-src]#
好了,编译完成了。
编译后的代码在/usr/local/hadoop-2.6.0-src/hadoop-dist/target下面,如下图。
生产source .jar
mvn source:jar 在 /hadoop-dist/target/hadoop-2.6.0/share/hadoop/hdfs/sources 目录下
7.动手时的注意事项
这是我整理好的所有资料,在编译过程中用到的各个包,这里都有。结构如下
如果对linux不熟悉,一定使用我们推荐的centos6.5 64位操作系统。因为本文介绍的各种操作都是针对该版本的操作系统。
编译过程中需要的jar依赖,我已经全部下载了,并且打包,大家可以使用我的把内容替换。Maven仓库的默认位置在~/.m2/repository中,大家解压我的repository替换自己的就行。
重要提示:一定要保证虚拟机的网络畅通。
a) 伪分布配置
1.把从文件夹“编译成功的hadoop2.6.0的64位版本”中解压hadoop-dist-2.6.0-binary-64.tar.gz 得到的hadoop-2.6.0放到/usr/local目录下。放好后,完整的目录结构是/usr/local/hadoop-2.6.0
如果是源代码编译的话,这里的路径指的是
2.文件夹“hadoop2.6.0伪分布配置文件”中的配置内容是伪分布设置。把这个目录中的所有内容复制到/usr/local/hadoop-2.6.0/etc/hadoop目录下,覆盖原有文件。
3.修改core-site.xml中的hdfs://crxy213.crxy:9000的值,改成自己的ip或者主机名
4.格式化,执行命令/usr/local/hadoop-2.6.0/sbin/hdfs namenode -format
5.启动,执行脚本/usr/local/hadoop-2.6.0/sbin/start-hadoop.sh
----------------------------bug
ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:3.0:descriptor (default-descriptor) on project hadoop-maven-plugins: The API of the mojo scanner is not compatible with this plugin version. Please check the plugin dependencies configured in the POM and ensure the versions match. /usr/java/jdk1.7.0_79/jre/lib/i386/xawt/libmawt.so: libXext.so.6: cannot open shared object file: No such file or directory -> [Help 1]
[ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.[ERROR] Re-run Maven using the -X switch to enable full debug logging.[ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles:[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException[ERROR] [ERROR] After correcting the problems, you can resume the build with the commandyum list | grep libXext
yum install libXext.i686 来安装。试过之后,不行,朋友过来说了下是不是环境变量有问题我检查了下jdk 是32位的。之后安装了64的,等了1个小时,成功!!!