此工具(config-dispatcher.sh)用于分发Hadoop集群的配置文件,可省去一个一个替换的麻烦,希望能给大家带来便利!
注:另外Hadoop集群文件分发工具(file-dispatcher.sh)见http://blog.csdn.net/rzhzhz/article/details/7056761
脚本如下:
#!/usr/bin/env bash
#此工具为Hadoop配置文件(conf目录下)分发工具
#把本地(namenode)配置文件分发到指定机器(datanode)
#前提是指定机器(datanode)的目录结构必须与本地(namenode)目录一致
# Environment Variables
#
# HADOOP_SLAVES File naming remote hosts.
# Default is ${HADOOP_CONF_DIR}/slaves.
# HADOOP_CONF_DIR Alternate conf dir. Default is ${HADOOP_HOME}/conf.
# HADOOP_SLAVE_SLEEP Seconds to sleep between spawning remote commands.
# HADOOP_SSH_OPTS Options passed to ssh when running remote commands.
# FILE_PATH 分发文件列表文件 默认为 ${HADOOP_CONF_DIR}/configs,配置的文件名必须为${HADOOP_CONF_DIR}目录下的文件名
#
##
usage="Usage: config-dispatcher.sh [--files <filelist>]"
bin=`dirname "$0"`
bin=`cd "$bin"; pwd`
. "$bin"/hadoop-config.sh
# get arguments
FILE_PATH=""
if [ $# -eq 2 ] && [ $1 = "--files" ] ;then
echo $2
FILE_PATH=$2;
elif [ $# -eq 0 ] ;then
FILE_PATH="${HADOOP_CONF_DIR}/configs";
else
echo $usage
exit 1;
fi
# If the slaves file is specified in the command line,
# then it takes precedence over the definition in
# hadoop-env.sh. Save it here.
HOSTLIST=$HADOOP_SLAVES
if [ -f "${HADOOP_CONF_DIR}/hadoop-env.sh" ]; then
. "${HADOOP_CONF_DIR}/hadoop-env.sh"
fi
echo "加载待分发的机器列表..."
if [ "$HOSTLIST" = "" ]; then
if [ "$HADOOP_SLAVES" = "" ]; then
export HOSTLIST="${HADOOP_CONF_DIR}/slaves"
else
export HOSTLIST="${HADOOP_SLAVES}"
fi
fi
if [ "$HOSTLIST" = "" ];then
echo "请配置待分发的机器..."
exit 0;
else
echo "待分发的机器如下:"
for hostname in `cat "$HOSTLIST"|sed "s/#.*$//;/^$/d"`;
do
echo $hostname
done
fi
echo "从$FILE_PATH文件加载待分发的配置文件名..."
export CONFIGLIST="$FILE_PATH"
echo "待分发配置文件如下:"
for configname in `cat "$CONFIGLIST"|sed "s/#.*$//;/^$/d"`;
do
echo $configname
done
#开始分发
for configname in `cat "$CONFIGLIST"|sed "s/#.*$//;/^$/d"`;
do
for hostname in `cat "$HOSTLIST"|sed "s/#.*$//;/^$/d"`;
do
echo "分发 ${HADOOP_CONF_DIR}/$configname 至 $hostname:${HADOOP_CONF_DIR} "
scp ${HADOOP_CONF_DIR}/$configname $hostname:${HADOOP_CONF_DIR}
done
done
wait
大家可拷贝文件内容至config-dispatcher.sh (自己新建)文件并置于hadoop/bin目录下,在指定的文件下(默认为hadoop/conf/configs)配置要分发的配置文件名,然后就可以直接像启动hadoop集群一样启用此工具了,具体用法可参考代码或hadoop/bin/config-dispatcher.sh --h获得帮助