1.前言
说明:安装hive前提是要先安装hadoop集群,并且hive只需要再hadoop的namenode节点集群里安装即可(需要再所有namenode上安装),可以不在datanode节点的机器上安装。另外还需要说明的是,虽然修改配置文件并不需要你已经把hadoop跑起来,但是本文中用到了hadoop命令,在执行这些命令前你必须确保hadoop是在正常跑着的,而且启动hive的前提也是需要hadoop在正常跑着,所以建议你先将hadoop跑起来在按照本文操作。有关如何安装和启动hadoop集群,请参考:
CentOS7.0下Hadoop 2.7.3集群搭建
2.下载安装Hive2.1.1
2.1.下载Hive 2.1.1
下载地址:http://hive.apache.org/downloads.html
点击图中的***Download a release now!***
按照步骤找到作一镜像,使用wget命令下载:
$wget https://mirrors.tuna.tsinghua.edu.cn/apache/hive/hive-2.1.1/apache-hive-2.1.1-bin.tar.gz
2.2.安装hive 2.1.1
#解压:
tar -zxvf apache-hive-2.1.1-bin.tar.gz
#把解压后的文件移到目录/usr/local/下:
mv apache-hive-2.1.1-bin /usr/local/apache-hive-2.1.1
#配置hive环境变量
vim /etc/profile
--------------------------------------------------
HIVE_HOME=/usr/local/apache-hive-2.1.1
HIVE_CONF_DIR=$HIVE_HOME/conf
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$HADOOP_HOME/bin:$HIVE_HOME/bin
export JAVA_HOME JRE_HOME PATH CLASSPATH HADOOP_HOME HIVE_HOME HIVE_CONF_DIR
-------------------------------------------------
#使配置文件的修改生效
source /etc/profile
3.配置hive
3.1.配置hive-site.xml
#进入目录
cd $HIVE_CONF_DIR
#拷贝hive-default.xml.template并重命名为hive-site.xml
cp hive-default.xml.template hive-site.xml
#编辑hive-site.xml
vim hive-site.xml
3.1.1 使用hadoop新建hdfs目录
因为在hive-site.xml中有这样的配置:
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
<name>hive.exec.scratchdir</name>
<value>/tmp/hive</value>
所以要在Hadoop集群新建/user/hive/warehouse目录,执行命令
cd $HADOOP_HOME #进入Hadoop主目录
bin/hadoop fs -mkdir -p /user/hive/warehouse #创建目录
bin/hadoop fs -chmod -R 777 /user/hive/warehouse #新建的目录赋予读写权限
bin/hadoop fs -mkdir -p /tmp/hive/#新建/tmp/hive/目录
bin/hadoop fs -chmod -R 777 /tmp/hive #目录赋予读写权限
#用以下命令检查目录是否创建成功
bin/hadoop fs -ls /user/hive
bin/hadoop fs -ls /tmp/hive
3.1.2.修改hive-site.xml中的临时目录
将hive-site.xml文件中的${system:java.io.tmpdir}替换为hive的临时目录,例如我替换为/usr/local/apache-hive-2.1.1/tmp/,该目录如果不存在则要自己手工创建,并且赋予读写权限。
cd $HIVE_HOME
mkdir tmp
chmod -R 777 tmp/
例如原来:
<property>
<name>hive.downloaded.resources.dir</name><value>${system:java.io.tmpdir}/${hive.session.id}_resources</value>
<description>Temporary local directory for added resources in the remote file system.</description>
</property>
替换为:
<property>
<name>hive.downloaded.resources.dir</name>
<!--value>${system:java.io.tmpdir}/${hive.session.id}_resources</value-->
<value>/user/local/apache-hive-2.1.1/tmp/${hive.session.id}_resources</value>
<description>Temporary local directory for added resources in the remote file system.</description>
</property>
3.1.3.将配置文件中${system:user.name}都替换为root
例如原来:
<property>
<name>hive.server2.logging.operation.log.location</name><value>${system:java.io.tmpdir}/${system:user.name}/operation_logs</value>
<description>Top level directory where operation logs are stored if logging functionality is enabled</description>
</property>
替换为:
<property>
<name>hive.server2.logging.operation.log.location</name>
<value>/user/local/apache-hive-2.1.1/tmp/root/operation_logs</value>
<description>Top level directory where operation logs are stored if logging functionality is enabled</description>
</property>
说明: 以上给出的只是配置文件中截取了几处以作举例,你在替换时候要认真仔细的全部替换掉。
3.1.4.修改hive-site.xml数据库相关的配置
3.1.4.1. javax.jdo.option.ConnectionDriverName,将该name对应的value修改为MySQL驱动类路径:
<property
<name>javax.jdo.option.ConnectionDriverName</name
<value>com.mysql.jdbc.Driver</value>
</property>
3.1.4.2. javax.jdo.option.ConnectionURL,将该name对应的value修改为MySQL的地址:
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://192.168.56.101:3306/hive?createDatabaseIfNotExist=true</value>
3.1.4.3.javax.jdo.option.ConnectionUserName,将对应的value修改为MySQL数据库登录名:
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
3.1.4.4.javax.jdo.option.ConnectionPassword,将对应的value修改为MySQL数据库的登录密码:
<name>javax.jdo.option.ConnectionPassword</name><value>*******</value>
3.1.4.5.将MySQL驱动包上载到Hive的lib目录下
cp /home/dtadmin/spark_cluster/mysql-connector-java-5.1.36.jar $HIVE_HOME/lib/
3.2.新建hive-env.sh文件并进行修改
cd $HIVE_CONF_DIR
cp hive-env.sh.template hive-env.sh #基于模板创建hive-env.sh
vim hive-env.sh #编辑配置文件并加入以下配置:
-------------------------------------------------
export HADOOP_HOME=/home/hadoop/hadoop-2.7.3
export HIVE_CONF_DIR=/usr/local/apache-hive-2.1.1/conf
export HIVE_AUX_JARS_PATH=/usr/local/apache-hive-2.1.1/lib
--------------------------------------------------
4.启动和测试
有关安装与配置MySQL数据库请参考文章:CentOS7.0安装配置MySQL5.7
4.1.对MySQL数据库初始化
#进入到hive的bin目录
cd $HIVE_HOME/bin
#对数据库进行初始化
schematool -initSchema -dbType mysql
执行成功后,在mysql的hive数据库里已生成metadata数据表:
4.2.启动Hive
cd $HIVE_HOME/bin #进入Hive的bin目录
./hive #执行hive启动
4.3.测试Hive
4.3.1.简单测试
成功启动Hive后,会进入hive的命令行模式,下面进行一系列简单测试:
hive> show functions;
OK
-----
!
!=
$sum0
%
&
*
+
-
/
<
<=
<=>
<>
=
==
>
>=
^
abs
acos
add_months
aes_decrypt
aes_encrypt
and
array
array_contains
ascii
asin
assert_true
atan
avg
base64
between
bin
bround
case
cbrt
ceil
ceiling
chr
coalesce
collect_list
collect_set
compute_stats
concat
...
hive> desc function sum;
OK
----
sum(x) - Returns the sum of a set of numbers
Time taken: 0.007 seconds, Fetched: 1 row(s)
4.3.2.执行新建库、表以及导入数据的测试
执行新建数据库的hive命令:
hive>create database reqdb;
hive>use reqdb;
hive> create table req
> (
> request_id bigint,
> message_type_id int,
> request_type int,
> request_time string,
> request_url string,
> request_source int,
> request_body string,
> response_time string,
> send_time string,
> attachment_id int,
> template_id bigint,
> linked_template_id string,
> linked_job_id bigint,
> application_id int,
> created_by string,
> creation_date string,
> last_updated_by string,
> last_updated_date string
> ) row format delimited fields terminated by ‘\t\t\t’;
hive> load data local inpath ‘/var/lib/mysql-files/sbux_nc_req.dat’ into table sbux_nc_req;
注:sbux_nc_req.dat文件可能在mysql数据库里生成,生成方法如下:
select * into outfile '/var/lib/mysql-files/req.dat' fields terminated by '\t\t\t' from req;
4.3.3.查看是否成功
hive> select * from req;