#!bin/bash#1、压缩包(存在)#参数检查:检查用户是否提供了压缩包路径作为第一个参数,并且该压缩包是否符合 Hadoop 支持的压缩格式(tar.gz 或 tgz)。pack=$1if[[!"$pack"=~ ^.*hadoop.*\.(tar\.gz|tgz)$ ]];thenecho"ERROR : ONLY SUPPORT tar.gz OR tgz HADOOP COMPRESS FROMAT"exit1fi#2.1、检查 hadoop 服务,若存在则关闭sc=$(jps|awk'BEGIN{c=0}/DataNode|SecondaryNameNode|NodeManager|ResourceManager|NameNode/{c++}END{print c}')if[$sc-gt0];then
stop-all.sh 1>/dev/null 2>hadoop_err.log || jps|awk'/DataNode|SecondaryNameNode|NodeManager|ResourceManager|NameNode/{print $1}|xargs kill -9'if[$?-ne0];thenecho"ERROR : FAIL TO STOP THE RUNNING HADOOP SERVICES"exit1elseecho"INFO : SUCCESS TO STOP THE OLD RUNNING HADOOP SERVICES"fifi#2.2、目标目录(不存在/opt/software则创建,存在子目录则删除)dest=${2%/}old=$(ls $dest|grep ^hadoop)if[$?-eq0];thenrm-rf$dest/$oldecho"INFO : OLD HADOOP EDITION FOUND BUT REMOVED"fiif[!-e$dest];thenmkdir-p$destecho"INFO : DEST DIR NOT EXIST BUT CREATED"fi#3.解压:将Hadoop压缩包解压到目标目录tar-zxf$pack-C$destif[$?-eq0];thenecho-n"INFO : SUCCESS"elseecho-n"ERROR : FAIL"exit3fiecho"TO DECOMPRESS $pack TO $dest"#4.环境变量(存在则删除,再追加新环境变量)env=/etc/profile.d/myenv.sh
if[!-e$env];thentouch$envecho"INFO : ENV FILE NOT EXIST BUT TOUCHED"fiold=$(cat $env|awk 'BEGIN{b=0;e=0;ln=0;}{ln++; if(b>0&& match($0,/^#.*/))e=ln-1; if(match($0,/^#.*hadoop/)) b=ln}END{if(b>0 && e==0) e=ln; print b","e}')if["$old"!="0,0"];thensed-i"${old}d"$envecho"INFO : ENV VARIABLUES FOR HADOOP FOUND BUT REMOVED LINE $old IN $env"fiold=$(ls $dest|grep ^hadoop)dest="$dest/$old"cat>>$env<<EOF
#hadoop 3.1.3
export HADOOP_HOME=$dest
export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$HADOOP_HOME/lib
export HDFS_NAMENODE_USER=root
export HDFS_DATANODE_USER=root
export HDFS_SECONDARYNAMENODE_USER=root
export HDFS_JOURNALNODE_USER=root
export HDFS_ZKFC_USER=root
export YARN_RESOURCEMANAGER_USER=root
export YARN_NODEMANAGER_USER=root
export HADOOP_MAPRED_HOME=$HADOOP_HOME
export HADOOP_COMMON_HOME=$HADOOP_HOME
export HADOOP_HDFS_HOME=$HADOOP_HOME
export HADOOP_YARN_HOME=$HADOOP_HOME
export HADOOP_INSTALL=$HADOOP_HOME
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
export HADOOP_LIBEXEC_DIR=$HADOOP_HOME/libexec
export JAVA_LIBRARY_PATH=$HADOOP_HOME/lib/native:$JAVA_LIBRARY_PATH
export HADOOP_CONF_DIR=$HADOOP_HOME/etc/hadoop
EOFecho"INFO : HADOOP ENV VARIABLUES APPENDED TO $env"#5.激活环境变量source /etc/profile
if[$?-eq0];thenecho"INFO : SUCCESS TO ACTIVATE ENV VARIABLUES FOR HADOOP"elseecho" ERROR : FAIL TO ACTIVATE ENV VARIABLUES FOR HADOOP"exit4fi#6.记录脚本目录,切换至hadoop主配置目录sdir=$(cd$(dirname $0);pwd)cd$dest/etc/hadoop
echo"INFO : CURRENT DIRECTORY CHANGED TO $PWD"#7.hadoop内部java环境变量配置sed-i"s/# export JAVA_HOME=/export JAVA_HOME=${JAVA_HOME//\//\\/}/" hadoop-env.sh
echo"INFO : SUCCESS TO FINISH hadoop-env.sh CONFIG"#8.0 检查并完善/etc/hosts下主机名与IP地址映射cat /etc/hosts|grep$HOSTNAME1>/dev/null 2>&1echo-n"INFO : IP & HOSTNAME MAP"if[$?-ne0];thenip=$(ip addr|grep-E inet.*ens33|awk'{print $2}'|cut -d/ -f1)echo"$ip$HOSTNAME">>/etc/hosts
echo"NOT FOUND BUT CREATED"elseecho"EXIST"fi#8.1 检查 hadoop 临时目录,存在则删除dir=/root/job/hadoop
if[-e$dir];thenrm-rf$direcho"INFO : TEMP & YARN LOCAL & YARN LOG DIR ($dir) FOUND AND REMOVED"fi
core-site.xml:配置 Hadoop 的核心配置,如文件系统的默认设置和临时目录
#8.hadoop内部 core-site.xml 配置cat> core-site.xml <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. See accompanying LICENSE file.
-->
<!-- Put site-specific property overrides in this file. -->
<configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://single01:9000</value>
</property>
<property>
<name>hadoop.tmp.dir</name>
<value>/tmp/hadoop</value>
</property>
<property>
<name>hadoop.http.staticuser.user</name>
<value>root</value>
</property>
<property>
<name>hadoop.proxyuser.root.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.root.groups</name>
<value>*</value>
</property>
<property>
<name>io.file.buffer.size</name>
<value>1048576</value>
</property>
</configuration>
EOFecho"INFO : SUCCESS TO FINISH core-site.xml CONFIG"
#9.hadoop内部 hdfs-site.xml 配置cat> hdfs-site.xml <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. See accompanying LICENSE file.
-->
<!-- Put site-specific property overrides in this file. -->
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
<property>
<name>dfs.blocksize</name>
<value>128M</value>
</property>
<property>
<name>dfs.namenode.name.dir</name>
<value>/opt/software/hadoop-3.1.3/data/dfs/name</value>
</property>
<property>
<name>dfs.datanode.data.dir</name>
<value>/opt/software/hadoop-3.1.3/data/dfs/data</value>
</property>
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>single01:9869</value>
</property>
<property>
<name>dfs.permissions.enabled</name>
<value>false</value>
</property>
</configuration>
EOFecho"INFO : SUCCESS TO FINISH hdfs-site.xml CONFIG"
#10.2.配置mapred-site.xmlcat> mapred-site.xml <<EOF
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. See accompanying LICENSE file.
-->
<!-- Put site-specific property overrides in this file. -->
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
<property>
<name>mapreduce.application.classpath</name>
<value>/opt/software/hadoop-3.1.3/etc/hadoop:/opt/software/hadoop-3.1.3/share/hadoop/common/lib/*:/opt/software/hadoop-3.1.3/share/hadoop/common/*:/opt/software/hadoop-3.1.3/share/hadoop/hdfs:/opt/software/hadoop-3.1.3/share/hadoop/hdfs/lib/*:/opt/software/hadoop-3.1.3/share/hadoop/hdfs/*:/opt/software/hadoop-3.1.3/share/hadoop/mapreduce/lib/*:/opt/software/hadoop-3.1.3/share/hadoop/mapreduce/*:/opt/software/hadoop-3.1.3/share/hadoop/yarn:/opt/software/hadoop-3.1.3/share/hadoop/yarn/lib/*:/opt/software/hadoop-3.1.3/share/hadoop/yarn/*</value>
</property>
<property>
<name>mapreduce.jobhistory.address</name>
<value>single01:10020</value>
</property>
<property>
<name>mapreduce.jobhistory.webapp.address</name>
<value>single01:19888</value>
</property>
<property>
<name>mapreduce.map.memory.mb</name>
<value>256</value>
</property>
<property>
<name>mapreduce.reduce.memory.mb</name>
<value>512</value>
</property>
</configuration>
EOFecho"INFO : SUCCESS TO FINISH mapred-site.xml CONFIG"
#11.hadoop内部yarn-site.xml配置cat> yarn-site.xml <<EOF
<?xml version="1.0"?>
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. See accompanying LICENSE file.
-->
<configuration>
<!-- Site specific YARN configuration properties -->
<property>
<name>yarn.resourcemanager.connect.retry-interval.ms</name>
<value>10000</value>
</property>
<property>
<name>yarn.resourcemanager.scheduler.class</name>
<value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairScheduler</value>
</property>
<property>
<name>yarn.nodemanager.localizer.address</name>
<value>single01:8040</value>
</property>
<property>
<name>yarn.nodemanager.address</name>
<value>single01:8050</value>
</property>
<property>
<name>yarn.nodemanager.webapp.address</name>
<value>single01:8042</value>
</property>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<property>
<name>yarn.nodemanager.local-dirs</name>
<value>/tmp/hadoop/yarn/local</value>
</property>
<property>
<name>yarn.nodemanager.log-dirs</name>
<value>/tmp/hadoop/yarn/log</value>
</property>
<property>
<name>yarn.nodemanager.vmem-check-enabled</name>
<value>false</value>
</property>
<property>
<name>yarn.application.classpath</name>
<value>/opt/software/hadoop-3.1.3/etc/hadoop:/opt/software/hadoop-3.1.3/share/hadoop/common/lib/*:/opt/software/hadoop-3.1.3/share/hadoop/common/*:/opt/software/hadoop-3.1.3/share/hadoop/hdfs:/opt/software/hadoop-3.1.3/share/hadoop/hdfs/lib/*:/opt/software/hadoop-3.1.3/share/hadoop/hdfs/*:/opt/software/hadoop-3.1.3/share/hadoop/mapreduce/lib/*:/opt/software/hadoop-3.1.3/share/hadoop/mapreduce/*:/opt/software/hadoop-3.1.3/share/hadoop/yarn:/opt/software/hadoop-3.1.3/share/hadoop/yarn/lib/*:/opt/software/hadoop-3.1.3/share/hadoop/yarn/*</value>
</property>
<property>
<name>yarn.nodemanager.env-whitelist</name>
<value>JAVA_HOME,HADOOP_COMMON_HOME,HADOOP_HDFS_HOME,HADOOP_CONF_DIR,CLASSPATH_PREPEND_DISTCACHE,HADOOP_YARN_HOME,HADOOP_MAPRED_HOME</value>
</property>
</configuration>
EOFecho"INFO : SUCCESS TO FINISH yarn-site.xml CONFIG"
#12.0 切换至脚本路径cd$sdir#12.格式化 namenode
hdfs namenode -format1>/dev/null 2>>hadoop_err.log
if[$?-ne0];thenecho"ERROR : FAIL TO format hdfs namenode"exit1elseecho"INFO : SUCCESS TO format hdfs namenode"fi#13.启动 hadoop 服务
start-all.sh 1>/dev/null 2>>hadoop_err.log
if[$?-ne0];thenecho"ERROR : FAIL TO START HADOOP SERVICE"exit1elsesc=$(jps|awk'BEGIN{c=0}/DataNode|SecondaryNameNode|NodeManager|ResourceManager|NameNode/{c++}END{print c}')if[$sc-eq5];thenecho"INFO : SUCCESS TO FAIL HADOOP SERVICE"elseecho"WARN : FAIL TO HADOOP SERVICE FOR NOT 5 SERVICES STARTED"fifi#14.在脚本结束前,取消设置所有局部环境变量unset hc
unsetdirunset sdir
unsetenvunset sc
unset old
unset dest
unset pack