看脚本需要注意的地方是用【" "】双引号括起来的字段一般是带有$某个东西的,例如$hostname,需要
取值的一般都用""不然会出错,如果不想取值,直接保留$hostname的时候,就用【''】单引号
#!/bin/bash
#设置触发条件
jdk=true
hadoop=true
zookeeper=true
hive=true
#定义主机名
hostname=`hostname`
#定义用户名
whoami=`whoami`
#定义一个安装目录,如果不存在就创建
installdir=/opt/soft
if [ ! -d "$installdir" ]; then
mkdir $installdir
fi
#安装jdk
if [ "$jdk=true" = true ]; then
echo "--安装java jdk--"
#解压缩到soft下
tar -zxf /opt/install/jdk-8u111-linux-x64.tar.gz -C /opt/soft/
#将解压缩的文件重命名,这一步可省略,看个人情况
mv /opt/soft/jdk1.8.0_111 /opt/soft/jdk180
#编写配置文件
echo "#jdk" >> /etc/profile
echo 'export JAVA_HOME=/opt/soft/jdk180' >> /etc/profile
echo 'export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' >>
/etc/profile
echo 'export PATH=$PATH:$JAVA_HOME/bin' >> /etc/profile
fi
#安装hadoop
if [ "$hadoop" = true ];then
echo "--安装hadoop--"
#解压缩
tar -zxf /opt/install/hadoop-2.6.0-cdh5.14.2.tar.gz -C /opt/soft/
#重命名
mv /opt/soft/hadoop-2.6.0-cdh5.14.2 /opt/soft/hadoop260
echo '--------修改配置hadoop-env.sh-------------'
#将以export JAVA_HOME=开头的替换为export JAVA_HOME=$JAVA_HOME/
sed -i "/^export JAVA_HOME=/cexport JAVA_HOME=$JAVA_HOME/"
/opt/soft/hadoop260/etc/hadoop/hadoop-env.sh
echo '--------修改配置mapred-env.sh-------------'
sed -i "/^# export JAVA_HOME=/cexport JAVA_HOME=$JAVA_HOME/"
/opt/soft/hadoop260/etc/hadoop/mapred-env.sh
echo '--------修改配置yarn-env.sh-------------'
sed -i "/^# export JAVA_HOME=/cexport JAVA_HOME=$JAVA_HOME/"
/opt/soft/hadoop260/etc/hadoop/yarn-env.sh
echo '--------修改配置core-site.xml-------------'
#定义core-site.xml的位置,方便以后书写
core_path="/opt/soft/hadoop260/etc/hadoop/core-site.xml"
sed -i '19a\<property><name>hadoop.proxyuser.root.group</name><value>*</value>
</property>' $core_path
sed -i '19a\<property><name>hadoop.proxyuser.root.hosts</name><value>*</value>
</property>' $core_path
sed -i '19a\<property><name>hadoop.tmp.dir</name>
<value>/opt/soft/hadoop260/hadooptmp</value></property>' $core_path
sed -i "19a\<property><name>fs.defaultFS</name><value>hdfs://$hostname:9000</value>
</property>" $core_path
echo '--------修改配置hdfs-site.xml-------------'
hdfs_path="/opt/soft/hadoop260/etc/hadoop/hdfs-site.xml"
sed -i "19a\<property><name>dfs.namenode.secondary.http-address</name>
<value>$hostname:50090</value></property>" $hdfs_path
sed -i '19a\<property><name>dfs.replication</name><value>3</value></property>'
$hdfs_path
echo '--------修改配置mapred-site.xml-------------'
mapred_path="/opt/soft/hadoop260/etc/hadoop/mapred-site.xml"
#复制一份mapred-site.xml.template并重命名为mapred-site.xml
cp /opt/soft/hadoop260/etc/hadoop/mapred-site.xml.template
/opt/soft/hadoop260/etc/hadoop/mapred-site.xml
sed -i "19a\<property><name>mapreduce.jobhistory.webapp.address</name>
<value>$hostname:19888</value></property>" $mapred_path
sed -i "19a\<property><name>mapreduce.jobhistory.address</name>
<value>$hostname:10020</value></property>" $mapred_path
sed -i '19a\<property><name>mapreduce.framework.name</name><value>yarn</value>
</property>' $mapred_path
echo '--------修改配置yarn-site.xml-------------'
yarn_path="/opt/soft/hadoop260/etc/hadoop/yarn-site.xml"
sed -i "15a\<property><name>yarn.log-aggregation.retain-seconds</name>
<value>604800</value></property>" $yarn_path
sed -i "15a\<property><name>yarn.log.aggregation.enble</name><value>true</value>
</property>" $yarn_path
sed -i "15a\<property><name>yarn.resourcemanager.hostname</name>
<value>$hostname</value></property>" $yarn_path
sed -i '15a\<property><name>yarn.nodemanager.aux-
services.mapreduce.shuffle.class</name>
<value>org.apache.hadoop.mapred.ShuffleHandler</value></property>' $yarn_path
echo '--------修改配置slaves.xml-------------'
sed -i "s/localhost/$hostname/g" /opt/soft/hadoop260/etc/hadoop/slaves
#修改配置文件
echo "#hadoop" >> /etc/profile
echo 'export HADOOP_HOME=/opt/soft/hadoop260' >> /etc/profile
echo 'export HADOOP_COMMON_HOME=$HADOOP_HOME' >> /etc/profile
echo 'export HADOOP_HDFS_HOME=$HADOOP_HOME' >> /etc/profile
echo 'export YARN_HOME=$HADOOP_HOME' >> /etc/profile
echo 'export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOMEb/native' >> /etc/profile
echo 'export HADOOP_OPTS="-Djava.library.path=$HADOOP_HOMEb"' >> /etc/profile
echo 'export PATH=$PATH:$HADOOP_HOME/sbin:$HADOOP_HOME/bin' >> /etc/profile
#初始化namenode
/opt/soft/hadoop260/bin/hadoop namenode -format
fi
if [ "$zookeeper" = true ];then
echo " ------------安装zookeeper----------------- "
tar -zxf /opt/install/zookeeper-3.4.5-cdh5.14.2.tar.gz -C /opt/soft/
mv /opt/soft/zookeeper-3.4.5-cdh5.14.2 /opt/soft/zookeeper345
cp /opt/soft/zookeeper345/conf/zoo_sample.cfg /opt/soft/zookeeper345/conf/zoo.cfg
sed -i '/^dataDir=/cdataDir=/opt/soft/zookeeper345/datatmp'
/opt/soft/zookeeper345/conf/zoo.cfg
echo "server.1=$hostname:2888:3888" >> /opt/soft/zookeeper345/conf/zoo.cfg
mkdir -p /opt/soft/zookeeper345/datatmp
echo "1" > /opt/soft/zookeeper345/datatmp/myid
echo 'export ZOOKEEPER_HOME=/opt/soft/zookeeper345' >> /etc/profile
echo 'export PATH=$PATH:$ZOOKEEPER_HOME/bin' >> /etc/profile
fi
if ["$hive" = true ];then
echo '--------安装hive----------'
tar -zxf /opt/install/hive-1.1.0-cdh5.14.2.tar.gz -C /opt/soft/
mv /opt/soft/hive-1.1.0-cdh5.14.2 /opt/soft/hive110
touch /opt/soft/hive110/conf/hive-site.xml
echo '--------配置mysql驱动-------------------'
cp /opt/install/mysql-connector-java-5.1.25.jar /opt/soft/hive110/lib/
echo "-----------修改配置hive-site.xml---------------"
hive_path="/opt/soft/hive110/conf/hive-site.xml"
echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' >> $hive_path
echo '<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>' >> $hive_path
sed -i '2a\</configuration>' $hive_path
sed -i '2a\<property><name>hive.server2.thrift.client.password</name>
<value>你自己的数据库密码</value></property>' $hive_path
sed -i "2a\<property><name>hive.server2.thrift.client.user</name><value>你自己的用户名
</value></property>" $hive_path
sed -i '2a\<property><name>javax.jdo.option.ConnectionPassword</name>
<value>你自己的数据库密码</value></property>' $hive_path
sed -i "2a\<property><name>javax.jdo.option.ConnectionUserName</name>
<value>你自己的用户名</value></property>" $hive_path
sed -i '2a\<property><name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value></property>' $hive_path
sed -i '2a\<property><name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://你自己的ip地址:3306/hive?
createDatabaseIfNotExist=true</value></property>' $hive_path
sed -i '2a\<configuration>' $hive_path
echo '#hive' >> /etc/profile
echo 'export HIVE_HOME=/opt/soft/hive110' >> /etc/profile
echo 'export PATH=$PATH:$HIVE_HOME/bin' >> /etc/profile
source /etc/profile
echo '------------hive初始化----------------'
schematool -dbType mysql -initSchema
fi