1.下载组件

首先去CDH网站上下载hadoop组件

地址:http://archive.cloudera.com/cdh5/cdh/5/

注意版本号要与其他的组件CDH版本一致

cdh hadoop的劣势 cdh hadoop 教程_cdh hadoop的劣势

 

2.环境配置

设置主机名和用户名

配置静态IP

配置SSH免密登录

配置JDK

3.配置HADOOP

1.新建用户hadoop,从root用户获取/opt文件夹的权限,所有节点都要执行



useradd -m hadoop -s /bin/bash
passwd hadoop
chown -R hadoop /opt/module/hadoop
chown -R hadoop /usr/sunny



 为hadoop用户添加管理权限



visudo



## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
##      user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
hadoop  ALL=(ALL)       ALL



 

2.hadoop的安装路径不推荐安装在/home/hadoop目录下,推荐安装在/opt目录下,然后切换到hadoop用户,解压文件后将hadoop转移到/opt/module下,并修改文件夹名称为hadoop



tar -zxvf hadoop-2.6.0-cdh5.12.0.tar.gz
mv hadoop-2.6.0-cdh5.12.0 /opt/module/hadoop



修改hadoop文件夹的权限



sudo chown -R hadoop:hadoop hadoop



 

3.配置环境变量



vim ~/.bash_profile



export HADOOP_HOME=/opt/module/hadoop
export HADOOP_INSTALL=$HADOOP_HOME
export HADOOP_MAPRED_HOME=$HADOOP_HOME
export HADOOP_COMMON_HOME=$HADOOP_HOME
export HADOOP_HDFS_HOME=$HADOOP_HOME
export YARN_HOME=$HADOOP_HOME
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin



source ~/.bash_profile



 

4.修改配置文件

配置文件的位置为hadoop-2.6.0-cdh5.12.0/etc/hadoop目录下,主要文件:

配置名称

类型

说明

hadoop-env.sh

Bash脚本

Hadoop运行环境变量设置

core-site.xml

xml

Hadoop的配置项,例如HDFS和MapReduce常用的I/O设置等

hdfs-site.xml

xml

HDFS守护进程的配置项,包括NameNode、SecondaryNameNode、DataNode、JN等

yarn-env.sh

Bash脚本

Yarn运行环境变量设置

yarn-site.xml

xml

YARN守护进程的配置项,包括ResourceManager和NodeManager等

mapred-site.xml

xml

MapReduce计算框架的配置项

capacity-scheduler.xml

xml

Yarn调度属性设置

container-executor.cfg

Cfg

Yarn Container配置

mapred-queues.xml

xml

MR队列设置

hadoop-metrics.properties

Java属性

控制metrics在Hadoop上如何发布的属性

hadoop-metrics2.properties

Java属性

控制metrics在Hadoop上如何发布的属性

slaves

Plain Text

运行DataNode和NodeManager的机器列表,每行一个

exclude

Plain Text

移除DN节点配置文件

log4j.properties

 

系统日志文件、NameNode审计日志DataNode子进程的任务日志的属性

configuration.xsl

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(1)修改hadoop-env.sh文件,在文件末尾增加环境变量




#--------------------Java Env------------------------------
export JAVA_HOME=/opt/module/jdk1.8.0_144
#--------------------Hadoop Env----------------------------
export HADOOP_HOME=/opt/module/hadoop-2.6.0-cdh5.12.0
#--------------------Hadoop Daemon Options-----------------
# export HADOOP_NAMENODE_OPTS="-Dhadoop.security.logger=${HADOOP_SECURITY_LOGGER:-INFO,RFAS} -Dhdfs.audit.logger=${HDFS_AUDIT_LOGGER:-INFO,NullAppender} $HADOOP_NAMENODE_OPTS"
# export HADOOP_DATANODE_OPTS="-Dhadoop.security.logger=ERROR,RFAS $HADOOP_DATANODE_OPTS"

