一、hive安装地址 http://hive.apache.org/ 文档查看地址:https://cwiki.apache.org/confluence/display/Hive/GettingStarted 下载地址:http://archive.apache.org/dist/hive/ 二、mysql安装 1、安装包准备 1)卸载自带的Mysql-libs(如果之前安装过mysql,要全都卸载掉) [atguigu@hadoop101 software]$ rpm -qa | grep -i -E mysql\|mariadb | xargs -n1 sudo rpm -e --nodeps 2)将安装包和JDBC驱动上传到/opt/software,共计6个 01_mysql-community-common-5.7.29-1.el7.x86_64.rpm 02_mysql-community-libs-5.7.29-1.el7.x86_64.rpm 03_mysql-community-libs-compat-5.7.29-1.el7.x86_64.rpm 04_mysql-community-client-5.7.29-1.el7.x86_64.rpm 05_mysql-community-server-5.7.29-1.el7.x86_64.rpm mysql-connector-java-5.1.48.jar 2、安装MySql 1)安装mysql依赖 [atguigu@hadoop101 software]$ sudo rpm -ivh 01_mysql-community-common-5.7.29-1.el7.x86_64.rpm [atguigu@hadoop101 software]$ sudo rpm -ivh 02_mysql-community-libs-5.7.29-1.el7.x86_64.rpm [atguigu@hadoop101 software]$ sudo rpm -ivh 03_mysql-community-libs-compat-5.7.29-1.el7.x86_64.rpm 2)安装mysql-client [atguigu@hadoop101 software]$ sudo rpm -ivh 04_mysql-community-client-5.7.29-1.el7.x86_64.rpm 3)安装mysql-server [atguigu@hadoop101 software]$ sudo rpm -ivh 05_mysql-community-server-5.7.29-1.el7.x86_64.rpm 4)启动mysql [atguigu@hadoop101 software]$ sudo systemctl start mysqld 5)查看mysql密码 [atguigu@hadoop101 software]$ sudo cat /var/log/mysqld.log | grep password ----并记录下该密码 3、配置MySql 配置只要是root用户+密码,在任何主机上都能登录MySQL数据库。 1)用刚刚查到的密码进入mysql(如果报错,给密码加单引号) [atguigu@hadoop101 software]$ mysql -uroot -p’password’ 2)设置复杂密码(由于mysql密码策略,此密码必须足够复杂) mysql> set password=password(“Qs23=zs32”); 3)更改mysql密码策略 mysql> set global validate_password_length=4; mysql> set global validate_password_policy=0; 4)设置简单好记的密码 mysql> set password=password(“000000”); —实际环境自便 5)进入msyql库 mysql> use mysql 6)查询user表 mysql> select user, host from user; 7)修改user表,把Host表内容修改为% mysql> update user set host="%" where user=“root”; 8)刷新 mysql> flush privileges; 9)退出 mysql> quit;
三、hive安装部署 1)把apache-hive-3.1.2-bin.tar.gz上传到linux的/opt/software目录下 2)解压apache-hive-3.1.2-bin.tar.gz到/opt/module/目录下面 [atguigu@hadoop101 software]$ tar -zxvf /opt/software/apache-hive-3.1.2-bin.tar.gz -C /opt/module/ 3)修改apache-hive-3.1.2-bin.tar.gz的名称为hive [atguigu@hadoop102 software]$ mv /opt/module/apache-hive-3.1.2-bin/ /opt/module/hive 4)修改/etc/profile.d/my_env.sh,添加环境变量 [atguigu@hadoop101 software]$ sudo vim /etc/profile.d/my_env.sh 5)添加内容
#HIVE_HOME
export HIVE_HOME=/opt/module/hive
export PATH=$PATH:$HIVE_HOME/bin
重启Xshell对话框使环境变量生效 6)解决日志Jar包冲突 [atguigu@hadoop101 software]$ mv $HIVE_HOME/lib/log4j-slf4j-impl-2.10.0.jar $HIVE_HOME/lib/log4j-slf4j-impl-2.10.0.bak
四、Hive元数据配置到MySql 1、拷贝驱动 将MySQL的JDBC驱动拷贝到Hive的lib目录下 [atguigu@hadoop101 software]$ cp /opt/software/mysql-connector-java-5.1.48.jar $HIVE_HOME/lib 2、配置Metastore到MySql 在HIVE_HOME/conf目录下新建hive-site.xml文件 [atguigu@hadoop101 software]$ vim $HIVE_HOME/conf/hive-site.xml 添加如下内容
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://hadoop101:3306/metastore?useSSL=false</value>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>000000</value>
</property>
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
</property>
<property>
<name>hive.metastore.schema.verification</name>
<value>false</value>
</property>
<property>
<name>hive.metastore.uris</name>
<value>thrift://hadoop101:9083</value>
</property>
<property>
<name>hive.server2.thrift.port</name>
<value>10000</value>
</property>
<property>
<name>hive.server2.thrift.bind.host</name>
<value>hadoop101</value>
</property>
<property>
<name>hive.metastore.event.db.notification.api.auth</name>
<value>false</value>
</property>
</configuration>
五、安装Tez引擎 1)将tez安装包拷贝到集群,并解压tar包 [atguigu@hadoop101 software]$ mkdir /opt/module/tez [atguigu@hadoop101 software]$ tar -zxvf /opt/software/tez-0.10.1-SNAPSHOT-minimal.tar.gz -C /opt/module/tez 2)上传tez依赖到HDFS [atguigu@hadoop101 software]$ hadoop fs -mkdir /tez [atguigu@hadoop101 software]$ hadoop fs -put /opt/software/tez-0.10.1-SNAPSHOT.tar.gz /tez 3)新建tez-site.xml [atguigu@hadoop101 software]$ vim $HADOOP_HOME/etc/hadoop/tez-site.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>tez.lib.uris</name>
<value>${fs.defaultFS}/tez/tez-0.10.1-SNAPSHOT.tar.gz</value>
</property>
<property>
<name>tez.use.cluster.hadoop-libs</name>
<value>true</value>
</property>
<property>
<name>tez.am.resource.memory.mb</name>
<value>1024</value>
</property>
<property>
<name>tez.am.resource.cpu.vcores</name>
<value>1</value>
</property>
<property>
<name>tez.container.max.java.heap.fraction</name>
<value>0.4</value>
</property>
<property>
<name>tez.task.resource.memory.mb</name>
<value>1024</value>
</property>
<property>
<name>tez.task.resource.cpu.vcores</name>
<value>1</value>
</property>
</configuration>
4)修改Hadoop环境变量 编辑hadoop-env.sh [atguigu@hadoop101 software]$ vim $HADOOP_HOME/etc/hadoop/shellprofile.d/tez.sh 添加Tez的Jar包相关信息
hadoop_add_profile tez
function _tez_hadoop_classpath
{
hadoop_add_classpath "$HADOOP_HOME/etc/hadoop" after
hadoop_add_classpath "/opt/module/tez/*" after
hadoop_add_classpath "/opt/module/tez/lib/*" after
}
5)修改Hive的计算引擎 [atguigu@hadoop101 software]$ vim $HIVE_HOME/conf/hive-site.xml
<property>
<name>hive.execution.engine</name>
<value>tez</value>
</property>
<property>
<name>hive.tez.container.size</name>
<value>1024</value>
</property>
小插曲: Hive运行日志信息配置 1)Hive的log默认存放在/tmp/atguigu/hive.log目录下(当前用户名下) 2)修改hive的log存放日志到/opt/module/hive/logs (1)修改/opt/module/hive/conf/hive-log4j.properties.template文件名称为 hive-log4j.properties [atguigu@hadoop102 conf]$ pwd /opt/module/hive/conf [atguigu@hadoop102 conf]$ mv hive-log4j2.properties.template hive-log4j2.properties (2)在hive-log4j.properties文件中修改log存放位置 hive.log.dir=/opt/module/hive/logs 6)解决日志Jar包冲突 [atguigu@hadoop101 software]$ rm /opt/module/tez/lib/slf4j-log4j12-1.7.10.jar
六、启动Hive 1、初始化元数据库 1)登陆MySQL [atguigu@hadoop101 software]$ mysql -uroot -p000000 2)新建Hive元数据库 mysql> create database metastore; mysql> quit; 3)初始化Hive元数据库 [atguigu@hadoop101 software]$ schematool -initSchema -dbType mysql -verbose
2、启动metastore和hiveserver2 1)Hive 2.x以上版本,要先启动这两个服务,否则会报错: FAILED: HiveException java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient 2)编写hive服务启动脚本 [atguigu@hadoop101 software]$ vim $HIVE_HOME/bin/hiveservices.sh
#!/bin/bash
HIVE_LOG_DIR=$HIVE_HOME/logs
mkdir -p $HIVE_LOG_DIR
function check_process()
{
pid=$(ps -ef 2>/dev/null | grep -v grep | grep -i $1 | awk '{print $2}')
ppid=$(netstat -nltp 2>/dev/null | grep $2 | awk '{print $7}' | cut -d '/' -f 1)
echo $pid
[[ "$pid" =~ "$ppid" ]] && [ "$ppid" ] && return 0 || return 1
}
function hive_start()
{
metapid=$(check_process HiveMetastore 9083)
cmd="nohup hive --service metastore >$HIVE_LOG_DIR/metastore.log 2>&1 &"
cmd=$cmd" sleep 4; hdfs dfsadmin -safemode wait >/dev/null 2>&1"
[ -z "$metapid" ] && eval $cmd || echo "Metastroe服务已启动"
server2pid=$(check_process HiveServer2 10000)
cmd="nohup hive --service hiveserver2 >$HIVE_LOG_DIR/hiveServer2.log 2>&1 &"
[ -z "$server2pid" ] && eval $cmd || echo "HiveServer2服务已启动"
}
function hive_stop()
{
metapid=$(check_process HiveMetastore 9083)
[ "$metapid" ] && kill $metapid || echo "Metastore服务未启动"
server2pid=$(check_process HiveServer2 10000)
[ "$server2pid" ] && kill $server2pid || echo "HiveServer2服务未启动"
}
case $1 in
"start")
hive_start
;;
"stop")
hive_stop
;;
"restart")
hive_stop
sleep 2
hive_start
;;
"status")
check_process HiveMetastore 9083 >/dev/null && echo "Metastore服务运行正常" || echo "Metastore服务运行异常"
check_process HiveServer2 10000 >/dev/null && echo "HiveServer2服务运行正常" || echo "HiveServer2服务运行异常"
;;
*)
echo Invalid Args!
echo 'Usage: '$(basename $0)' start|stop|restart|status'
;;
esac
3)添加执行权限 [atguigu@hadoop101 software]$ chmod +x $HIVE_HOME/bin/hiveservices.sh 4)启动Hive后台服务 [atguigu@hadoop101 software]$ hiveservices.sh start ( hiveservices.sh stop hiveservices.sh restart)
3、HiveJDBC访问 1)启动beeline客户端 [atguigu@hadoop101 software]$ beeline -u jdbc:hive2://hadoop102:10000 -n atguigu 2)看到如下界面 Connecting to jdbc:hive2://hadoop102:10000 Connected to: Apache Hive (version 3.1.2) Driver: Hive JDBC (version 3.1.2) Transaction isolation: TRANSACTION_REPEATABLE_READ Beeline version 3.1.2 by Apache Hive 0: jdbc:hive2://hadoop102:10000>就可以在这里敲sql了。。
4、Hive常用交互命令 [atguigu@hadoop101 hive]$ bin/hive -help 1)“-e”不进入hive的交互窗口执行sql语句 [atguigu@hadoop101 hive]$ bin/hive -e “select id from student;” 2)“-f”执行脚本中sql语句 (1)在/opt/module/datas目录下创建hivef.sql文件 [atguigu@hadoop101 datas]$ touch hivef.sql (2)文件中写入正确的sql语句 select *from student; (3)执行文件中的sql语句 [atguigu@hadoop101 hive]$ bin/hive -f /opt/module/datas/hivef.sql (4)执行文件中的sql语句并将结果写入文件中 [atguigu@hadoop101 hive]$ bin/hive -f /opt/module/datas/hivef.sql > /opt/module/datas/hive_result.txt
5、Hive其他命令操作 1)退出hive窗口: hive(default)>exit; hive(default)>quit; 在新版的hive中没区别了,在以前的版本是有的: exit:先隐性提交数据,再退出; quit:不提交数据,退出; 2)在hive cli命令窗口中如何查看hdfs文件系统 hive(default)>dfs -ls /; 3)查看在hive中输入的所有历史命令 (1)进入到当前用户的根目录/root或/home/atguigu (2)查看. hivehistory文件 [atguigu@hadoop101 ~]$ cat .hivehistory
|