6.1.1 登录 MySQL
[xiaoxq@hadoop105 lib]$ mysql -uroot -p密码
6.1.2 新建 Hive 元数据库
mysql> create database metastore;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| metastore |
+--------------------+
2 rows in set (0.01 sec)
mysql> quit
6.1.3 初始化 Hive 元数据库
[xiaoxq@hadoop105 lib]$ source /etc/profile
[xiaoxq@hadoop105 lib]$ schematool -initSchema -dbType mysql -verbose
注意:初始化前要source /etc/profile (前面改了配置文件,只要改了配置文件,最好都source一遍)
6.2.1 Hive 2.x以上版本,要先启动这两个服务否则会报错:
FAILED: HiveException java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
[xiaoxq@hadoop105 hive-3.1.2]$ hive --service metastore
2020-07-29 19:45:40: Starting Hive Metastore Server
注意: 启动后窗口不能再操作,需打开一个新的shell窗口做别的操作
启动 hiveserver2
[xiaoxq@hadoop105 software]$ hive --service hiveserver2
which: no hbase in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/module/jdk1.8.0_212/bin:/opt/ha/hadoop-3.1.3/bin:/opt/ha/hadoop-3.1.3/sbin:/opt/module/zookeeper-3.5.7/bin:/home/xiaoxq/.local/bin:/home/xiaoxq/bin:/opt/module/jdk1.8.0_212/bin:/opt/module/hadoop-3.1.3/bin:/opt/module/hadoop-3.1.3/sbin:/opt/module/zookeeper-3.5.7/bin:/opt/module/hive-3.1.2/bin)
2020-07-29 19:47:48: Starting HiveServer2
6.2.2 编写 hive 服务启动脚本
(1)前台启动的方式导致需要打开多个shell窗口,可以使用如下方式后台方式启动
[xiaoxq@hadoop105 ~]$ nohup hive --service metastore>log.txt 2>&1 &
[1] 11304
[xiaoxq@hadoop105 ~]$ nohup hive --service hiveserver2>log2.txt 2>&1 &
[2] 11454
(2)为了方便使用,可以直接编写脚本来管理服务的启动和关闭
[xiaoxq@hadoop105 ~]$ cd /opt/module/hive-3.1.2/bin/
[xiaoxq@hadoop105 bin]$ vim hiveservices.sh
#!/bin/bash
HIVE_LOG_DIR=$HIVE_HOME/logs
if [ ! -d $HIVE_LOG_DIR ]
then
mkdir -p $HIVE_LOG_DIR
fi
#检查进程是否运行正常,参数1为进程名,参数2为进程端口
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)添加执行权限
-rw-rw-r--. 1 xiaoxq xiaoxq 1719 7月 29 19:57 hiveservices.sh
[xiaoxq@hadoop105 bin]$ chmod +x hiveservices.sh
(4)启动 Hive 后台服务(需要先启动 Hadoop)
[xiaoxq@hadoop105 bin]$ hiveservices.sh start
6.3.1 启动 beeline客户端
[xiaoxq@hadoop105 hive-3.1.2]$ pwd
/opt/module/hive-3.1.2
[xiaoxq@hadoop105 hive-3.1.2]$ bin/beeline -u jdbc:hive2://hadoop105:10000 -n xiaoxq
6.3.1 显示如下界面
Connecting to jdbc:hive2://hadoop105: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://hadoop105:10000>
6.4 Hive 访问
6.4.1 启动 Hive 客户端
[xiaoxq@hadoop105 hive-3.1.2]$ hive
6.4.2 显示如下界面
which: no hbase in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/module/jdk1.8.0_212/bin:/opt/module/hadoop-3.1.3/bin:/opt/module/hadoop-3.1.3/sbin:/opt/module/zookeeper-3.5.7/bin:/opt/module/hive-3.1.2/bin:/home/xiaoxq/.local/bin:/home/xiaoxq/bin)
Hive Session ID = 9afe6495-6411-44bd-a39c-cbcab36ff41c
Logging initialized using configuration in jar:file:/opt/module/hive-3.1.2/lib/hive-common-3.1.2.jar!/hive-log4j2.properties Async: true
Hive Session ID = f5fafc28-d783-4be3-bec0-a098909b3611
hive>
6.4.3 打印 当前库 和 表头 的设置
[xiaoxq@hadoop105 hive-3.1.2]$ vim conf/hive-site.xml
<property>
<name>hive.cli.print.headername>
<value>truevalue>
<description>Whether to print the names of the columns in query output.description>
property>
<property>
<name>hive.cli.print.current.dbname>
<value>truevalue>
<description>Whether to include the current database in the Hive prompt.description>
property>