Hive 安装 以及使用 DataGrip 连接 Hive
Hive 安装说明
① 环境说明
Hadoop 环境已通过腾讯云服务器完成搭建,我是将 hive 安装到 该 hadoop 集群的某个节点了
Mysql 中间件使用的是阿里云的产品
② 版本说明
Hadoop 2.7.5 +Hive 2.1.1 + Mysql 5.7 + Centos 7 + DataGrip 2021
强烈建议别用 Mysql 8 这个版本
Hive 安装前的准备
① 搭建 Hadoop 环境
Hadoop 介绍及集群搭建_半吊子Kyle的博客-CSDN博客
② 安装 Mysql
我使用的是 阿里云的 Mysql
,如果你需要自己搭建请参考以下文章:
Linux 安装 Mysql_半吊子Kyle的博客-CSDN博客
③ Hive 安装包下载,软件包为 apache-hive-2.1.0-bin.tar.gz
# 也可以通过该命令,直接下载到服务器,前提是你的服务器可以连接外网
wget http://archive.apache.org/dist/hive/hive-2.1.1/apache-hive-2.1.0-bin.tar.gz
④ 准备 mysql 驱动包
Maven Repository: mysql » mysql-connector-java » 8.0.18 (mvnrepository.com)
⑤ 此时假设你已经安装好了 DataGrip ,安装请参考该文章:
Macbook m1环境配置终极版_半吊子Kyle的博客-CSDN博客
Hive 的安装方式
Hive
的安装一共有三种方式 :内嵌模式、本地模式、远程模式,本博客的安装方式使用的是远程模式
① 内嵌模式
使用的是内嵌的 Derby
数据库来存储元数据,也不需要额外起 Metastore
服务。数据库和 Metastore
服务都嵌入在主 Hive Server
进程中。这个是默认的,配置简单,但是一次只能一个客户端连接,适用于用来实验,不适用于生产环境
缺点:不同路径启动 hive
,每一个 hive
拥有一套自己的元数据,无法共享
# 安装步骤
# 1.解压 hive 安装包
# 2.启动 hive
bin/hive
② 本地模式
本地模式采用外部数据库来存储元数据,目前支持的数据库有:MySQL、Postgres、Oracle、MS SQL Server.在这里我们使用MySQL
本地模式不需要单独起 metastore
服务,用的是跟 hive
在同一个进程里的 metastore
服务,也就是说当你启动一个 hive
服务,里面默认会帮我们启动一个 metastore
服务
hive
根据 hive.metastore.uris
参数值来判断,如果为空,则为本地模式
缺点:每启动一次 hive
服务,都内置启动了一个 metastore
③ 远程模式
该模式下需要单独起 metastore
服务,然后每个客户端都在配置文件里配置连接到该 metastore
服务
远程模式的 metastore
服务和 hive
运行在不同的进程里
在生产环境中,建议用远程模式来配置 Hive Metastore
Hive 远程模式安装
注意:我们当前已经有了一个 hadoop 集群,集群中有 3 个节点,我们只需要在其中的某个节点安装 Hive 即可
① 解压 Hive 安装包并重命名
# 进入自定义的安装目录,上传安装包
cd /opt/server
# 解压安装包
tar -zxvf apache-hive-2.1.0-bin.tar.gz
# 重命名安装目录
mv apache-hive-2.1.0-bin hive-2.1.0
② 修改 hive-env.sh
# 进入配置文件目录
cd /opt/server/hive-2.1.0/conf
# 修改 hive 的配置文件 : hive-env.sh
cp hive-env.sh.template hive-env.sh
vim hive-env.sh
# 增加修改内容:指定 hadoop 安装目录 和 hive 的配置文件目录
HADOOP_HOME=/opt/server/hadoop-2.7.5
export HIVE_CONF_DIR=/opt/server/hive-2.1.0/conf
③ 修改 hive-site.xml
# 进入配置文件目录
cd /opt/server/hive-2.1.0/conf
# 编辑 hive-site.xml
vim hive-site.xml
# 增加以下内容,并修改
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- 将此处的用户名修改为你自定义的 mysql 用户名 -->
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>用户名</value>
</property>
<!-- 将此处的密码修改为你自定义的 mysql 密码 -->
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>密码</value>
</property>
<!-- 将此处的 mysql 修改为你自定义的 mysql 地址,我使用的是阿里云的 mysql -->
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://xxxx:3306/hive?createDatabaseIfNotExist=true&useSSL=false</value>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<property>
<name>hive.metastore.schema.verification</name>
<value>false</value>
</property>
<property>
<name>datanucleus.schema.autoCreateAll</name>
<value>true</value>
</property>
<property>
<name>hive.server2.thrift.bind.host</name>
<value>node3</value>
</property>
</configuration>
④ 上传 mysql
的驱动包到 hive 的 lib
目录下
# 就是这个目录
/opt/server/hive-2.1.0/lib
⑤ 将 hive-2.1.0/jdbc/
目录下的 hive-jdbc-2.1.0-standalone.jar
拷贝到 hive-2.1.0/lib/
目录
cp /opt/server/hive-2.1.0/jdbc/hive-jdbc-2.1.0-standalone.jar /opt/server/hive-2.1.0/lib/
⑥ 在当前节点上配置 hive 的环境变量
# 修改环境配置文件
vim /etc/profile
# 添加以下内容
export HIVE_HOME=/opt/server/hive-2.1.0
export PATH=:$HIVE_HOME/bin:$PATH
Hive 启动
① 启动服务
# 启动 metastore 服务
cd /opt/server/hive-2.1.0
nohup bin/hive --service metastore &
# 启动验证:如果出现 RunJar 的进程,则启动成功
jps
# 可以查看日志,日志文件为 nohup.out 文件
② 使用 BeenClient 验证
# 启动 hiveserver2 , beeline 客户端需要使用 hiveserver2
cd /opt/server/hive-2.1.0
nohup bin/hive --service hiveserver2 &
# 启动 beeline
cd /opt/server/hive-2.1.0
bin/beeline
# 在命令行连接
!connect jdbc:hive2://node3:10000
Hive 的交互方式
① 第一种交互方式: hive
# 进入 bin 目录
cd /opt/server/hive-2.1.0/bin/
# 直接通过 hive 命令启动
hive
# 测试创建一个数据库
create database if not exists mytest;
② 第二种交互方式:使用 sql
语句或者 sql
脚本进行交互
不进入 hive
的客户端直接执行 hive
的 hql
语句
# 进入 bin 目录
cd /opt/server/hive-2.1.0/bin/
# 直接执行 sql
hive -e "create database if not exists mytest;"
或者我们可以将我们的 hql
语句写成一个脚本然后执行
cd /opt/server/
vim hive.sql
create database if not exists mytest;
use mytest;
create table stu(id int,name string);
通过 hive -f
来执行我们的sql脚本
# 在生产中,通过脚本定时执行经常会用到
hive -f /opt/server/hive.sql
③ 第三种交互方式:Beeline Client
hive
经过发展,推出了第二代客户端 beeline
,但是 beeline
客户端不是直接访问 metastore
服务的,而是需要单独启动hiveserver2
服务
# nohup 和 & 表示后台启动
# 1.启动 metastore 服务
cd /opt/server/hive-2.1.0
nohup bin/hive --service metastore &
# 2.启动hiveserver2
cd /opt/server/hive-2.1.0
nohup bin/hive --service hiveserver2 &
# 3.在 hive 的 bin 目录下执行
cd /opt/server/hive-2.1.0
bin/beeline
# 4.输入 hive 的连接
!connect jdbc:hive2://node3:10000
# 5.依次输入用户名、密码,待连接成功后,就可以直接执行输入的 sql 命令
# 6.beeline 会话退出命令
!quit
开启智能本地模式
我用的是云服务器,带宽有点小,hive
执行起来很慢,可以考虑开启本地模式
-- 本地模式:对数据量比较小的操作,就可以在本地执行,这样要比提交任务到集群执行效率要快很多
SET hive.exec.mode.local.auto=true;
beeline 一键启动脚本
Hive 一键启动脚本
写一个 expect
脚本,可以一键启动 beenline
,并登录到 hive
。expect
是建立在 tcl
基础上的一个自动化交互套件, 在一些需要交互输入指令的场景下, 可通过脚本设置自动进行交互通信
# 1.安装 expect
yum install expect
# 2.创建脚本
cd /opt/server/hive-2.1.0/bin
vim beenline.exp
# 3.增加以下内容
#!/bin/expect
spawn beeline
set timeout 5
expect "beeline>"
send "!connect jdbc:hive2://node3:10000\r"
expect "Enter username for jdbc:hive2://node3:10000:"
send "root\r"
expect "Enter password for jdbc:hive2://node3:10000:"
send "123456\r"
interact
# 4.设置脚本权限
chmod 777 beenline.exp
# 5.通过脚本启动 beeline
expect beenline.exp
Hive 安装常见的问题
① 第一个问题:datanucleus.schema.autoCreateTables
未启用
javax.jdo.JDODataStoreException: Required table missing : “VERSION” in Catalog “” Schema “”. DataNucleus requires this table to perform its persistence operations. Either your MetaData is incorrect, or you need to enable “datanucleus.schema.autoCreateTables”
at org.datanucleus.api.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:553)
问题修复:
# 修改 hive-site.xml
cd /opt/server/hive-2.1.0/conf/
vim hive-site.xml
# 增加以下内容
<property>
<name>datanucleus.schema.autoCreateAll</name>
<value>true</value>
</property>
执行命令时,启动 hive
时使用如下命令:
# 启动元数据服务
cd /opt/server/hive-2.1.0
nohup hive --service metastore &
# 启动客户端连接服务
cd /opt/server/hive-2.1.0
nohup hive --service hiveserver2 &
② 问题2:连接拒绝
Could not open client transport with JDBC Uri: jdbc:hive2://node3:10000: java.net.ConnectException: Connection refused (Connection refused) (state=08S01,code=0)
问题修复:
# 你在 hive-site.xml 中配置的用户名和密码没有在 mysql 中赋权
# 注意如果 mysql 中没有这个用户就创建这个用户
# 如果是 mysql 5.0 + 的版本,在 mysql 中执行以下 sql
create user 用户名 identified by '密码';
grant all privileges on *.* to '用户名'@'%' identified by '密码';
flush privileges;
# 如果是 mysql 8.0 + 的版本,在 mysql 中执行以下 sql
create user 用户名 identified by '密码';
grant all privileges ON hive.* TO '用户名'@'%';
flush privileges;
③ 问题3:用户权限不足
Error: 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=,code=0)
问题修复:
# 修改 hadoop 配置文件 etc/hadoop/core-site.xml,集群中的每个节点都需要修改,修改前记得先停止集群
# 进入 hadoop 配置文件目录
cd /opt/server/hadoop-2.7.5/etc/hadoop/
# 修改 core-site.xml
vim core-site.xml
# 增加以下内容,注意使用什么用户,此处就写什么用户
<!-- 如果是 test 用户,此处就应该为 hadoop.proxyuser.test.hosts,同下-->
<property>
<name>hadoop.proxyuser.root.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.root.groups</name>
<value>*</value>
</property>
问题 ④ :需要初始化数据库 或 第一次启动需要初始化数据库
第一种:Exception in thread “main” java.lang.RuntimeException: org.apache.hadoop.hive.ql.metadata.HiveException: org.apache.hadoop.hive.ql.metadata.HiveException: MetaException(message:Hive metastore database is not initialized. Please use schematool (e.g. ./schematool -initSchema -dbType …) to create the schema. If needed, don’t forget to include the option to auto-create the underlying database in your JDBC connection string (e.g. ?createDatabaseIfNotExist=true for mysql))
第二种:Exception in thread “main” java.lang.RuntimeException: org.apache.hadoop.hive.ql.metadata.HiveException: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
切记:在哪个路径下初始化的,以后启动命令就在哪个路径下执行
问题修复:
根据错误提示,执行如下命令:
# 初始化数据库
cd /opt/server/hive-2.1.0
schematool -dbType mysql -initSchema
切记:在哪个目录下执行的初始化命令,以后 hive 的启动命令就要在哪个目录下执行,不然会找不到元数据
问题 ⑤ :初始化错误,元数据状态不一致
Error: Syntax error: Encountered “” at line 1, column 64. (state=42X01,code=30000)
org.apache.hadoop.hive.metastore.HiveMetaException: Schema initialization FAILED! Metastore state would be inconsistent !!
Underlying cause: java.io.IOException : Schema script failed, errorcode 2
Use --verbose for detailed stacktrace.
问题修复:
心态炸了,这个问题困扰了我好几天,我一直以为是我的配置错误,配置的是 mysql
的驱动,执行却用的是 derby
的驱动,这当然会报错,应该想到的是我们的 hive-site.xml
配置没生效,详细解答请看这里
别在 hive
的 bin
目录下执行初始化命令,在其它地方执行都没问题!!!在哪个目录下执行的初始化命令,以后 hive 的启动都要去那个目录执行
Hadoop集群搭建:9.Hive安装及site.xml不生效的踩坑 - 简书 (jianshu.com)
成功执行!
DataGrip2021 连接 Hive2.1
在 DataGrip 2021 版本中,默认的 Hive Driver 的版本为 3.1,所以我们需要手动的设置 Hive 2.1 的驱动
① 在本地解压 apache-hive-2.1.0-bin.tar.gz
② 在解压后的目录中 ./apache-hive-2.1.0-bin/lib
中的指定 jar
包移动到 DataGrip
中,没有的 jar
包自己下载
# 我的 DataGrip 的 hive 驱动目录,请对应到自己的目录
/Users/kylewhite/Library/Application Support/JetBrains/DataGrip2021.1/jdbc-drivers/Hive
# 创建目录
mkdir himkdir 2.1.0
# 将以下 jar 包移动到刚才创建的目录中
guava-25.0-jre.jar
hive-shims-2.1.0.jar
hadoop-auth-2.6.0.jar
httpclient-4.4.jar
hadoop-common-2.6.0.jar
httpclient-4.5.8-sources.jar
hive-common-2.1.0.jar
httpcore-4.4.jar
hive-jdbc-2.1.0.jar
jcl-over-slf4j-1.7.25.jar
hive-metastore-2.1.0.jar
libthrift-0.9.3.jar
hive-serde-2.1.0.jar
slf4j-api-1.7.5.jar
hive-service-2.1.0.jar
hive-service-rpc-2.1.0.jar
commons-lang-2.6.jar
③ 更换当前的驱动版本
④ 选择刚才已经复制的 jar
包列表
⑤ 测试 DataGrip 连接