使用元数据服务的方式访问 Hive & 使用 JDBC 方式访问 Hive

目录

1.使用元数据服务的方式访问 Hive

2.使用 JDBC 方式访问 Hive


首先一定要开启hadoop集群!!!如果报错连接拒绝,注意有没有开启

1.使用元数据服务的方式访问 Hive

1)在 /opt/module/hive/conf/hive-site.xml 文件中添加如下配置信息

[atguigu@hadoop102 software]$ vim $HIVE_HOME/conf/hive-site.xml

//添加的内容

<!-- 指定存储元数据要连接的地址 -->
<property>
<name>hive.metastore.uris</name>
<value>thrift://hadoop102:9083</value>
</property>

2)启动 metastore

[atguigu@hadoop202 hive]$ bin/hive --service metastore
2020-04-24 16:58:08: Starting Hive Metastore Server
注意 : 启动后窗口不能再操作,需打开一个新的 shell 窗口做别的操作

3)启动 hive

只有启动了metastore才能启动hive,如果直接启动hive会报错 

[atguigu@hadoop202 hive]$ bin/hive 

2.使用 JDBC 方式访问 Hive

1)在 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>

2)启动 metastore 

hiveserver2依赖于metastore

[atguigu@hadoop202 hive]$ bin/hive --service metastore
2020-04-24 16:58:08: Starting Hive Metastore Server
注意 : 启动后窗口不能再操作,需打开一个新的 shell 窗口做别的操作
3)启动 hiveserver2

[atguigu@hadoop102 hive]$ bin/hive --service hiveserver2 

一定要多等,可能会很久, 至少出现4个ID可能才开启hiveserver2,如下图:

 为了确保hiveserver2开启,再打开一个shell窗口,查看是否有端口号信息出现

[atguigu@Hadoop102 hive]$ netstat -anop | grep 10000
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp6       0      0 :::10000                :::*                    LISTEN      12616/java           off (0.00/0/0)
 

 4)启动 beeline 客户端

[atguigu@hadoop102 hive]$ bin/beeline -u jdbc:hive2://hadoop102:10000 -n atguigu
如果报错: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: atguigu is not allowed to impersonate atguigu (state=08S01,code=0)

解决方案: 报错:Error: Could not open client transport with JDBC Uri: jdbc:hive2://hadoop102:10000_不爱研究的研究僧的博客-CSDN博客

5)看到如下界面即成功 

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>

3.编写 hive 服务启动脚本

1)为了方便使用,可以直接编写脚本来管理服务的启动和关闭

[atguigu@hadoop102 hive]$ vim 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 &"
	[ -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

2)添加执行权限

[atguigu@hadoop102 hive]$ chmod 777 bin/hiveservices.sh

3)启动 Hive 后台服务 

[atguigu@hadoop102 hive]$ hiveservices.sh start

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值