#--------------------Hadoop Logs---------------------------
#export HADOOP_LOG_DIR=${HADOOP_LOG_DIR}/$USER
#--------------------SSH PORT-------------------------------
#export HADOOP_SSH_OPTS="-p 6000" #如果你修改了SSH登录端口,一定要修改此配置。



 

(2)修改core-site.xml



<configuration>
        <property>
                <name>fs.defaultFS</name>
                <value>hdfs://node1.sunny.cn:9000</value>
        </property>
        <property>
                <name>hadoop.tmp.dir</name>
                <value>file:/usr/sunny/hadoop/tmp</value>
                <description>Abase for other temporary directories.</description>
        </property>
</configuration>



这一步是设置提供HDFS服务的主机名和端口号,也就是说HDFS通过master的9000端口提供服务,这项配置也指明了NameNode所运行的节点,即主节点

 

(3)修改hdfs-site.xml



<configuration>
        <property>
                <name>dfs.namenode.secondary.http-address</name>
                <value>node1.sunny.cn:50090</value>
        </property>
     <property>
                <name>dfs.replication</name>
                <value>2</value>
        </property>
        <property>
                <name>dfs.namenode.name.dir</name>
                <value>/usr/sunny/hadoop/hdfs/name</value>
        </property>
        <property>
                <name>dfs.datanode.data.dir</name>
                <value>/usr/sunny/hadoop/hdfs/data</value>
        </property>
</configuration>



 

以下为网络方案,仅供参考



<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
  <!--开启web hdfs -->
  <property>
    <name>dfs.webhdfs.enabled</name>
    <value>true</value>
  </property>
  <!-- 存放name table本地目录 -->
  <property>
    <name>dfs.namenode.name.dir</name>
    <value>/usr/sunny/hadoop/hdfs/name</value>
  </property>
  <!-- NameNode存放reansaction file(edits)本地目录 -->
  <property>
    <name>dfs.namenode.edits.dir</name>
    <value>${dfs.namenode.name.dir}</value>
  </property>
  <!-- DataNode存放block本地目录 -->
  <property>
    <name>dfs.datanode.data.dir</name>
    <value>/home/hadoopuser/hadoop-2.6.0-cdh5.6.0/data/dfs/data</value>
  </property>
  <!-- 文件副本个数,默认为3份 -->
  <property>
    <name>dfs.replication</name>
    <value>1</value>
  </property>
  <!-- 块大小 256M(默认) -->
  <property>
    <name>dfs.blocksize</name>
    <value>268435456</value>
  </property>

  <!--======================================================================= -->
  <!--HDFS高可用配置 -->
  <!--nameservices逻辑名 -->
  <property>
    <name>dfs.nameservices</name>
    <value>hadoop-cluster</value>
  </property>
  <!-- 设置NameNode IDs 5.6版本只支持2个NameNode-->
  <property>
    <name>dfs.ha.namenodes.hadoop-cluster</name>
    <value>namenode1,namenode2</value>
  </property>
  <!-- Hdfs HA: dfs.namenode.rpc-address.[nameservice ID] rpc 通信地址 -->
  <property>
    <name>dfs.namenode.rpc-address.mycluster.nn1</name>
    <value>namenode1:8020</value>
  </property>
  <property>
    <name>dfs.namenode.rpc-address.mycluster.nn2</name>
    <value>namenode2:8020</value>
  </property>
  <!-- Hdfs HA: dfs.namenode.http-address.[nameservice ID] http 通信地址 -->
  <property>
    <name>dfs.namenode.http-address.hadoop-cluster.namenode1</name>
    <value>namenode1:50070</value>
  </property>
  <property>
    <name>dfs.namenode.http-address.hadoop-cluster.namenode2</name>
    <value>namenode2:50070</value>
  </property>

  <!--==================Namenode editlog同步 ============================================ -->
  <!--保证数据恢复 -->
  <property>
    <name>dfs.journalnode.http-address</name>
    <value>0.0.0.0:8480</value>
  </property>
  <property>
    <name>dfs.journalnode.rpc-address</name>
    <value>0.0.0.0:8485</value>
  </property>
  <!--设置JournalNode服务器地址,QuorumJournalManager用于存储editlog -->
  <!--格式:qjournal://<host1:port1>;<host2:port2>;<host3:port3>/<journalId> 端口同journalnode.rpc-address -->
  <property>
    <name>dfs.namenode.shared.edits.dir</name>
    <value>qjournal://namenode1:8485;namenode2:8485;namenode3:8485/hadoop-cluster</value>
  </property>
  <!--JournalNode存放数据地址 -->
  <property>
    <name>dfs.journalnode.edits.dir</name>
    <value>/usr/sunny/hadoop/hdfs/journal</value>
  </property>
  <!--==================DataNode editlog同步 ============================================ -->
  <property>
    <!--DataNode,Client连接Namenode识别选择Active NameNode策略 -->
    <name>dfs.client.failover.proxy.provider.mycluster</name>
    <value>org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider</value>
  </property>
  <!--==================Namenode fencing:=============================================== -->
  <!--Failover后防止停掉的Namenode启动,造成两个服务 -->
  <property>
    <name>dfs.ha.fencing.methods</name>
    <value>sshfence</value>
  </property>
  <property>
    <name>dfs.ha.fencing.ssh.private-key-files</name>
    <value>/home/hadoopuser/.ssh/id_rsa</value>
  </property>
  <property>
    <!--多少milliseconds 认为fencing失败 -->
    <name>dfs.ha.fencing.ssh.connect-timeout</name>
    <value>30000</value>
  </property>

  <!--==================NameNode auto failover base ZKFC and Zookeeper====================== -->
  <!--开启基于Zookeeper及ZKFC进程的自动备援设置,监视进程是否死掉 -->
  <property>
    <name>dfs.ha.automatic-failover.enabled</name>
    <value>true</value>
  </property>
  <property>
    <name>ha.zookeeper.quorum</name>
    <!--<value>Zookeeper-01:2181,Zookeeper-02:2181,Zookeeper-03:2181</value>-->
    <value>Hadoop-DN-01:2181,Hadoop-DN-02:2181,Hadoop-DN-03:2181</value>
  </property>
  <property>
    <!--指定ZooKeeper超时间隔,单位毫秒 -->
    <name>ha.zookeeper.session-timeout.ms</name>
    <value>2000</value>
  </property>
