Hive的原理和2.x版本部署

1.什么是Hive

Hive 是由 Facebook 实现并开源
是基于 Hadoop 的一个数据仓库工具
可以将机构化数据映射为一张数据库表
提供 HQL(Hive SQL)查询功能
底层数据是存储在 HDFS 上
hive的本质是将 SQL 语句转化为 MapReduce 任务运行
使不熟悉 MapReduce 的用户很方便的利用 HQL 处理和计算 HDFS 上的结构化数据,适用于离线批量数据计算
  • Hive 是数据仓库,用来存储数据的,但是真实存储在 Hive 中的数据,底层是存储在 HDFS
    上的。

  • Hive是一个使用sql语句计算存储在HDFS上的数据的,底层的窒息那个引擎是:MapReduce

  • Hive 是数据仓库,但是本身不存储数据,仅仅是用来存储描述存储在HDFS上的数据

  • 为了SQL语法能够顺利进行,肯定能够需要把存储在HDFS上的数据,进行一番抽象,并且把抽象出来的数据进行存储,Hive就存储这份数据,叫元数据。

  • Hive的元数据,就是描述存储在HDFS上的数据的数据,Hive数据仓库的元数据是不能丢失的,最常用的也是存储在MySQL中,Hive本身也不存储元数据。

  • Hive的本质是就是把用户编写的 SQL语句转换成hadoop能够执行的mapreduce分布式计算程序去执行

    所以说 Hive 是基于 Hadoop 的一个数据仓库工具,实质就是一款基于 HDFS 的 MapRedeuce 计算框架,对存储在 HDFS 中的数据进行分析和管理。
    在这里插入图片描述

2. hive 的特点

优点:

  1. 可扩展性,横向扩展,Hive 可以自由的扩展集群的规模,一般情况下不需要重启服务,其扩展其实就是hadoop集群的扩展。
  2. 延展性,Hive支持自定义函数,用户可以根据自己的需求实现自己的函数
  3. 良好的容错性,可以包张即使有节点出现问题,sql语句人可以完成执行。

缺点:

  1. Hive 不支持记录级别的增删改查操作,2.3版本后支持记录级别的插入操作
  2. Hive 的查询延时很严重,因为MapReduce Job的启动过程消耗时间,所以不能用在交互系统中
  3. Hive 不支持事务

3. hive的基本组成和内部架构

Hive的内部架构是由下面四部分组成:

  1. 用户接口:shell/CLI jdbc/odbc webui
  2. 跨语言服务:thrift server 提供了一种能力,让用户可以使用多种不同的语言来操纵hive
  3. 底层Driver: 驱动器Driver 、 编译器compiler、 优化器optimizer 、 执行器executor
  4. 元数据存储系统:RDBMS MySQL

基本组成

  1. 用户接口

    CLI ,shell 终端命令行,采用交互形式使用 Hive 命令行与Hive进行交互
    JDBC/ODBC 是Hive的基于JDBC操作提供的客户端,用户通过这连接到Hiver server 服务

  2. Thrift Server
    Thrift 是 Facebook 开发的一个软件框架,可以用来进行可扩展且跨语言的服务的开发,Hive 继承了该服务,能让不同的变成语言调用 Hive 的接口。

  3. 元数据存储
    元数据就是存储在Hive中的数据的描述信息。(实际在mysql中)
    Hive中的元数据通常包阔:表的名字,表的列和分区及其属性,表的属性,表的数据在hdfs上的目录
    Metastore 默认存在自带的Derby数据库中,且缺点是不支持多用户操作,并且数据存储目录不固定,数据库跟着Hive走,不方便管理,所以一般情况下元数据库存储在mysql中

  4. Driver: 编译器,优化器,执行器
    driver组件完成HQL查询语句从词法分析,语法分析,编译,优化,以及生成逻辑执行计划的生成,生成的逻辑执行计划存储在 HDFS 中,并随后由 MapReduce 调用执行。

架构图
在这里插入图片描述

4. hive 自带的derby的部署

搭建Hive分为两种模式:
1.使用自带的derby模式
2.使用自定义的MySQL模式
看下图,这里有2个版本的Hive,我们用hive-1.2.2使用自带的derby模式部署
在这里插入图片描述

[root@wyl01 apache-hive-1.2.2-bin]# ./bin/hive

Logging initialized using configuration in jar:file:/opt/apache-hive-1.2.2-bin/lib/hive-common-1.2.2.jar!/hive-log4j.properties
hive> show databases;
OK
default
Time taken: 0.533 seconds, Fetched: 1 row(s)

执行上述指令后,我们发现在当前目录下看到derby.log 和 metastore_db 两个文件
在这里插入图片描述
而 hdfs 中并没有产生产生Hive的存储目录
在这里插入图片描述
我们创建一个属于自己的数据库

hive> create database wyldb;
OK
Time taken: 0.314 seconds
hive> 

在这里插入图片描述
然后再创建一个表

hive> create table student(id int);
OK
Time taken: 0.678 seconds
hive> 

默认是在default的下面
在这里插入图片描述
我们切换数据库,然后再创建一个表

hive> use wyldb;
OK
Time taken: 0.02 seconds
hive> create table student2(id int);
OK
Time taken: 0.088 seconds

在wyldb.db的目录下有一个student2的表
在这里插入图片描述
我们用insert语句插入数据,实际中我们不会这么去插入数据的。这里只是测试

