概述
The Apache Hive ™ data warehouse software facilitates reading, writing, and managing large datasets residing in distributed storage using SQL. Structure can be projected onto data already in storage. A command line tool and JDBC driver are provided to connect users to Hive.
Apache Hive数据仓库软件可以使用SQL方便地阅读、编写和管理分布在分布式存储中的大型数据集。结构可以投射到已经存储的数据上。提供了一个命令行工具和JDBC驱动程序来将用户连接到Hive。
1. Hive的产生背景
The Apache Hive™ data warehouse software facilitates reading, writing, and managing large datasets residing in distributed storage and queried using SQL syntax.
Built on top of Apache Hadoop™, Hive provides the following features:
- Tools to enable easy access to data via SQL, thus enabling data warehousing tasks such as extract/transform/load (ETL), reporting, and data analysis.
- A mechanism to impose structure on a variety of data formats
- Access to files stored either directly in Apache HDFS™ or in other data storage systems such as Apache HBase™
- Query execution via Apache Tez™, Apache Spark™, or MapReduce
- Procedural language with HPL-SQL
- Sub-second query retrieval via Hive LLAP, Apache YARN and Apache Slider.
Apache Hive数据仓库软件可以方便地阅读、编写和管理分布在分布式存储中的大型数据集,并使用SQL语法查询。
基于Apache Hadoop之上,Hive提供以下特性:
- 能够通过SQL轻松访问数据的工具,从而支持诸如提取/转换/装载(ETL)、报告和数据分析等数据仓库任务。
- 一种对各种数据格式施加结构的机制
- 访问Apache HDFS或其他数据存储系统中存储的文件,比如Apache HBase
- 通过Apache Tez、Apache Spark或MapReduce进行查询执行
- 过程语言与HPL-SQL
- Sub-second query retrieval via Hive LLAP, Apache YARN and Apache Slider.
1.1 MapReduce局限性
- MapReduce编程不便性
- HDFS上的文件缺少Schema(字段名,字段类型等)
- 传统RDBMS人员需求可以通过现有技能SQL操纵数据
1.2 什么是hive
- Facebook解决海量结构化日志数据的统计文件
- 构建在Hadoop之上的数据仓库
- 数据存储在HDFS之上
- 计算方式采用MapReduce
- 底层支持多种不同的执行引擎(Hive on MapReduce、Hive on Tez、Hive on Spark)
- Hive定义一种类SQL的查询语言: HQL(类似SQL但不完全相同)
- 支持多种不同的压缩格式、存储格式以及自定义函数(压缩:GZIP、LZO、Snappy、BZIP2.. ; 存 储:TextFile、SequenceFile、RCFile、ORC、Parquet ; UDF:自定义函数)
- 弹性线性扩展
1.3 为什么要使用Hive
- 简单、容易上手通过SQL搞定海量数据统计分析(提供了类似SQL查询语言HQL)
- 为超大数据集设计的计算/存储扩展能力(MR计算、HDFS存储弹性线性扩展)
- 统一的元数据管理(可与Presto/Impala/SparkSQL等共享数据)
1.4 Hive不是
- 一个关系数据库
- 一个设计用于联机事务处理(OLTP)
- 实时查询和行级更新的语言
1.5 Hive特定
- 它存储架构在一个数据库中并处理数据到HDFS。
- 它是专为OLAP设计。
- 它提供SQL类型语言查询叫HiveQL或HQL,通过SQL语句翻译MapReduce作业,并提交到YARN上运行
- 它是熟知,快速,可扩展和可扩展的。
2.Hive使用场景
- Hive 构建在基于静态批处理的Hadoop 之上,Hadoop 通常都有较高的延迟并且在作业提交和调度的时候需要大量的开销。因此,Hive 并不能够在大规模数据集上实现低延迟快速的查询,例如,Hive 在几百MB 的数据集上执行查询一般有分钟级的时间延迟。因此,Hive 并不适合那些需要低延迟的应用,例如,联机事务处理(OLTP)。
- Hive 查询操作过程严格遵守Hadoop MapReduce 的作业执行模型,Hive 将用户的HiveQL 语句通过解释器转换为MapReduce 作业提交到Hadoop 集群上,Hadoop 监控作业执行过程,然后返回作业执行结果给用户。
- Hive 并非为联机事务处理而设计,Hive 并不提供实时的查询和基于行级的数据更新操作。Hive 的最佳使用场合是大数据集离线批处理作业(OLAP联机分析处理),例如,网络日志分析。
3. Hive生产环境使用方式
- 实时产生数据存放HDFS存储(热数据)
- 定期转移亚马逊S3/阿里云OSS(冷数据)
- 通过参数table的location指向不同入口(hdfs S3 OSS)切换不同分析源数据
4. 体系架构

