目录
1 初始化元数据库
1)登陆MySQL
2)新建Hive元数据库
3)初始化Hive元数据库
2 启动Hive
1)先启动hadoop集群
2)启动Hive
3)使用Hive
4)开启另一个窗口测试开启hive
3、 使用JDBC方式访问Hive
1)在hive-site.xml文件中添加如下配置信息
2)启动hiveserver2
3)启动beeline客户端(需要多等待一会)
4)看到如下界面(启动成功)
4、 编写启动metastore和hiveserver2脚本(了解)
1)Shell命令介绍
2)编写脚本【重点】
[atguigu@hadoop102 software]$ mysql -uroot -p000000
mysql> create database metastore;
mysql> quit;
[atguigu@hadoop102 software]$ schematool -initSchema -dbType mysql -verbose
[atguigu@hadoop102 hive]$ bin/hive
hive> show databases;
hive> show tables;
hive> create table test (id int);
hive> insert into test values(1);
hive> select * from test;
[atguigu@hadoop102 hive]$ bin/hive
hive.server2.thrift.bind.host
hadoop102
hive.server2.thrift.port
10000
[atguigu@hadoop102 hive]$ bin/hive --service hiveserver2
[atguigu@hadoop102 hive]$ bin/beeline -u jdbc:hive2://hadoop102:10000 -n atguigu
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>
nohup hive --service hiveserver2 &
nohup:不随窗口关闭而关闭进程,
&:在后台启动,不阻塞当前窗口
>:重定向到自主创建的log日志
nohup hive --service hiveserver2 >/opt/module/hive-3.1.2/logs/hiveServer2.log 2>&1 &
nohup hive --service metastore >/opt/module/hive-3.1.2/logs/metastore.log 2>&1 &
[atguigu@hadoop102 hive]$ vim $HIVE_HOME/bin/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)添加执行权限
[atguigu@hadoop102 hive]$ chmod u+x $HIVE_HOME/bin/hiveservices.sh
4)启动Hive后台服务
[atguigu@hadoop102 hive]$ hiveservices.sh start