</configuration>



dfs.replication配置hdfs中文件的副本数为3,HDFS会自动对文件做冗余处理,这项配置就是配置文件的冗余数,3表示有2份冗余。

dfs.name.dir设置NameNode的元数据存放的本地文件系统路径

dfs.data.dir设置DataNode存放数据的本地文件系统路径

 

(4)修改mapred-site.xml

目录中只有一个mapred-site.xml.template文件,cp一份出来



<configuration>
        <property>
                <name>mapreduce.framework.name</name>
                <value>yarn</value>
        </property>
        <property>
                <name>mapreduce.jobhistory.address</name>
                <value>node1.sunny.cn:10020</value>
        </property>
        <property>
                <name>mapreduce.jobhistory.webapp.address</name>
                <value>node1.sunny.cn:19888</value>
        </property>
</configuration>



以下为网络方案,仅供参考



<configuration>
        <!-- 配置JVM大小 -->
        <property>
                <name>mapred.child.java.opts</name>
                <value>-Xmx1000m</value>
                <final>true</final>
                <description>final=true表示禁止用户修改JVM大小</description>
        </property>
        <!-- 配置 MapReduce Applications -->
        <property>
                <name>mapreduce.framework.name</name>
                <value>yarn</value>
        </property>
        <!-- JobHistory Server ============================================================== -->
        <!-- 配置 MapReduce JobHistory Server 地址 ,默认端口10020 -->
        <property>
                <name>mapreduce.jobhistory.address</name>
                <value>0.0.0.0:10020</value>
        </property>
        <!-- 配置 MapReduce JobHistory Server web ui 地址, 默认端口19888 -->
        <property>
                <name>mapreduce.jobhistory.webapp.address</name>
                <value>0.0.0.0:19888</value>
        </property>