In Hive distribution, we can find the below components majorly.
- CLI — Command Line Interface. It is the most common way of interacting with Hive. (Hive shell) This is the default service.
- HWI — Hive Web Interface. It is an alternative to the shell for interacting with hive through web browser.
- JDBC/ODBC/Thrift Server — These are provide programmatic access to Hive server. Applications using Thrift, JDBC, and ODBC connectors need to run a Hive server to communicate with Hive. HIVE_PORT environment variable need to be specified with the available port(defaults to 10,000) number to let the server listen on.
- Driver — Driver compiles the input commands and queries, optimizes thecomputation required, and executes the required steps with MapReduce jobs.
- Metastore — The metastore is the central repository of Hive metadata. The metastore is divided into two pieces: a service and the backing store for the data. By default, the metastore is run in the same process as the Hive service. Using this service, it is possible to run the metastore as a standalone (remote) process. Set theMETASTORE_PORT environment variable to specify the port the server will listen on
- Job Tracker — Hive communicates with the Job Tracker to initiate the MapReduce jobs. Hive does not have to be running on the same master node with the JobTracker.
- Namenode — The data files to be processed are in HDFS, which is managed by the NameNode
在Hive分发中,我们可以主要找到下面的组件。
- CLI - 命令行界面。这是与Hive进行交互的最常见的方式。 (Hive shell)这是默认的服务。
- HWI - Hive Web界面。它是通过web浏览器与配置单元进行交互的shell的替代方案。
- JDBC / ODBC / Thrift Server - 提供对Hive服务器的编程访问。使用Thrift,JDBC和ODBC连接器的应用程序需要运行Hive服务器与Hive进行通信。 HIVE_PORT环境变量需要使用可用的端口(默认为10,000)来指定,以便服务器侦听。
- Driver - 驱动程序编译输入命令和查询,优化所需的计算,并执行MapReduce作业所需的步骤。
- Metastore - Metastore是Hive元数据的中央存储库。 Metastore分为两部分:服务和数据的后备存储。默认情况下,Metastore与Hive服务在相同的进程中运行。使用此服务,可以将Metastore作为独立(远程)进程运行。设置METASTORE_PORT环境变量以指定服务器将侦听的端口
- Job Tracker - Hive与作业跟踪器通信以启动MapReduce作业。 Hive不必与JobTracker在同一主节点上运行。
- Namenode - 要处理的数据文件在HDFS中,由NameNode管理
Hive clients:
Below are the three main clients that can interact with Hive Architecture.
- Thrift Client: Hive Thrift Client can run Hive commands from a wide range of programming languages. Thrift bindings for Hive are available for Java, Python, and Ruby.
- JDBC Driver:Hive provides a JDBC driver, defined in the class
org.apache.hadoop.hive.jdbc.HiveDriver. When configured with a JDBC URI of
the form jdbc:hive://host:port/dbname , a Java application will connect to a Hive
server running in a separate process at the given host and port.
We may alternatively choose to connect to Hive through JDBC in embedded mode using the URI jdbc:hive://. In this mode, Hive runs in the same JVM as the application
invoking it, so there is no need to launch it as a standalone server since it does not
use the Thrift service or the Hive Thrift Client.
Hive客户:
以下是可以与Hive 架构交互的三个主要客户端。
- Thrift客户端:Hive Thrift客户端可以运行各种编程语言的Hive命令。 Hive的Thrift绑定可用于Java,Python和Ruby。
- JDBC驱动程序:Hive提供了一个在类中定义的JDBC驱动程序org.apache.hadoop.hive.jdbc.HiveDriver。当配置了一个JDBC URI的时候形式为jdbc:hive:// host:port / dbname,Java应用程序将连接到一个Hive服务器在给定主机和端口的独立进程中运行。
我们也可以选择通过使用URI jdbc:hive://的嵌入模式通过JDBC连接到Hive。在这种模式下,Hive与应用程序在相同的JVM中运行调用它,所以没有必要启动它作为一个独立的服务器,因为它没有使用Thrift服务或Hive Thrift客户端。 - ODBC驱动程序:Hive ODBC Driver允许支持ODBC协议的应用程序连接到Hive。像JDBC驱动程序一样,ODBC驱动程序使用Thrift与Hive服务器进行通信。
6. ETL介绍
extract 英 [ˈekstrækt] vt. 提取;取出;摘录;榨取 n. 汁;摘录;榨出物;选粹
transform 美 [træns’fɔrm] vi. 变换,改变;转化
load 英 [ləʊd] vi. [力] 加载;装载;装货 n. 负载,负荷;工作量;装载量
ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract)、转换(transform)、加载(load)至目的端的过程。ETL一词较常用在数据仓库,但其对象并不限于数据仓库。
实际上我们可以将ETL看做建立一个数据仓库的的三个步骤,extract就是从各个子系统或者模块的数据库中提取数据,trasfrom实际上就是对提取出来的数据进行清洗,加载就是清洗好的数据再放入数据仓库中。
实际我们可以把实现一个数据仓库的数据获取(Data Acquisition)部分看成ETL。
为了能更好地实现ETL,笔者建议用户在实施ETL过程中应注意以下几点:
第一,如果条件允许,可利用数据中转区对运营数据进行预处理,保证集成与加载的高效性;
第二,如果ETL的过程是主动“拉取”,而不是从内部“推送”,其可控性将大为增强;如果采取的是拉取的策略,只要我们的数据仓库是从去连接各个子模块的数据库获取数据即可,即只需要在数据库仓库应用中实现一个拉取模块即可,如果采取的是推送机制,那么每个子模块都要单独实现各自的推送。
第三,ETL之前应制定流程化的配置管理和标准协议;
第四,关键数据标准至关重要。ETL面临的最大挑战是当接收数据时其各源数据的异构性和低质量。以电信为例,A系统 按照统计代码管理数据,B系统按照账目数字管理,C系统按照语音ID管理。当ETL需要对这三个系统进行集成以获得对客户的全面视角时,这一过程需要复杂 的匹配规则、名称/地址正常化与标准化。而ETL在处理过程中会定义一个关键数据标准,并在此基础上,制定相应的数据接口标准。
















