mysql
首先把mysql的驱动包放在hive的lib下
创建hive-site.xml
[root@hadoop102 conf]# vim hive-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- jdbc 连接的 URL -->
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://ip:3306/metastore?useUnicode=true&rewriteBatchedStatements=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai</value>
</property>
<!-- jdbc 连接的 Driver-->
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<!-- jdbc 连接的 username-->
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
</property>
<!-- jdbc 连接的 password -->
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>root</value>
</property>
<!-- Hive 元数据存储版本的验证 -->
<property>
<name>hive.metastore.schema.verification</name>
<value>false</value>
</property>
<!--元数据存储授权-->
<property>
<name>hive.metastore.event.db.notification.api.auth</name>
<value>false</value>
</property>
<!-- Hive 默认在 HDFS 的工作目录 -->
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
</property>
</configuration>
###########初始化mysql
[root@hadoop102 hive]# schematool -initSchema -dbType mysql -verbose
[root@hadoop102 hive]# bin/hive
which: no hbase in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/module/jdk1.8.0_251/bin:/opt/module/hadoop-3.1.3/bin:/opt/module/hadoop-3.1.3/sbin:/opt/module/hive/bin:/root/bin)
Hive Session ID = 5f10de66-60b6-485f-a03a-bb0177786696
Logging initialized using configuration in jar:file:/opt/module/hive/lib/hive-common-3.1.2.jar!/hive-log4j2.properties Async: true
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
Hive Session ID = 749ff390-eaec-4109-97a8-0f4fc60714ea
#################################查看数据库
hive> show databases;
OK
default
Time taken: 0.241 seconds, Fetched: 1 row(s)
hive> show tables;
OK
Time taken: 0.091 seconds
#######################################创建表
hive> create table test (id string);
OK
Time taken: 0.52 seconds
#######################################查询
hive> select * from test;
OK
1001
Time taken: 1.407 seconds, Fetched: 1 row(s)
hive>
为了可以多个客户端连接hive,所以配置mysql
客户端连接hive
修改hive-site.xml
<!-- Hive 指定存储元数据要连接的地址 -->
<property>
<name>hive.metastore.uris</name>
<value>thrift://hadoop102:9083</value>
</property>
############################启动
[root@hadoop102 hive]# bin/hive --service metastore
############################在另一个窗口使用进入hive服务
[root@hadoop102 hive]# bin/hive
使用jdbc方式连接hive
修改hive-site.xml
<!-- 指定hiveserver2连接的host -->
<property>
<name>hive.server2.thrift.bind.host</name>
<value>hadoop102</value>
</property>
<!-- 指定hiveserver2连接的端口号 -->
<property>
<name>hive.server2.thrift.port</name>
<value>10000</value>
</property>
[root@hadoop102 hive]# bin/hive --service metastore
2022-10-27 11:20:59: Starting Hive Metastore Server
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
在另一个窗口
[root@hadoop102 hive]# bin/hive --service hiveserver2
再打开一个窗口
[root@hadoop102 hive]# bin/beeline -u jdbc:hive2://hadoop102:10000 -n root
Connecting to jdbc:hive2://hadoop102:10000
22/10/27 11:31:27 [main]: WARN jdbc.HiveConnection: Failed to connect to hadoop102:10000
Error: Could not open client transport with JDBC Uri: jdbc:hive2://hadoop102:10000: Failed to open new session: java.lang.RuntimeException: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.authorize.AuthorizationException): User: root is not allowed to impersonate root (state=08S01,code=0)
Beeline version 3.1.2 by Apache Hive
报错了
停止hadoop集群,修改core-site.xml
[root@hadoop102 hive]# myhadoop.sh stop
[root@hadoop102 hive]# vim /opt/module/hadoop-3.1.3/etc/hadoop/core-site.xml
<!-- jdbc连接hive -->
<property>
<name>hadoop.proxyuser.xxx.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.xxx.groups</name>
<value>*</value>
</property>
其中xxx就是你自己的用户名,在启动的时候需要使用
再次启动
[root@hadoop102 hive]# bin/hive --service metastore
2022-10-27 11:46:46: Starting Hive Metastore Server
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
###############################打开第二个窗口
[root@hadoop102 hive]# bin/hive --service hiveserver2
which: no hbase in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/module/jdk1.8.0_251/bin:/opt/module/hadoop-3.1.3/bin:/opt/module/hadoop-3.1.3/sbin:/opt/module/hive/bin:/root/bin)
2022-10-27 11:47:01: Starting HiveServer2
Hive Session ID = 417c1ab9-4579-4a1f-ad1a-1569cdc216d1
Hive Session ID = 0ca0db98-4643-40b5-ad34-5b55f5fafe6d
Hive Session ID = 8392223b-c68a-45b1-b177-f74ce1a46e31
Hive Session ID = 6f98205f-b9c0-4720-bca2-5c190d4a59ee
#####################################打开第三个窗口
[root@hadoop102 hive]# bin/beeline -u jdbc:hive2://hadoop102:10000 -n root
Connecting to jdbc:hive2://hadoop102:10000
22/10/27 11:47:56 [main]: WARN jdbc.HiveConnection: Failed to connect to hadoop102:10000
Could not open connection to the HS2 server. Please check the server URI and if the URI is correct, then ask the administrator to check the server status.
Error: Could not open client transport with JDBC Uri: jdbc:hive2://hadoop102:10000: java.net.ConnectException: 拒绝连接 (Connection refused) (state=08S01,code=0)
Beeline version 3.1.2 by Apache Hive
[root@hadoop102 hive]# bin/beeline -u jdbc:hive2://hadoop102:10000 -n root
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>
后台启动
我们发现上面的启动不能关闭终端,而我们肯定是要在能关闭终端下启动的
[root@hadoop102 hive]# nohup bin/hive --service metastore 2>&1 &
[1] 17600
[root@hadoop102 hive]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@hadoop102 hive]# nohup bin/hive --service hiveserver2 2>&1 &
[1] 17600
[root@hadoop102 hive]# bin/beeline -u jdbc:hive2://hadoop102:10000 -n root
使用自定义sheel脚本
[root@hadoop102 bin]# cd /usr/bin/
[root@hadoop102 bin]# vim myhiveservice.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&"
[ -z "$metapid" ] && eval $cmd || echo "Metastroe 服务已启动"
server2pid=$(check_process HiveServer2 10000)
cmd="nohup 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
####################启动
[root@hadoop102 bin]# chmod 777 myhiveservice.sh
[root@hadoop102 bin]# myhiveservice.sh start
######################因为启动比较慢,所以可以查看状态看看
[root@hadoop102 bin]# myhiveservice.sh status
Metastore 服务运行正常
HiveServer2 服务运行正常
说明启动成功
hive-site.xml
添加表头
<property>
<name>hive.cli.print.header</name>
<value>true</value>
</property>
<property>
<name>hive.cli.print.current.db</name>
<value>true</value>
</property>