Centos搭建Hive

一、Hive简介

Hive是在HDFS之上的架构,Hive中含有其自身的组件,解释器、编译器、执行器、优化器。解释器用于对脚本进行解释,编译器是对高级语言代码进行编译,执行器是对java代码的执行,优化器是在执行过程中进行优化。这里的代码就是Hadoop中的MapReduce,这里的MapReduce通过Hive往HDFS上执行、分析、查询数据。

在这里插入图片描述

上图展示hql的执行过程,一个hql脚本首先到hive的解释器,转化为MapReduce(当然例如“select * from table_name;”这样的语句不用转化成MapReduce),解释器是用于解释脚本的,MapReduce是由脚本生成的;然后编译器再对MapReduce程序进行编译;再然后执行器对代码的执行以及优化器在执行过程优化。可见,Hive并没有更改数据的存储介质,数据仍然在HDFS上。Hive只是通过MapReduce对数据进行查询和分析,这时MapReduce不用进行解释、编译、优化,hive会帮助完成。这时写一个MapReduce程序就变成了写一个hql语句/脚本(或者说类似sql语句/脚本)。

Hive的本质不是一个数据库,更不是一个服务,它不需要端口,没有监听客户端。正因为hive不是一个服务,所以不需要考虑HA和分布式方面的问题,hive实际上就是一种工具,是一种把sql语句转化成MapReduce,然后再放到hadoop去执行MapReduce的一种工具。可以将hive理解为hadoop的一个客户端,因为是hive去连接hdfs,是hive去提交MapReduce程序到hadoop中的ResourceManager主节点。

hive也有其不足之处。虽然hive可以替代一部分MapReduce,但只能做统计查询,以及一些简单的统计分析,要想做数据挖掘仍需写MapReduce。因为hive的特点是基于hql脚本进行操作的,它非常接近关系型数据库的sql语句,所以它的数据结构一定是要有关系的那种才适合统计分析,这种统计分析有局限性。

二、安装Hive

2.1hive下载

清华大学软件镜像站-hive下载

2.2上传解压

将安装包上传至服务器:

rz apach-hive-xxxx.tar.gz

解压:

tar -zxvf apach-hive-xxx.tar.gz

重命名hive:

mv apach-hive-xxx hive

2.3配置hive相关的环境变量

vi ~/.bashrc

export HIVE_HOME=/usr/local/hive
export PATH=$HIVE_HOME/bin

重启使配置生效:

source ~/.bashrc

三、Mysql

在Centos上需要安装Mysql服务,通过网上其他资源,参考安装mysql服务。
CentOS7下使用YUM安装MySQL5.6

yum install  -y  mysql-server

//启动mysql服务
service mysqld start

//开机启动
chkconfig mysqld on

3.1安装mysql connector

yum install -y mysql-connector-java

wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.31.tar.gz

tar zxf mysql-connector-java-5.1.31.tar.gz

3.2 将mysqld上创建 connector 拷贝到hive的lib包中

   cp mysql-connector-java-5.1.31/mysql-connector-java-5.1.31-bin.jar /home/hadoop/hive/lib/

3.3在mysql上创建hive元数据库,并对hive进行授权

 create user 'hive'@'localhost' identified by 'hive'; (创建hive用户)

 grant all privileges on *.* to 'hive'@'localhost' with grant option;(赋予权限)

 flush privileges;(刷新权限)

或:

create database if not existshive_metadata;

grant all privileges onhive_metadata.* to 'hive'@'%' identified by 'hive';

grant all privileges onhive_metadata.* to 'hive'@'localhost' identified by 'hive';

grant all privileges onhive_metadata.* to 'hive'@'spark1' identified by 'hive';

flush privileges;

use hive_metadata;

四、配置hive-site.xml

vi /hive/conf/hive-site.xml


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?><!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<configuration>
                <!--Hive的元数据库,这是连接master.hadoop:3306端口的hive数据库,如果库不存在就可以创建-->
        <property>
                <name>javax.jdo.option.ConnectionURL</name>
                <value>jdbc:mysql://master.hadoop:3306/hive?createDatabaseIfNotExist=true</value>
        </property>
        <!--连接元数据的驱动名-->
        <property>
                <name>javac.jdo.option.ConnectionDriverName</name>
                <value>com.mysql.jdbc.Driver</value>
        </property>
        <!--数据库的用户名-->
        <property>
                <name>javax.jdo.option.ConnectionUserName</name>
                <value>hive</value>
        </property>
        <!--数据库的密码-->
        <property>
                <name>javax.jdo.option.ConnectionPassword</name>
                <value>hive</value>
        </property>
        <!--表示数据在hdfs中的存储位置-->
        <property>
                <name>hive.metastore.warehouse.dir</name>
                <value>hdfs://mycluster/user/hive/warehouse</value>
        </property>
        <!--动态分区的模式,默认strict,表示必须指定至少一个分区为静态分区,nonstrict模式表示允许所有的分区字段都可以使用动态分区。-->
        <property>
                <name>hive.exec.dynamic.partition.mode</name>
                <value>nonstrict</value>
        </property>
        <!--默认情况下,HiveServer2以提交查询的用户执行查询(true),如果hive.server2.enable.doAs设置为false,查询将以运行hiveserver2进程的用户运行。-->
        <property>
                <name>hive.server2.enable.doAs</name>
                <value>false</value>
        </property>
         <!-- hive.metastore.schema.verification值为false即可解决“Caused by: MetaException(message:Version information not found in metastore.)”-->
        <property>  
                <name>hive.metastore.schema.verification</name>  
                <value>false</value>  
        </property>
</configuration>

五、配置hive-env.sh和hive-config.sh

mv hive-env.sh.template hive-env.sh  
#若无hive-env.sh文件,则将hive-env.sh.template 改名为hive-env.sh
vi /usr/local/hive/bin/hive-config.sh 
#增加环境变量
export JAVA_HOME=/usr/java/latest
export HIVE_HOME=/usr/local/hive
exportHADOOP_HOME=/usr/local/hadoop

六、验证hive

直接输入hive命令,即可进入hive命令行。

原文地址:https://www.cnblogs.com/aixing/p/13327452.html