1. 代码
(1) 创建脚本目录
mkdir -p ~/bin
(2) 创建脚本文件
touch ~/bin/hadoop-cluster
(3) 给脚本文件执行权限
chmod u+x hadoop-cluster
(4) 向加入脚本文件中增加脚本内容
#!/bin/bash
# 1. 环境变量配置
# HADOOP和JDK的位置
HADOOP_HOME=/home/hadoop/hadoop-3.1.3
JAVA_HOME=/home/hadoop/jdk1.8.0_144
# 需要操作的主机列表, 通过空格隔开
hostnames=(hadoop181 hadoop182 hadoop183)
user=`whoami`
# 2. 获取输入参数个数,如果没有参数,直接退出
argsCount=$#
if((argsCount==0)); then
exit 0;
fi
# 3. 接受参数
command=$1
# 4. 操作方法
function execute_cmd(){
echo "[DEBUG] $1 the $2 service "
$HADOOP_HOME/sbin/$1-$2.sh
}
# 5. 查看状态
function show_cluster_status(){
for host in "${hostnames[@]}"
do
echo "[DEBUG] ssh to $host to execute commands [ \" jps -l \"] "
ssh $user@$host "jps -l"
done
echo "[info] ========================================================="
}
# 6. 主命令
case $command in
"start" )
echo "[info] ready to start the Hadoop cluster "
execute_cmd start dfs
#echo "[DEBUG] Start the HDFS service "
#$HADOOP_HOME/sbin/start-dfs.sh
execute_cmd start yarn
#echo "[DEBUG] Start the YARN service "
#$HADOOP_HOME/sbin/start-yarn.sh
echo "[DEBUG] Start the HISTORY service "
$HADOOP_HOME/sbin/mr-jobhistory-daemon.sh start historyserver
echo "[info] System startup complete about the Hadoop cluster "
show_cluster_status
;;
"stop" )
echo "[info] ready to stop the Hadoop cluster "
execute_cmd stop yarn
# echo "[DEBUG] Stop the HDFS service "
# $HADOOP_HOME/sbin/stop-dfs.sh
execute_cmd stop dfs
# echo "[DEBUG] Stop the YARN service "
# $HADOOP_HOME/sbin/stop-yarn.sh
echo "[DEBUG] Stop the HISTORY service "
$HADOOP_HOME/sbin/mr-jobhistory-daemon.sh stop historyserver
echo "[info] System shutdown complete about the Hadoop cluster "
show_cluster_status
;;
"status" )
echo "[info] ready to show the Hadoop cluster status"
show_cluster_status
;;
* )
echo "i don't know what do you want!"
;;
esac
2. 效果演示
(1) 启动服务
hadoop-cluster start
(2) 停止服务
# 我放在了 $USER_HOME/bin 目录中,所以在这个用户下任意位置都能用
hadoop-cluster stop
3. 注意事项
这个脚本使用前还是需要先启动 zookeeper 的, zookeeper的启动命令这个里面并没有包含, 所以每次使用前都要先看看zk状态