hive> insert into student2 (id) values (1),(2),(3);
Query ID = root_20190521214824_ccfbc555-a7be-4fbf-8adc-717014df3ca7
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1558255407994_0028, Tracking URL = http://wyl02:8088/proxy/application_1558255407994_0028/
Kill Command = /opt/hadoop/bin/hadoop job  -kill job_1558255407994_0028
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2019-05-21 21:48:43,830 Stage-1 map = 0%,  reduce = 0%
2019-05-21 21:48:53,832 Stage-1 map = 100%,  reduce = 0%, Cumulative CPU 1.75 sec
MapReduce Total cumulative CPU time: 1 seconds 750 msec
Ended Job = job_1558255407994_0028
Stage-4 is selected by condition resolver.
Stage-3 is filtered out by condition resolver.
Stage-5 is filtered out by condition resolver.
Moving data to: hdfs://wyl01:8020/user/hive/warehouse/wyldb.db/student2/.hive-staging_hive_2019-05-21_21-48-24_871_7963711988067543195-1/-ext-10000
Loading data to table wyldb.student2
Table wyldb.student2 stats: [numFiles=1, numRows=3, totalSize=6, rawDataSize=3]
MapReduce Jobs Launched: 
Stage-Stage-1: Map: 1   Cumulative CPU: 1.75 sec   HDFS Read: 3312 HDFS Write: 76 SUCCESS
Total MapReduce CPU Time Spent: 1 seconds 750 msec
OK
Time taken: 30.602 seconds

我们打开另一个终端,用hdfs指令查看数据
在这里插入图片描述
那么当我们换一个目录通过客户端登陆到hive中去,如下图所示:

我们发现刚刚创建的wyldb数据库不见了,原因在上面也讲过了
Metastore 默认存在自带的Derby数据库中,且缺点是不支持多用户操作,并且数据存储目录不固定,数据库跟着Hive走,不方便管理,所以一般情况下元数据库存储在mysql中
Hive会在当前目录下重新创建derby.log和meta的元数据文件,而当前的文件中,自然没有我们刚刚创建的wyldb的数据信息。

5 Hive自定义MySQL的Hive安装

hive-1.2.1的版本,不需要进行元数据的初始化。我们现在选择的版本是2.3.5版本

安装过程:

5.1 解压缩包

# 解压缩包
tar -xf apache-hive-2.3.5-bin.tar.gz  -C /opt/
# 创建软连接
ln -s apache-hive-2.3.5-bin hive

5.2 配置环境变量

vim /etc/profile
# 添加以下内容
export HIVE_HOME=/opt/hive
export PATH=${HIVE_HOME}/bin:$PATH
# 配置完记得要生效
source  /etc/profile

5.3 配置文件hive-site.xml

<configuration>
 <property>
 <name>javax.jdo.option.ConnectionURL</name>
 <value>jdbc:mysql://wyl01:3306/hive_metastore235?createDatabaseIfNotExist=true</value>
 </property>
 <property>
 <name>javax.jdo.option.ConnectionDriverName</name>
 <value>com.mysql.jdbc.Driver</value>
 </property>
 <property>
 <name>javax.jdo.option.ConnectionUserName</name>
 <value>root</value>
 </property>
 <property>
 <name>javax.jdo.option.ConnectionPassword</name>
 <value>baifendian</value>
 </property>
 <property>
 <name>hive.metastore.warehouse.dir</name>
 <value>/user/hive/warehouse</value>
 </property>
</configuration>

5.4 把mysqljava驱动的jar包复制到hivelib的目录下
mysql-connector的jar包下载地址
5.5 确保hadoop集群是启动的

# 启动略

5.6 元数据初始化

[root@wyl01 opt]# schematool -dbType mysql -initSchema
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/apache-hive-2.3.5-bin/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hadoop-2.9.2/share/hadoop/common/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Metastore connection URL:	 jdbc:mysql://wyl01:3306/hive_metastore_235?createDatabaseIfNotExist=true
Metastore Connection Driver :	 com.mysql.jdbc.Driver
Metastore connection User:	 root
Wed May 22 09:07:49 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Starting metastore schema initialization to 2.3.0
Initialization script hive-schema-2.3.0.mysql.sql
Wed May 22 09:07:50 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Initialization script completed
Wed May 22 09:07:53 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
schemaTool completed

初始化完成后,我们可以看到上面配置中hive_metastore_235这个原数据库235是hive的版本号
在这里插入图片描述
5.7 进入hive中

[root@wyl01 opt]#  hive

现在我们去hdfs上查看一下,没有hive默认的user目录存在
在这里插入图片描述
验证是否安装成功

# 创建数据库
hive> create database wyldb2;
OK
Time taken: 0.297 seconds
hive> create table student(id int);
OK
Time taken: 0.801 seconds

# 加载数据
hive> load data local inpath "/opt/student.txt" into table student;
Loading data to table default.student
OK
Time taken: 1.087 seconds
hive> select * from student;
OK
1
2
3
4
5
Time taken: 3.098 seconds, Fetched: 5 row(s)        #5条记录

这时候刷新页面,我们看到有刚刚创建的wyldb2数据库还有student表。
在这里插入图片描述

6 FAQ

6.1 在启动或者hive客户端连接的的过程中出现下面的错误

Wed May 22 11:27:40 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

原因是:
是Mysql数据库的SSL连接问题,提示警告不建议使用没有带服务器身份验证的SSL连接,是在MYSQL5.5.45+, 5.6.26+ and 5.7.6+版本中才有的这个问题。解决办法在警告中已经说明了,在连接后添加一个useSSL=false即可,例如:

jdbc:mysql://wyl01:3306/hive_metastore_235?useSSL=falsec?reateDatabaseIfNotExist=true&amp;useSSL=false

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值