搭建环境:Centos x 6.4 64bit

1、安装JDK

我这里用的是64位机,要下载对应的64位的JDK,下载地址:http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk7-downloads-1880260-zhs.html,选择对应的JDK版本,解压JDK,然后配置环境变量,


    1. vi /etc/profile



    注:这里有的人喜欢配置在当前用户里,我这里是配置的全局。

      1. export PATH  
      2. export JAVA_HOME=/opt/jdk1.7  
      3. export PATH=$PATH:$JAVA_HOME/bin  
      4.   
      5. source /etc/profile

      测试下JDK是否安装成功: java -version


        1. java version "1.7.0_45"  
        2. Java(TM) SE Runtime Environment (build 1.7.0_45-b18)  
        3. Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)



        2、编译前的准备(maven)

        maven官方下载地址,可以选择源码编码安装,这里就直接下载编译好的 就可以了



        1. wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.1.1/binaries/apache-maven-3.1.1-bin.zip

        解压文件后,同样在/etc/profie里配置环境变量



        1. export MAVEN_HOME=/opt/maven3.1.1  
        2. export PATH=$PATH:$MAVEN_HOME/bin

        验证配置是否成功: mvn -version



          1. Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 23:22:22+0800)  
          2. Maven home: /opt/maven3.1.1  
          3. Java version: 1.7.0_45, vendor: Oracle Corporation  
          4. Java home: /opt/jdk1.7/jre  
          5. Default locale: en_US, platform encoding: UTF-8  
          6. OS name: "linux", version: "2.6.32-358.el6.x86_64", arch: "amd64", family: "unix"



          3、编译hadoop

          这个地方你将会遇到各式各样的头疼问题

          首先官方下载hadoop源码


          1. wget http://mirrors.cnnic.cn/apache/hadoop/common/hadoop-2.2.0/hadoop-2.2.0-src.tar.gz

          如果是你32bit的机器,可以直接下载官方已经编译好的包,64bit的机子跑编译好的包跑不了。

                  由于maven国外服务器可能连不上,先给maven配置一下国内镜像,在maven目录下,conf/settings.xml,在<mirrors></mirros>里添加,原本的不要动


            1. <mirror>
            2. <id>nexus-osc</id>
            3. <mirrorOf>*</mirrorOf>
            4. <name>Nexusosc</name>
            5. <url>http://maven.oschina.net/content/groups/public/</url>
            6. </mirror>



                同样,在<profiles></profiles>内新添加


            1. <profile>
            2. <id>jdk-1.7</id>
            3. <activation>
            4. <jdk>1.4</jdk>
            5. </activation>
            6. <repositories>
            7. <repository>
            8. <id>nexus</id>
            9. <name>local private nexus</name>
            10. <url>http://maven.oschina.net/content/groups/public/</url>
            11. <releases>
            12. <enabled>true</enabled>
            13. </releases>
            14. <snapshots>
            15. <enabled>false</enabled>
            16. </snapshots>
            17. </repository>
            18. </repositories>
            19. <pluginRepositories>
            20. <pluginRepository>
            21. <id>nexus</id>
            22. <name>local private nexus</name>
            23. <url>http://maven.oschina.net/content/groups/public/</url>
            24. <releases>
            25. <enabled>true</enabled>
            26. </releases>
            27. <snapshots>
            28. <enabled>false</enabled>
            29. </snapshots>
            30. </pluginRepository>
            31. </pluginRepositories>
            32. </profile>

            编译clean

            1. cd hadoop2.2.0-src  
            2. mvn clean install –DskipTests


            发现异常

            1. [ERROR] Failed to execute goal org.apache.hadoop:hadoop-maven-plugins:2.2.0:protoc (compile-protoc) on project hadoop-common: org.apache.maven.plugin.MojoExecutionException: 'protoc --version' did not return a version ->
            2. [ERROR]   
            3. [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.  
            4. [ERROR] Re-run Maven using the -X switch to enable full debug logging.  
            5. [ERROR]   
            6. [ERROR] For more information about the errors and possible solutions, please read the following articles:  
            7. [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException  
            8. [ERROR]   
            9. [ERROR] After correcting the problems, you can resume the build with the command  
            10. [ERROR]   mvn <goals>


            hadoop2.2.0编译需要protoc2.5.0的支持,所以还要下载protoc,下载地址:https://code.google.com/p/protobuf/downloads/list,要下载2.5.0版本噢

            对protoc进行编译安装前先要装几个依赖包:gcc,gcc-c++,make 如果已经安装的可以忽略



              1. yum install gcc  
              2. yum intall gcc-c++  
              3. yum install make



              安装protoc


              1. tar -xvf protobuf-2.5.0.tar.bz2  
              2. cd protobuf-2.5.0  
              3. ./configure --prefix=/opt/protoc/  
              4. make && make install

              安装完配置下环境变量,就不多说了,跟上面过程一样。


              别急,还不要着急开始编译安装,不然又是各种错误,需要安装cmake,openssl-devel,ncurses-devel依赖


                1. yum install cmake  
                2. yum install openssl-devel  
                3. yum install ncurses-devel



                ok,现在可以进行编译了,


                  1. mvn package -Pdist,native -DskipTests -Dtar

                  现在可以拿出你的手机,玩会游戏了,慢慢等吧!


                  直到看到上面的内容那就说明编译完成了。

                  1. [INFO] ------------------------------------------------------------------------  
                  2. [INFO] Reactor Summary:  
                  3. [INFO]   
                  4. [INFO] Apache Hadoop Main ................................ SUCCESS [3.709s]  
                  5. [INFO] Apache Hadoop Project POM ......................... SUCCESS [2.229s]  
                  6. [INFO] Apache Hadoop Annotations ......................... SUCCESS [5.270s]  
                  7. [INFO] Apache Hadoop Assemblies .......................... SUCCESS [0.388s]  
                  8. [INFO] Apache Hadoop Project Dist POM .................... SUCCESS [3.485s]  
                  9. [INFO] Apache Hadoop Maven Plugins ....................... SUCCESS [8.655s]  
                  10. [INFO] Apache Hadoop Auth ................................ SUCCESS [7.782s]  
                  11. [INFO] Apache Hadoop Auth Examples ....................... SUCCESS [5.731s]  
                  12. [INFO] Apache Hadoop Common .............................. SUCCESS [1:52.476s]  
                  13. [INFO] Apache Hadoop NFS ................................. SUCCESS [9.935s]  
                  14. [INFO] Apache Hadoop Common Project ...................... SUCCESS [0.110s]  
                  15. [INFO] Apache Hadoop HDFS ................................ SUCCESS [1:58.347s]  
                  16. [INFO] Apache Hadoop HttpFS .............................. SUCCESS [26.915s]  
                  17. [INFO] Apache Hadoop HDFS BookKeeper Journal ............. SUCCESS [17.002s]  
                  18. [INFO] Apache Hadoop HDFS-NFS ............................ SUCCESS [5.292s]  
                  19. [INFO] Apache Hadoop HDFS Project ........................ SUCCESS [0.073s]  
                  20. [INFO] hadoop-yarn ....................................... SUCCESS [0.335s]  
                  21. [INFO] hadoop-yarn-api ................................... SUCCESS [54.478s]  
                  22. [INFO] hadoop-yarn-common ................................ SUCCESS [39.215s]  
                  23. [INFO] hadoop-yarn-server ................................ SUCCESS [0.241s]  
                  24. [INFO] hadoop-yarn-server-common ......................... SUCCESS [15.601s]  
                  25. [INFO] hadoop-yarn-server-nodemanager .................... SUCCESS [21.566s]  
                  26. [INFO] hadoop-yarn-server-web-proxy ...................... SUCCESS [4.754s]  
                  27. [INFO] hadoop-yarn-server-resourcemanager ................ SUCCESS [20.625s]  
                  28. [INFO] hadoop-yarn-server-tests .......................... SUCCESS [0.755s]  
                  29. [INFO] hadoop-yarn-client ................................ SUCCESS [6.748s]  
                  30. [INFO] hadoop-yarn-applications .......................... SUCCESS [0.155s]  
                  31. [INFO] hadoop-yarn-applications-distributedshell ......... SUCCESS [4.661s]  
                  32. [INFO] hadoop-mapreduce-client ........................... SUCCESS [0.160s]  
                  33. [INFO] hadoop-mapreduce-client-core ...................... SUCCESS [36.090s]  
                  34. [INFO] hadoop-yarn-applications-unmanaged-am-launcher .... SUCCESS [2.753s]  
                  35. [INFO] hadoop-yarn-site .................................. SUCCESS [0.151s]  
                  36. [INFO] hadoop-yarn-project ............................... SUCCESS [4.771s]  
                  37. [INFO] hadoop-mapreduce-client-common .................... SUCCESS [24.870s]  
                  38. [INFO] hadoop-mapreduce-client-shuffle ................... SUCCESS [3.812s]  
                  39. [INFO] hadoop-mapreduce-client-app ....................... SUCCESS [15.759s]  
                  40. [INFO] hadoop-mapreduce-client-hs ........................ SUCCESS [6.831s]  
                  41. [INFO] hadoop-mapreduce-client-jobclient ................. SUCCESS [8.126s]  
                  42. [INFO] hadoop-mapreduce-client-hs-plugins ................ SUCCESS [2.320s]  
                  43. [INFO] Apache Hadoop MapReduce Examples .................. SUCCESS [9.596s]  
                  44. [INFO] hadoop-mapreduce .................................. SUCCESS [3.905s]  
                  45. [INFO] Apache Hadoop MapReduce Streaming ................. SUCCESS [7.118s]  
                  46. [INFO] Apache Hadoop Distributed Copy .................... SUCCESS [11.651s]  
                  47. [INFO] Apache Hadoop Archives ............................ SUCCESS [2.671s]  
                  48. [INFO] Apache Hadoop Rumen ............................... SUCCESS [10.038s]  
                  49. [INFO] Apache Hadoop Gridmix ............................. SUCCESS [6.062s]  
                  50. [INFO] Apache Hadoop Data Join ........................... SUCCESS [4.104s]  
                  51. [INFO] Apache Hadoop Extras .............................. SUCCESS [4.210s]  
                  52. [INFO] Apache Hadoop Pipes ............................... SUCCESS [9.419s]  
                  53. [INFO] Apache Hadoop Tools Dist .......................... SUCCESS [2.306s]  
                  54. [INFO] Apache Hadoop Tools ............................... SUCCESS [0.037s]  
                  55. [INFO] Apache Hadoop Distribution ........................ SUCCESS [21.579s]  
                  56. [INFO] Apache Hadoop Client .............................. SUCCESS [7.299s]  
                  57. [INFO] Apache Hadoop Mini-Cluster ........................ SUCCESS [7.347s]  
                  58. [INFO] ------------------------------------------------------------------------  
                  59. [INFO] BUILD SUCCESS  
                  60. [INFO] ------------------------------------------------------------------------  
                  61. [INFO] Total time: 11:53.144s  
                  62. [INFO] Finished at: Fri Nov 22 16:58:32 CST 2013  
                  63. [INFO] Final Memory: 70M/239M  
                  64. [INFO] ------------------------------------------------------------------------

                  编译后的路径在:hadoop-2.2.0-src/hadoop-dist/target/hadoop-2.2.0

                    1. [root@localhost bin]# ./hadoop version  
                    2. Hadoop 2.2.0  
                    3. Subversion Unknown -r Unknown  
                    4. Compiled by root on 2013-11-22T08:47Z  
                    5. Compiled with protoc 2.5.0  
                    6. From source with checksum 79e53ce7994d1628b240f09af91e1af4  
                    7. This command was run using /data/hadoop-2.2.0-src/hadoop-dist/target/hadoop-2.2.0/share/hadoop/common/hadoop-common-2.2.0.jar



                    可以看出hadoop的版本

                      1. [root@localhost hadoop-2.2.0]# file lib//native/*  
                      2. lib//native/libhadoop.a:        current ar archive  
                      3. lib//native/libhadooppipes.a:   current ar archive  
                      4. lib//native/libhadoop.so:       symbolic link to `libhadoop.so.1.0.0'  
                      5. lib//native/libhadoop.so.1.0.0: <span style="color:#ff0000;">ELF 64-bit LSB shared object, x86-64, version 1</span>
                      6. lib//native/libhadooputils.a:   current ar archive  
                      7. lib//native/libhdfs.a:          current ar archive  
                      8. lib//native/libhdfs.so:         symbolic link to `libhdfs.so.0.0.0'  
                      9. lib//native/libhdfs.so.0.0.0:   <span style="color:#ff0000;">ELF 64-bit LSB shared object, x86-64, version 1</span>

                      注意红色字体部分,如果下载官网的编译好的包,这里显示的是32-bit。


                      hadoop编译成功,下面可以来部署集群。



                      5、部署集群准备

                           两台以上机器,修改hostname, ssh免登陆,关闭防火墙等

                      5.1、创建新用户


                      1. useradd hadoop  
                      2. su hadoop

                        注意以下操作有些需要root权限


                      5.2、修改主机名


                        1. vi /etc/sysconfig/network

                        1. hostname master

                        注销一下系统


                        1. [root@master ~]#

                        变成master了,修改生效

                        5.3、修改hosts


                        1. vi /etc/hosts  
                        2. 新增你的主机IP和HOSTNAME  
                        3.   
                        4. 192.168.10.10  master  
                        5. 192.168.10.11  slave1


                            5.4、ssh免登陆

                        查看ssh


                        1. [root@localhost data]# rpm -qa|grep ssh  
                        2. libssh2-1.4.2-1.el6.x86_64  
                        3. openssh-5.3p1-84.1.el6.x86_64  
                        4. openssh-server-5.3p1-84.1.el6.x86_64

                        缺少openssh-clients,


                        1. yum install openssh-clients

                              现在开始配置无密登录




                        1. [hadoop@master ~]$ cd /home/hadoop/  
                        2. [hadoop@master ~]$ ssh-keygen -t rsa

                        一路回车



                        1. [hadoop@master ~]$ cd .ssh/  
                        2. [hadoop@master .ssh]$ cp id_rsa.pub authorized_keys  
                        3. [hadoop@master .ssh]$ chmod 600 authorized_keys

                        把authorized_keys复制到其他要无密的机器上

                        1. [hadoop@master .ssh]$ scp authorized_keys hadoop@192.168.10.11:/home/hadoop/.ssh/

                        记得这里是以要以hadoop权限过去,不然会报权限错误

                        一般情况到这里就可以无密登录了,可是我怎么还是需要密码,经过一翻搜寻才知道这是centos6.4版本的问题,《关于centos ssh无密登录失败的记录》


                        1. [hadoop@master .ssh]$ ssh slave1  
                        2. Last login: Mon Nov 25 14:49:25 2013 from master  
                        3. [hadoop@slave1 ~]$

                        看到已经变成slave1了,说明成功鸟



                        6、开始集群配置工作

                        配置之前在要目录下创建三个目录,用来放hadooop文件和日志数据




                          1. [hadoop@master ~]$mkdir -p dfs/name  
                          2. [hadoop@master ~]$mkdir -p dfs/data  
                          3. [hadoop@master ~]$mkdir -p temp


                          把之前编译成功的版本移到hadoop目录下,注意目录权限问题

                          下面就开始配置文件

                          6.1  hadoop-env.sh

                          找到JAVA_HOME,把路径改为实际地址

                                  6.2  yarn-env.sh

                          同6.1

                          6.3  slave

                          配置所有slave节点

                          6.4 core-site.xml



                          1. <property>
                          2. <name>fs.defaultFS</name>
                          3. <value>hdfs://master:9000</value>
                          4. </property>
                          5. <property>
                          6. <name>io.file.buffer.size</name>
                          7. <value>131072</value>
                          8. </property>
                          9. <property>
                          10. <name>hadoop.tmp.dir</name>
                          11. <value>file:/home/hadoop/temp</value>
                          12. </property>
                          13. <property>
                          14. <name>hadoop.proxyuser.hadoop.hosts</name>
                          15. <value>*</value>
                          16. </property>
                          17. <property>
                          18. <name>hadoop.proxyuser.hadoop.groups</name>
                          19. <value>*</value>
                          20. </property>

                            注意fs.defaultFS为2.2.0新的变量,代替旧的:fs.default.name


                          6.5、hdfs-site.xml


                          1. <property>
                          2. <name>dfs.namenode.secondary.http-address</name>
                          3. <value>master:9001</value>
                          4. </property>
                          5. <property>
                          6. <name>dfs.namenode.name.dir</name>
                          7. <value>file:/home/hadoop/dfs/name</value>
                          8. </property>
                          9. <property>
                          10. <name>dfs.datanode.data.dir</name>
                          11. <value>file:/home/hadoop/dfs/data</value>
                          12. </property>
                          13. <span style="white-space:pre">  </span><property>
                          14. <name>dfs.replication</name>
                          15. <value>3</value>
                          16. </property>
                          17. <span style="white-space:pre">      </span><property>
                          18. <name>dfs.webhdfs.enabled</name>
                          19. <span style="white-space:pre">          </span><value>true</value>
                          20. </property>

                          新的:dfs.namenode.name.dir,旧:dfs.name.dir,新:dfs.datanode.name.dir,旧:dfs.data.dir


                          dfs.replication确定 data block的副本数目,hadoop基于rackawareness(机架感知)默认复制3份分block,(同一个rack下两个,另一个rack下一 份,按照最短距离确定具体所需block, 一般很少采用跨机架数据块,除非某个机架down了)



                          6.6、mapred-site.xml

                          这个地方需要把mapred-site.xml.template复制重新命名



                          1. <property>
                          2. <name>mapreduce.framework.name</name>
                          3. <value>yarn</value>
                          4. </property>
                          5. <property>
                          6. <name>mapreduce.jobhistory.address</name>
                          7. <value>cloud001:10020</value>
                          8. </property>
                          9. <property>
                          10. <name>mapreduce.jobhistory.webapp.address</name>
                          11. <value>master:19888</value>
                          12.   
                          13. </property>



                          新的计算框架取消了实体上的jobtracker, 故不需要再指定mapreduce.jobtracker.addres,而是要指定一种框架,这里选择yarn. 备注2:hadoop2.2.还支持第三方的计算框架,但没怎么关注过。


                          配置好以后将$HADOOP_HOME下的所有文件,包括hadoop目录分别copy到其它3个节点上。


                              6.7、yarn-site.xml


                          到这里基本配置好了,把所有复制到其他的slave节点。

                          1. <property>
                          2. <name>yarn.nodemanager.aux-services</name>
                          3. <value>mapreduce_shuffle</value>
                          4. </property>
                          5. <property>
                          6. <name>yarn.nodemanager.aux-services.mapreduce.shuffle.class</name>
                          7. <value>org.apache.hadoop.mapred.ShuffleHandler</value>
                          8. </property>
                          9. <property>
                          10. <name>yarn.resourcemanager.address</name>
                          11. <value>master:8032</value>
                          12. </property>
                          13. <property>
                          14. <name>yarn.resourcemanager.scheduler.address</name>
                          15. <value>master:8030</value>
                          16. </property>
                          17. <property>
                          18. <name>yarn.resourcemanager.resource-tracker.address</name>
                          19. <value>master:8031</value>
                          20. </property>
                          21. <property>
                          22. <name>yarn.resourcemanager.admin.address</name>
                          23. <value>master:8033</value>
                          24. </property>
                          25. <property>
                          26. <name>yarn.resourcemanager.webapp.address</name>
                          27. <value>master:8088</value>
                          28. </property>


                          7、启动hadoop

                          这里你可以进行环境变量设置,不举例了

                          7.1、格式化namenode


                          1. [hadoop@master hadoop]$ cd /home/hadoop/hadoop-2.2.0/bin/  
                          2. [hadoop@master bin]$ ./hdfs namenode -format

                          7.2、启动hdfs



                          1. [hadoop@master bin]$ cd ../sbin/  
                          2. [hadoop@master sbin]$ ./start-dfs.sh

                          这时候在master中输入jps应该看到namenode和secondarynamenode服务启动,slave中看到datanode服务启动



                          7.3、启动yarn

                          1. [hadoop@master sbin]$ ./start-yarn.sh


                          master中应该有ResourceManager服务,slave中应该有nodemanager服务

                          查看集群状态:./bin/hdfs dfsadmin –report

                          查看文件块组成:  ./bin/hdfsfsck / -files -blocks

                          查看HDFS:    http://192.168.10.10:50070

                          查看RM:    http:// 192.168.10.11:8088

                          8、安装中要注意的事项

                          8.1、注意版本,机器是32bit还是64位

                          8.2、注意依赖包的安装

                          8.3、写配置文件注意”空格“,特别是从别的地方copy的时候

                          8.4、关闭所有节点的防火墙

                          如果有看到类似"no route to host"这样的异常,基本就是防火墙没关

                          记得关的时候要切换到root帐号



                          1. (1) 重启后永久性生效:  
                          2.   
                          3. 开启:chkconfig iptables on  
                          4.   
                          5. 关闭:chkconfig iptables off  
                          6.   
                          7. (2) 即时生效,重启后失效:  
                          8.   
                          9. 开启:service iptables start  
                          10.   
                          11. 关闭:service iptables stop

                          8.5、开启datanode后自动关闭

                           基本是因为namenode和datanode的clusterID不一致,可以参考《解决hadoop集群中datanode启动后自动关闭的问题》


                          其他一些特殊异常只能google之了


                          9、运行测试例子



                          1. [hadoop@master bin]$ ./yarn jar ../share/hadoop/mapreduce/hadoop-mapreduce-examples-2.2.0.jar randomwriter /home/hadoop/dfs/input/

                          这里要注意不要用 -jar,不然会报异常“Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/util/ProgramDriver”



                          1. [hadoop@master bin]$ ./yarn jar ../share/hadoop/mapreduce/hadoop-mapreduce-examples-2.2.0.jar wordcount /home/hadoop/dfs/input/ /home/hadoop/dfs/output/

                          在input下面新建两个文件


                          1. $mkdir /dfs/input %echo ‘hello,world’ >>
                          2. $echo ‘hello, ruby’ >>
                          3.   
                          4. ./bin/hadoop fs -mkdir -p /home/hadoop/dfs/input  
                          5. ./bin/hadoop fs –put /home/hadoop/dfs/input /home/hadoop/test/test_wordcount/in  
                          6.   
                          7. 查看word count的计算结果:  
                          8. $bin/hadoop fs -cat /home/hadoop/test/test_wordcount/out/*  
                          9. hadoop 1  
                          10. hello  1  
                          11. ruby