</configuration>



 

(5)修改yarn-site.xml



<configuration>
        <property>
                <name>yarn.resourcemanager.hostname</name>
                <value>node1.sunny.cn</value>
        </property>
        <property>
                <name>yarn.nodemanager.aux-services</name>
                <value>mapreduce_shuffle</value>
        </property>
</configuration>



以下为网络方案,仅供参考



<configuration>
        <!-- nodemanager 配置 ================================================= -->
        <property>
                <name>yarn.nodemanager.aux-services</name>
                <value>mapreduce_shuffle</value>
        </property>
        <property>
                <name>yarn.nodemanager.aux-services.mapreduce.shuffle.class</name>
                <value>org.apache.hadoop.mapred.ShuffleHandler</value>
        </property>
        <property>
                <description>Address where the localizer IPC is.</description>
                <name>yarn.nodemanager.localizer.address</name>
                <value>0.0.0.0:23344</value>
        </property>
        <property>
                <description>NM Webapp address.</description>
                <name>yarn.nodemanager.webapp.address</name>
                <value>0.0.0.0:23999</value>
        </property>

        <!-- HA 配置 =============================================================== -->
        <!-- Resource Manager Configs -->
        <property>
                <name>yarn.resourcemanager.connect.retry-interval.ms</name>
                <value>2000</value>
        </property>
        <property>
                <name>yarn.resourcemanager.ha.enabled</name>
                <value>true</value>
        </property>
        <property>
                <name>yarn.resourcemanager.ha.automatic-failover.enabled</name>
                <value>true</value>
        </property>
        <!-- 使嵌入式自动故障转移。HA环境启动,与 ZKRMStateStore 配合 处理fencing -->
        <property>
                <name>yarn.resourcemanager.ha.automatic-failover.embedded</name>
                <value>true</value>
        </property>
        <!-- 集群名称,确保HA选举时对应的集群 -->
        <property>
                <name>yarn.resourcemanager.cluster-id</name>
                <value>yarn-cluster</value>
        </property>
        <property>
                <name>yarn.resourcemanager.ha.rm-ids</name>
                <value>rm1,rm2</value>
        </property>
        <!--这里RM主备结点需要单独指定,(可选)
        <property>
                <name>yarn.resourcemanager.ha.id</name>
                <value>rm2</value>
        </property>
        -->
        <property>
                <name>yarn.resourcemanager.scheduler.class</name>
                <value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairScheduler</value>
        </property>
        <property>
                <name>yarn.resourcemanager.recovery.enabled</name>
                <value>true</value>
        </property>
        <property>
                <name>yarn.app.mapreduce.am.scheduler.connection.wait.interval-ms</name>
                <value>5000</value>
        </property>
        <!-- ZKRMStateStore 配置 -->
        <property>
                <name>yarn.resourcemanager.store.class</name>
                <value>org.apache.hadoop.yarn.server.resourcemanager.recovery.ZKRMStateStore</value>
        </property>
        <property>
                <name>yarn.resourcemanager.zk-address</name>
                <!--<value>Zookeeper-01:2181,Zookeeper-02:2181,Zookeeper-03:2181</value>-->
                <value>Hadoop-DN-01:2181,Hadoop-DN-02:2181,Hadoop-DN-03:2181</value>
        </property>
        <property>
                <name>yarn.resourcemanager.zk.state-store.address</name>
                <!--<value>Zookeeper-01:2181,Zookeeper-02:2181,Zookeeper-03:2181</value>-->
                <value>Hadoop-DN-01:2181,Hadoop-DN-02:2181,Hadoop-DN-03:2181</value>
        </property>
        <!-- Client访问RM的RPC地址 (applications manager interface) -->
        <property>
                <name>yarn.resourcemanager.address.rm1</name>
                <value>Hadoop-NN-01:23140</value>
        </property>
        <property>
                <name>yarn.resourcemanager.address.rm2</name>
                <value>Hadoop-NN-02:23140</value>
        </property>
        <!-- AM访问RM的RPC地址(scheduler interface) -->
        <property>
                <name>yarn.resourcemanager.scheduler.address.rm1</name>
                <value>Hadoop-NN-01:23130</value>
        </property>
        <property>
                <name>yarn.resourcemanager.scheduler.address.rm2</name>
                <value>Hadoop-NN-02:23130</value>
        </property>
        <!-- RM admin interface -->
        <property>
                <name>yarn.resourcemanager.admin.address.rm1</name>
                <value>Hadoop-NN-01:23141</value>
        </property>
        <property>
                <name>yarn.resourcemanager.admin.address.rm2</name>
                <value>Hadoop-NN-02:23141</value>
        </property>
        <!--NM访问RM的RPC端口 -->
        <property>
                <name>yarn.resourcemanager.resource-tracker.address.rm1</name>
                <value>Hadoop-NN-01:23125</value>
        </property>
        <property>
                <name>yarn.resourcemanager.resource-tracker.address.rm2</name>
                <value>Hadoop-NN-02:23125</value>
        </property>
        <!-- RM web application 地址 -->
        <property>
                <name>yarn.resourcemanager.webapp.address.rm1</name>
                <value>Hadoop-NN-01:23188</value>
        </property>
        <property>
                <name>yarn.resourcemanager.webapp.address.rm2</name>
                <value>Hadoop-NN-02:23188</value>
        </property>
        <property>
                <name>yarn.resourcemanager.webapp.https.address.rm1</name>
                <value>Hadoop-NN-01:23189</value>
        </property>
        <property>
                <name>yarn.resourcemanager.webapp.https.address.rm2</name>
                <value>Hadoop-NN-02:23189</value>
        </property>
