大数据常用shell脚本之ha-hadoop脚本

但使书种多,会有岁稔时。这篇文章主要讲述大数据常用shell脚本之ha-hadoop脚本相关的知识,希望能为你提供帮助。


一、前置准备CentOS7、jdk1.8、zookeeper-3.5.7、hadoop-2.7.7
想要完成本期视频中所有操作,需要以下准备:


二、简单了解


  • KaTeX parse error: Undefined control sequence: \\* at position 1: \\?*?表示传递给函数或脚本的所有参数…*”会将所有的参数作为一个整体
  • 延时函数??sleep???,默认以秒为单位,也可以自己指定延迟的单位。
    sleep 1s 延迟一秒
    sleep 1m 延迟一分钟
    sleep 1h 延迟一小时
    sleep 1d 延迟一天

三、脚本源码
#!/bin/bash



USAGE="使用方法:sh ha-hadoop.sh start/stop/status"
if [ $# -ne 1 ]; then
echo $USAGE
exit 1
fi
SHELL_ZK=/home/xiaokang/bin/zk.sh
HADOOP_HOME=/opt/software/hadoop-2.7.7
NODES=("hadoop01" "hadoop02" "hadoop03")
case $1 in
"start")
#启动zookeeper集群
$SHELL_ZK start
sleep 4s
#开始启动ha-hadoop集群
for NODE in ${NODES[*]}; do
echo "--------$NODE启动ha-hadoop集群--------"
if [ "hadoop01" = $NODE ]; then
ssh $NODE "$HADOOP_HOME/sbin/start-dfs.sh & & $HADOOP_HOME/sbin/mr-jobhistory-daemon.sh start historyserver"
fi
if [ "hadoop02" = $NODE ]; then
ssh $NODE "$HADOOP_HOME/sbin/yarn-daemon.sh start resourcemanager"
fi
if [ "hadoop03" = $NODE ]; then
ssh $NODE "$HADOOP_HOME/sbin/start-yarn.sh"
fi
done
; ;
"stop")
#开始停止ha-hadoop集群
for NODE in ${NODES[*]}; do
echo "--------$NODE停止ha-hadoop集群--------"
if [ "hadoop01" = $NODE ]; then
ssh $NODE "$HADOOP_HOME/sbin/stop-dfs.sh & & $HADOOP_HOME/sbin/mr-jobhistory-daemon.sh stop historyserver"
fi
if [ "hadoop02" = $NODE ]; then
ssh $NODE "$HADOOP_HOME/sbin/yarn-daemon.sh stop resourcemanager"
fi
if [ "hadoop03" = $NODE ]; then
ssh $NODE "$HADOOP_HOME/sbin/stop-yarn.sh"
fi
done
#停止zookeeper集群
$SHELL_ZK stop
; ;
"status")
for NODE in ${NODES[*]}; do
echo "--------$NODE查看ha-hadoop集群进程--------"
ssh $NODE "jps"
done
; ;
*)
echo $USAGE
; ;
esac



四、测试使用
# 修改脚本权限
[xiaokang@hadoop01 bin]$ chmod 777 ha-hadoop.sh

执行方式:
# 方式一
[xiaokang@hadoop01 bin]$ ./ha-hadoop.sh start
# 方式二
[xiaokang@hadoop01 bin]$ sh ha-hadoop.sh start
# 方式三
# 1.将shell脚本所在目录配置为环境变量(/etc/profile),记得source生效
export SHELL_HOME=/home/xiaokang/bin
export PATH=${java_HOME}/bin:${HADOOP_HOME}/bin:${HADOOP_HOME}/sbin:${ZOOKEEPER_HOME}/bin:${HIVE_HOME}/bin:${HBASE_HOME}/bin:${KYLIN_HOME}/bin:${KAFKA_HOME}/bin:${SCALA_HOME}/bin:${SCALA_HOME}/sbin:${SPARK_HOME}/bin:${SHELL_HOME}/:$PATH
# 2.在任意目录都可以使用自己编写的shell脚本
[xiaokang@hadoop01 ~]$ ha-hadoop.sh start

使用脚本进行启动和查看进程测试,成功如下图所示:
【大数据常用shell脚本之ha-hadoop脚本】

五、拓展脚本查看集群进程脚本??call-cluster.sh??:
#!/bin/bash


USAGE="使用方法:sh call-cluster.sh jps or sh call-cluster.sh jps; java -version"
if [ $# -eq 0 ]; then
echo $USAGE
exit 1
fi
NODES=("hadoop01" "hadoop02" "hadoop03")
for NODE in ${NODES[*]}; do
echo "--------$NODE--------"
ssh $NODE "$*"
done
echo "----------------------------------------------------------------------------------------"
echo "--------c



不仅仅可以查看进程,还能同时完成好多事情,比如既查看集群进程也查看java版本:
[xiaokang@hadoop01 ~]$ call-cluster.sh jps; java -version



    推荐阅读