一、安装三种模式

hive的安装一共有三种方式:内嵌模式、本地模式、远程模式

 

内嵌模式

内嵌模式使用的是内嵌的Derby数据库来存储元数据,也不需要额外起Metastore服务。数据库和Metastore服务都嵌入在主Hive Server进程中。这个是默认的,配置简单,但是一次只能一个客户端连接,适用于用来实验,不适用于生产环境。

解压hive安装包  bin/hive 启动即可使用

缺点:不同路径启动hive,每一个hive拥有一套自己的元数据,无法共享。

hive在root安装时为什么时anonymous hive的安装模式_bc

本地模式

本地模式采用外部数据库来存储元数据,目前支持的数据库有:MySQL、Postgres、Oracle、MS SQL Server.在这里我们使用MySQL。

本地模式不需要单独起metastore服务,用的是跟hive在同一个进程里的metastore服务。也就是说当你启动一个hive 服务,里面默认会帮我们启动一个metastore服务。

hive根据hive.metastore.uris 参数值来判断,如果为空,则为本地模式。

缺点是:每启动一次hive服务,都内置启动了一个metastore。

hive在root安装时为什么时anonymous hive的安装模式_hive_02

 

 

远程模式*********一般都采用此模式的安装

远程模式下,需要单独起metastore服务,然后每个客户端都在配置文件里配置连接到该metastore服务。远程模式的metastore服务和hive运行在不同的进程里。

在生产环境中,建议用远程模式来配置Hive Metastore。

在这种情况下,其他依赖hive的软件都可以通过Metastore访问hive。

hive在root安装时为什么时anonymous hive的安装模式_bc_03

远程模式下,需要配置hive.metastore.uris 参数来指定metastore服务运行的机器ip和端口,并且需要单独手动启动metastore服务。

 

 

 



 

hive的安装

 

一、安装hive

 

1.1、准备工作和安装mysql

 

1、下载hive的安装包,这里我们选用hive的版本是2.1.1,软件包为:apache-hive-2.1.0-bin.tar.gz

Hive下载地址:http://archive.apache.org/dist/hive/">http://archive.apache.org/dist/hive/

2、下载mysql的安装包,我们使用的mysql版本是5.7.29,软件包为:mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz

     下载地址:https://downloads.mysql.com/archives/community/">https://downloads.mysql.com/archives/community/  

3、将apache-hive-2.1.0-bin.tar.gz上传到/export/software目录

将mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz上传到/export/software目录

 

03 mysql5.6 安装(本地包和rpm方式),mysql8.0安装

 

 


 

1.2、正式安装(node3机器)

 

1.2.1  解压Hive安装包并重命名

cd    /export/software
tar -zxvf apache-hive-2.1.0-bin.tar.gz  -C/export/servers
cd  /export/server
mv apache-hive-2.1.0-bin    hive-2.1.0

 

 

 

1.2.2 修改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

 

1.2.3 修改hive-site.xml(其实不存在,添加)

cd  /export/server/hive-2.1.0/conf
vim hive-site.xml

 

在该文件中添加以下内容

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
      <name>javax.jdo.option.ConnectionUserName</name>
      <value>root</value>
  </property>
  <property>
      <name>javax.jdo.option.ConnectionPassword</name>
      <value>123456</value>
  </property>
  <property>
      <name>javax.jdo.option.ConnectionURL</name>
      <value>jdbc:mysql://node3: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>

1.2.4 上传mysql的lib驱动包

 

将mysql的lib驱动包上传到hive的lib目录下

 

 

cd /export/server/hive-2.1.0/lib

将mysql-connector-java-5.1.38.jar 上传到这个目录下

1.2.5 拷贝相关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/

 

1.2.6 配置hive的环境变量

 

node03服务器执行以下命令配置hive的环境变量

vim /etc/profile

 

添加以下内容:

export HIVE_HOME=/export/server/hive-2.1.0

export PATH=:$HIVE_HOME/bin:$PATH

 

重新加载配置文件

Source /etc/profile

 

至此hive安装完成

 

1.3 hive的交互式方式

 

第一种交互方式: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 mytest"

 

或者我们可以将我们的hql语句写成一个sql脚本然后执行

cd /export/servers

vim  hive.sql

 

脚本内容如下:

create database mytest2;
use mytest2;
createtable stu(id int,name string);

 

通过hive -f   来执行我们的sql脚本

bin /hive  -/export/server/hive.sql

 

第三种交互方式:Beeline Client

hive经过发展,推出了第二代客户端beeline,但是beeline客户端不是直接访问metastore服务的,而是需要单独启动hiveserver2服务。

在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 和 & 表示后台启动

在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/server/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在root安装时为什么时anonymous hive的安装模式_bc_04

 

注意: 如果报出以下, 请修改 hadoop中 core-site.xml文件

错误信息为:   User: root isnot allowed to impersonate root

 

解决方案: 在node1的 hadoop的 core-site.xml文件中添加一下内容:

<property>
<name>hadoop.proxyuser.root.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.root.groups</name>
<value>*</value>
</property>

添加后, 将 core-site.xml 发送到其他两台机子:

cd /export/serverss/hadoop-2.7.5/etc/hadoop

scp core-site.xml node2:$PWD

scp core-site.xml node3:$PWD

然后重启hadoop即可

Hive一键启动脚本(必须在hive运行的服务器上,首先启动metastore服务,然后启动hiveserver2服务)

 

这里,我们写一个expect脚本,可以一键启动beenline,并登录到hive。expect是建立在tcl基础上的一个自动化交互套件, 在一些需要交互输入指令的场景下, 可通过脚本设置自动进行交互通信。

安装expect

yum install expect

 

创建脚本

cd/export/server/hive-2.1.0/bin

vim  beenline.exp

 

\r 代表回车

添加以下内容:

#!/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

 

修改脚本权限

chmod   777 beenline.exp

 

启动beeline

expect beenline.exp