</configuration>



 

(6)修改slaves文件

配置的都是datanode



node2.sunny.cn
node3.sunny.cn



 

5.初始化namenode



hdfs namenode -format



 

6.启动Hadoop



start-dfs.sh
start-yarn.sh        (可以start-all.sh)
mr-jobhistory-daemon.sh start historyserver



启动后的进程

cdh hadoop的劣势 cdh hadoop 教程_cdh hadoop的劣势_02

如果datanode启动不成功,需要把所有hadoop下tmp文件夹删掉再重新格式化namenode

访问地址:http://192.168.2.11:50070

 

cdh hadoop的劣势 cdh hadoop 教程_cdh hadoop的劣势_03

 

 7.执行分布式实例

(1) 在HDFS 上创建用户目录



hdfs dfs -mkdir -p /user/hadoop



==============================前方高能,建议先行扫描====================================================

在此过程中可能会报出警告:



WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable 



意思是无法加载本地native库,位于hadoop/lib/native目录,这时候就需要去下载源码编译hadoop-2.6.0-cdh5.12.0-src.tar.gz

 下载后上传到服务器并解压,进入到解压后的源码目录下执行命令



mvn package -Dmaven.javadoc.skip=true -Pdist,native -DskipTests -Dtar



打包过程中出现错误



Detected JDK Version: 1.8.0-144 is not in the allowed range [1.7.0,1.7.1000}



表示源码编译需要1.7的JDK,而实际是1.8,所以把jdk降级下

source ~/.bash_profile后jdk依然是1.8,关掉所有进程后依然没变,所以reboot,然后解决

继续编译源码,大概20、30、40、50、60、70多分钟,注意修改mvn的镜像地址,使用国内地址的会快一些

 ...

好吧,编译到common时继续报错



[ERROR] Failed to execute goal org.apache.hadoop:hadoop-maven-plugins:2.6.0-cdh5.12.0:protoc (compile-protoc) on project hadoop-common: org.apache.maven.plugin.MojoExecutionException: 'protoc --version' did not return a version -> [Help 1]



查阅资料得知:

protobuf是google提供的一个可以编码格式化结构数据方法,Google大部分的RPC端通信协议都是基于protocol buffers的。同时现Hadoop中master和slave中的RPC通信协议也都是基于它实现的。所以下载吧。

