准备工作
在这里,我们使用mysql数据库作为Hive的元数据存储,所以在安装Hive之前,必须安装好mysql
MySQL安装 参考这篇文章
下载hive的安装包,这里我们选用hive的版本是2.1.0,软件包为:apache-hive-2.1.0-bin.tar.gz
Hive下载地址:http://archive.apache.org/dist/hive/
链接:传送门
提取码:ikvk
1、解压Hive安装包并重命名
cd /export/software tar -zxvf apache-hive-2.1.0-bin.tar.gz -C /export/server cd /export/server mv apache-hive-2.1.0-bin hive-2.1.0
1、修改hive的配置文件
hive-env.sh
添加我们的hadoop的环境变量
cd /export/server/hive-2.1.0/conf cp hive-env.sh.template hive-env.sh vim hive-env.sh
修改内容如下:
HADOOP_HOME=/export/server/hadoop-2.7.5 export HIVE_CONF_DIR=/export/server/hive-2.1.0/conf
2、修改hive-site.xml
cd /export/server/hive-2.1.0/conf vim hive-site.xml
在该文件中添加以下内容
javax.jdo.option.ConnectionUserName root javax.jdo.option.ConnectionPassword 123456 javax.jdo.option.ConnectionURL jdbc:mysql://node3:3306/hive?createDatabaseIfNotExist=true&useSSL=false javax.jdo.option.ConnectionDriverName com.mysql.jdbc.Driver hive.metastore.schema.verification false datanucleus.schema.autoCreateAll true hive.server2.thrift.bind.host node3
3、上传mysql的lib驱动包
将mysql的lib驱动包上传到hive的lib目录下
cd /export/server/hive-2.1.0/lib
将mysql-connector-java-5.1.41-bin.jar 上传到这个目录下
4、拷贝相关jar包
将hive-2.1.0/jdbc/目录下的hive-jdbc-2.1.0-standalone.jar 拷贝到hive-2.1.0/lib/目录
cp /export/server/hive-2.1.0/jdbc/hive-jdbc-2.1.0-standalone.jar /export/server/hive-2.1.0/lib/
5、配置hive的环境变量
node03服务器执行以下命令配置hive的环境变量
vim /etc/profile
添加以下内容:
export HIVE_HOME=/export/server/hive-2.1.0 export PATH=:$HIVE_HOME/bin:$PATH
#初始化元数据
cd /export/server/hive-2.1.0/ bin/schematool -dbType mysql -initSchemahive的交互方式
第一种交互方式:bin/hive
cd /export/server/hive-2.1.0/
bin/hive
创建一个数据库
create database mytest; show databases;
第二种交互方式:使用sql语句或者sql脚本进行交互
不进入hive的客户端直接执行hive的hql语句
cd /export/server/hive-2.1.0/ bin/hive -e "create database mytest2"
或者我们可以将我们的hql语句写成一个sql脚本然后执行
cd /export/server vim hive.sql
脚本内容如下:
create database mytest3; use mytest3; create table stu(id int,name string);
通过hive -f 来执行我们的sql脚本
bin/hive -f /export/server/hive.sql
第三种交互方式:Beeline Client
hive经过发展,推出了第二代客户端beeline,但是beeline客户端不是直接访问metastore服务的,而是需要单独启动hiveserver2服务。
1)在node1的/export/server/hadoop-2.7.5/etc/hadoop目录下,修改core-site.xml,在该文件中添加以下配置,实现用户代理:
hadoop.proxyuser.root.hosts * hadoop.proxyuser.root.groups *
将修改好的core-site.xml文件分发到node2和node3,然后重启Hadoop(stop-all.sh start-all.sh)
2)在hive运行的服务器上,首先启动metastore服务,然后启动hiveserver2服务。
nohup /export/server/hive-2.1.0/bin/hive --service metastore & nohup /export/server/hive-2.1.0/bin/hive --service hiveserver2 &
nohup 和 & 表示后台启动
3:在node3上使用beeline客户端进行连接访问。
/export/server/hive-2.1.0/bin/beeline
根据提醒进行以下操作:
[root@node3 ~]# /export/server/hive-2.1.0/bin/beeline which: no hbase in (:/export/server/hive-2.1.0/bin::/export/server/hadoop-2.7.5/bin:/export/data/hadoop-2.7.5/sbin::/export/server/jdk1.8.0_241/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/export/server/mysql-5.7.29/bin:/root/bin) Beeline version 2.1.0 by Apache Hive beeline> !connect jdbc:hive2://node3:10000 Connecting to jdbc:hive2://node3:10000 Enter username for jdbc:hive2://node3:10000: root Enter password for jdbc:hive2://node3:10000:123456
连接成功之后,出现以下内容,可以在提示符后边输入hive sql命令
一键启动hive脚本这里,我们写一个expect脚本,可以一键启动beenline,并登录到hive。expect是建立在tcl基础上的一个自动化交互套件, 在一些需要交互输入指令的场景下, 可通过脚本设置自动进行交互通信。
安装expect
yum -y install expect
创建脚本
cd /export/server/hive-2.1.0 vim beenline.exp
添加以下内容:
#!/bin/expect spawn beeline set timeout 5 expect "beeline>" send "!connect jdbc:hive2://node3:10000r" expect "Enter username for jdbc:hive2://node3:10000:" send "rootr" expect "Enter password for jdbc:hive2://node3:10000:" send "123456r" interact
修改脚本权限
chmod 777 beenline.exp
启动beeline
expect beenline.exp



