hadoop集群群起小脚本

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状态

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值