需要安装protoc,版本protobuf-2.5.0,但目前google官方链接下载不了。好人的下载链接:http://pan.baidu.com/s/1pJlZubT

上传到服务器之后解压,然后准备编译

编译protoc之前还要先安装gccgcc-c++make,否则又是一堆错误



yum install gcc  
yum intall gcc-c++  
yum install make 

tar -xvf protobuf-2.5.0.tar.bz2  
cd protobuf-2.5.0  
./configure --prefix=/opt/module/protoc/  
make && make install



编译好之后记得配置下环境变量



PROTOC_HOME="/opt/module/protoc"
export PATH=$PATH:$PROTOC_HOME/bin



然后source下

然后可以继续编译hadoop源码了

 OK

终于编译成功,大约持续了N久

编译好的包在hadoop-src/hadoop-dist/target/hadoop-2.4.1.tar.gz

把tar包复制到本地hadoop/lib/native目录下并将tar包的native里的文件复制到本地hadoop/lib/native下

使用命令查看native库的版本



ldd libhadoop.so.1.0.0



cdh hadoop的劣势 cdh hadoop 教程_cdh hadoop的劣势_04

 然后将这些文件分发到其他的节点上



scp * root@node2.sunny.cn:/opt/module/hadoop/lib/native/
scp * root@node3.sunny.cn:/opt/module/hadoop/lib/native/



然后需要修改下~/.bash_profile文件



export HADOOP_OPTS="-Djava.library.path=$HADOOP_HOME/lib:$HADOOP_COMMON_LIB_NATIVE_DIR"



然后source下

验证OK

==============================高能区域结束====================================================

 继续执行

(2)



hdfs dfs -mkdir input
hdfs dfs -put /usr/local/hadoop/etc/hadoop/*.xml input
hadoop jar /opt/moudle/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-*.jar grep input output 'dfs[a-z.]+'



查看进度:http://192.168.2.11:8088/cluster

查看输出结果



hdfs dfs -cat output/*



若要再次运行需要把output删除掉 



hdfs dfs -rm -r output



 

附注:

关闭hadoop集群

在node1上执行:



stop-yarn.sh

stop-dfs.sh (stop-all.sh)

mr-jobhistory-daemon.sh stop historyserver



具体操作:

hadoop上基础操作



hadoop fs -ls *** (hdfs dfs –ls ***) 查看列表

hadoop fs -mkdir  *** (hdfs dfs –mkdir ***) 创建文件夹

hadoop fs -rm -r *** (hdfs dfs –rm -r ***) 删除文件夹

hadoop fs -put *** (hdfs dfs –put ***) 上传文件到hdfs

hadoop fs -get *** (hdfs dfs –get ***) 下载文件

hadoop fs –cp *** (hdfs dfs –cp ***) 拷贝文件

hadoop fs -cat *** (hdfs dfs –cat ***) 查看文件

hadoop fs -touchz *** (hdfs dfs –touchz ***) 创建空文件



列出所有Hadoop Shell支持的命令:



hadoop fs –help



Java结合例子:

java程序操作

首先导入hadoop相关jar包

新建一个User library

点击Window-->Preferences 搜索框中输入User Libraries

 

cdh hadoop的劣势 cdh hadoop 教程_运维_05

cdh hadoop的劣势 cdh hadoop 教程_cdh hadoop的劣势_06

cdh hadoop的劣势 cdh hadoop 教程_hadoop_07

cdh hadoop的劣势 cdh hadoop 教程_java_08

cdh hadoop的劣势 cdh hadoop 教程_大数据_09

cdh hadoop的劣势 cdh hadoop 教程_hadoop_10

cdh hadoop的劣势 cdh hadoop 教程_hadoop_11

cdh hadoop的劣势 cdh hadoop 教程_大数据_12

cdh hadoop的劣势 cdh hadoop 教程_java_13

cdh hadoop的劣势 cdh hadoop 教程_java_14

cdh hadoop的劣势 cdh hadoop 教程_cdh hadoop的劣势_15