前言

Apache Hive是一个开源的数据仓库软件,用于读取、写入和管理直接存储在HDFS或其他数据存储系统(如Apache HBase)中的大型数据集文件。 Hive允许SQL开发人员编写类似于标准SQL语句的HQL语句,用于数据查询和分析。 它的目的是使MapReduce编程更容易,因为您不需要了解和编写冗长的Java代码。 相反,您可以在HQL中更简单地编写查询,然后Hive可以创建映射并减少函数。


一、Hive的概述?

(一)Hive是什么?

1.Related Concepts

(1)definition

Apache Hive是一个建立在Hadoop上的开源数据仓库框架。它允许用户使用类SQL语言HiveQL (HQL)查询和分析存储在HDFS (Hadoop Distributed File System)中的大型数据集。

Apache Hive is an open-source data warehouse framework built on Hadoop. It allows users to query and analyze large datasets stored in HDFS (Hadoop Distributed File System) using a SQL-like language called HiveQL (HQL).

(2)the advantage of HQL

Hive允许SQL开发人员编写类似于标准SQL语句的HQL语句,用于数据查询和分析。 它的目的是使MapReduce编程更容易,因为您不需要了解和编写冗长的Java代码。 相反,您可以在HQL中更简单地编写查询,然后Hive可以创建映射并减少函数

Hive enables SQL developers to write Hive Query Language (HQL)
statements that are similar to standard SQL statements for data query and analysis. It is designed to make MapReduce programming easier because you don’t have to know and write lengthy Java code. Instead, you
can write queries more simply in HQL, and Hive can then create the map and reduce the functions.

2.How to explain

(1)编程实现

Hive用户可以编写简单的类似SQLHQL的查询,而不是编写Java的MapReduce程序

Instead of writing Java MapReduce programs,Hive users can write simple SQL like queries

(2)运行方式

Hive在内部将HiveQL转换MapReduce、Tez或Spark作业执行

Hive internally converts HiveQL into MapReduce,Tez,or Spark jobs for execution

(3)数据仓库

不是传统的RDBMS,而是用于批处理的查询引擎。

It is not a traditional RDBMS bur a query engine for batch processing.

3.Hive的特点

(1) SQL-like query language (HiveQL).
HQL实现查询功能,类SQL语言

(2)Data warehousing capabilities (ETL, reporting, analytics).
数据仓库功能

(3)Scalable & fault-tolerant (uses Hadoop cluster).
可扩展性和容错性

(4)Extensible (supports User Defined Functions).
可扩展(支持自定义函数)

(5)Schema on Read: Schema is applied when reading data, not when storing (flexible with semi-structured data).
读取模式:读取数据时应用模式,存储数据时不应用模式(对半结构化数据灵活)

4.Hive的功能

Hive是一种基于Hadoop文件系统的数据仓库架构。 对存储在HDFS中的数据进行分析和管理。 主要提供以下功能:

(1)它提供了一系列可用于**提取/转换/加载(ETL)**数据的工具
It provides a series of tools that can be used to extract / transform / load (ETL) the data

(2)它是一种可以存储、查询和分析存储在HDFS(或HBase)上的大规模数据的机制。
It is a mechanism that can store, query, and analyze large-scale data stored in HDFS (or HBase)

(3)查询是通过MapReduce完成的(并非所有查询都需要MapReduce,例如select * from XXX)。
The query is done through MapReduce (not all queries require MapReduce, such as select * from XXX)

(4)在Hive 3.1.1中,类似于select a, b这样的查询可以在不使用MapReduce的情况下进行配置,(通过更换引擎(Tez/Spark)和更聪明的优化器,能让一大部分简单的查询(如只选字段、带简单过滤条件的查询)都不再走MapReduce,速度大幅提升
In Hive 3.1.1, queries similar to select a, b from XXX can be configured without MapReduce through configuration

5.在Hive中分析和管理数据

Hive定义了一种类似sql的查询语言HQL。 对于熟悉SQL的用户,可以使用Hive直接查询数据。 同时,该语言还允许熟悉MapReduce的开发人员开发自定义映射器和简化器来处理内置映射器和简化器无法完成的复杂分析任务。Hive允许用户编写可以在查询中使用的自己的函数UDF。 Hive中有三种类型的UDF:

(1)UDF

UDF (User Defined Function) → Scalar functions (row-level).、
UDF(用户定义函数) →标量函数(行级)。

(2)UDAF

UDAF (User Defined Aggregate Function) → Aggregate (works on multiple rows).
UDAF(用户定义的聚合函数) →Aggregate(多行处理)。

(3)UDTF

UDTF (User Defined Table Function) → Generates multiple rows from one input row
UDTF(用户自定义表函数) →从一个输入行生成多行

(二)Hive Storage

1.Hive data model

(1)definition

Hive将存储分为表数据(HDFS中的实际用户数据)元数据(Metastore中的模式信息) 两部分。

(2)detailed information

表数据→以文件的形式存储在HDFS中。
Table Data → Stored in HDFS as files.

元数据→存储在名为Metastore的关系型数据库中
(例如:MySQL/Derby)。包括表名、列详细信息、分区、表类型(托管/外部)、数据目录等。
没有元数据服务,Hive就无法工作,因为查询需要模式信息。

Metadata → Stored in a relational database called Metastore (e.g., MySQL/Derby).Includes table names, column details, partitions, table type (managed/external), data directory, etc.Without Metastore, Hive cannot function because queries need schema info.

2.Hive data storage

Hive基于Hadoop分布式文件系统,其数据存储在Hadoop分布式文件系统中。 Hive本身没有特殊的数据存储格式或索引数据。 您只需要在创建表时告诉Hive列分隔符和行分隔符,Hive就可以解析数据 ** 。 因此,将数据导入到Hive表中只是将数据移动到表所在的目录**(如果数据在HDFS中)。 但是,如果数据位于本地文件系统中,则需要将数据复制到表所在的目录中。


二、Hive Architecture

Hive架构定义了Hive接收查询、编译查询、优化查询以及在Hadoop上执行查询的方式。

在这里插入图片描述

(一)Hive Clients

Hive允许使用多种语言编写应用程序,包括Java、Python和c++。 它支持不同类型的客户端,例如:

1.CLI (Command Line Interface)

CLI (command Line Interface,命令行接口):允许用户直接从终端运行 Hive查询。
Allows users to run Hive queries directly from the terminal.

2.Web UI

基于浏览器的界面与Hive交互。
Browser-based interface interact with Hive.

3.JDBC/ODBC drivers

JDBC/ODBC驱动——使应用程序(如Java、BI工具如Tableau/Power BI)能够连接到Hive。

JDBC:JDBC驱动程序存在于类org.apache.hadoop.hive.jdbc.HiveDriver中。
ODBC:允许支持ODBC协议的应用程序连接到Hive。

Enable applications (e.g., Java, BI tools like Tableau/Power BI) to connect to Hive.

4.Thrift Server

使用Thrift协议提供跨语言支持(Python、c++等)。
Provides cross-language support (Python, C++, etc.) using the Thrift protocol.

(二)Hive Services

1.Hive Driver

接收来自不同来源的查询,如web UI、CLI、Thrift和JDBC/ODBC Driver。它将查询传递给编译器。

Hive Driver – It receives queries from different sources like web UI, CLI, Thrift, and JDBC/ODBC driver.It transfers the queries to the compiler.

2.Compiler

编译器的目的是解析查询并对不同的查询块和表达式执行语义分析。它将HiveQL语句转换为MapReduce任务。

The purpose of the compiler is to parse the query and perform semantic analysis on the different query blocks and expressions. It converts HiveQL statements into MapReduce jobs.

3.Execution Engine

通过向Hadoop提交作业来执行优化的查询计划(任务的DAG)。它管理执行顺序和依赖关系。执行引擎也适用于Tez或Spark(不仅仅是MapReduce)。

Executes the optimized query plan (DAG of tasks) by submitting jobs to Hadoop. It manages the execution order and dependencies.

4.MetaStore

元数据存储——存储元数据(表模式、分区、列类型、表位置等)。MetaStore专门在RDBMS(如MySQL/Derby) 中保存元数据。

MetaStore – Stores metadata (table schemas, partitions, column types, table locations, etc.).MetaStore specifically holds metadata in an RDBMS (like MySQL/Derby).

元数据与计算层

读写数据

JDBC/ODBC/Thrift协议

调用

读写表结构

提交任务

处理引擎层 - Hive 大脑

Hive Driver
包含:
解析器 -> 编译器 -> 优化器 -> 执行器

核心服务层 - Hive 服务进程

HiveServer2
提供Thrift/JDBC/ODBC接口

客户端层 - 用户交互界面

Beeline
CLI

JDBC/ODBC
应用程序
如DBeaver, Tableau

Web UI
如Hue

Metastore
元数据库

计算引擎
如 MapReduce, Tez, Spark

存储层
如 HDFS, S3


三、Hive Data Models

在这里插入图片描述
Hive是一个建立在Hadoop之上的开源数据仓库系统,用于查询和分析存储在Hadoop文件中的大型数据集。 它在Hadoop中处理结构化和半结构化数据。 Apache Hive中的数据可以分为:

(一)tables

1.concepts

Apache Hive表类似于关系数据库表。 Hive的表是由数据组成的,它们的布局是通过关联的元数据来描述的。 可以在这些表上执行筛选、连接、项目和联合操作。

Apache Hive tables are similar to relational database tables(RDBMS). Tables of Hive consist of data and are their layout is described with the help of associated metadata. Filter, join, project and union operations can be performed on these tables

2.types

通常,在Hadoop中,数据存储在HDFS中,但Hive将元数据存储在关系数据库(Metadata stored in Metastore.) 而不是HDFS中。 Hive中存在两种类型的表:

  1. Managed/internal table
  2. External Tables

(二)partitions

1.definition

Hive的表是通过将相同类型的数据根据任意列或分区键分组在一起来进行分区组织的。 每个表都有一个分区键用于标识。 分区可以加快查询和切片过程

Table of Hive is organized in partitions by grouping same types of data together based on any column or partition key. Every table has a partition key for identification. Partitions can speed up query and slicing process.

simple definition:分区即为基于列值对表的逻辑划分(例如,year=2023, region=Asia)。

创建分区的语法如下:

create Table tablename(var String,var Int) Partitioned BY (partition1 String,partition2 String);

2.advantages

分区通过减少查询的延迟来提高查询的速度,因为它只扫描相关数据而不是扫描完整的数据。

Partitioning increases the speed of querying by reducing its latency as it only scans relevant data rather than scanning full data.

(三)buckets

1.definition

使用列上的散列函数将分区(或表)进一步细分为固定大小的文件。

Further subdivision of partitions (or tables) into fixed-size files using a hash function on a column.

创建分桶的语法如下:

create Table tablename Partitioned BY (partition1 data_type, partition2 data_type ) Clustered BY (column1, column2) Sorted BY(column_name Asc:Desc,___) Into num_buckets Buckets;

2.advantage

对于采样和加速关联(因为匹配的数据进入同一个桶)很有用。

Useful for sampling and speeds up joins (as matching data goes to the same bucket).


四、Hive Data Types

Hive中的Data Types指Hive表中的列类型。 Hive Data类型分为两类:原始数据类型和复杂数据类型。

在这里插入图片描述

(一) Primitive Data Types

Hive支持多种大小的整数和浮点类型、布尔类型和任意长度的字符串。 Hive v0.8.0增加了时间戳和二进制字段的类型。

Hive是用Java开发的,Hive中的基本数据类型除了字符串类型外,与Java中的基本数据类型相对应。 有符号整数类型:TINYINT、SMALLINT、INT和BIGINT分别相当于java的byte、short、INT和long数据类型。 它们分别是1字节、2字节、4字节和8字节有符号整数。

Hive的浮点数据类型FLOATDOUBLE对应于Java的基本类型FLOATDOUBLE

hive BOOLEAN类型相当于java的基本数据类型BOOLEAN

在这里插入图片描述

分类 具体类型 中文描述(含关键点英文标注) 示例
整数类型
(Integer Types)
TINYINT 1字节整数,范围-128到127(1-byte signed integer, from -128 to 127) TINYINT
示例:10
SMALLINT 2字节整数,范围-32,768到32,767(2-byte signed integer, from -32768 to 32767) SMALLINT
示例:1000
INT 4字节整数,范围-2³¹到2³¹-1(4-byte signed integer, from -2³¹ to 2³¹-1) INT
示例:1000000
BIGINT 8字节整数,范围-2⁶³到2⁶³-1(8-byte signed integer, from -2⁶³ to 2⁶³-1) BIGINT
示例:10000000000
浮点类型
(Floating-point Types)
FLOAT 4字节单精度浮点数,近似数值(4-byte single precision floating point number, approximate numeric) FLOAT
示例:3.14159
DOUBLE 8字节双精度浮点数,近似数值(8-byte double precision floating point number, approximate numeric) DOUBLE
示例:3.141592653589793
布尔类型
(BOOLEAN)
BOOLEAN 逻辑真/假值,true/false(Logical true/false value) BOOLEAN
示例:true, false
字符类型
(Character Data)
STRING 可变长度字符串,无最大长度限制(Variable-length character string, no maximum length limit) STRING
示例:'Hello Hive'
CHAR(n) 固定长度字符串,长度n(1≤n≤255)(Fixed-length character string, length n (1≤n≤255)) CHAR(10)
示例:'Hive'(实际存储为’Hive ',填充空格)
VARCHAR(n) 可变长度字符串,最大长度n(1≤n≤65535)(Variable-length character string, maximum length n (1≤n≤65535)) VARCHAR(100)
示例:'Hello'(按实际长度存储)
时间与二进制类型
(Time and Binary Values)
TIMESTAMP 时间戳,精度到纳秒,从UNIX纪元(1970-01-01 00:00:00 UTC)开始(Timestamp with nanosecond precision, from UNIX epoch) TIMESTAMP
示例:'2023-10-01 12:30:45.123456789'
BINARY 字节数组,用于存储二进制数据(Byte array, for storing binary data) BINARY
示例:'binary_data'(实际存储为字节序列)

(二) Complex Data Types

Hive支持的列类型包括结构、映射、联合和数组。
在这里插入图片描述

类型 描述 语法 更正后的正确示例(仅供参考)
ARRAY 有序的字段集合,字段类型必须相同(ordered set of fields, the field types must be same) ARRAY<元素类型> ARRAY<TINYINT>
例如:array(1, 2)[1, 2]
MAP 无序的键值对集合。键的类型必须是原子类型,值可以是任意类型。同一个映射中,所有键的类型必须相同,所有值的类型也必须相同(An unordered set of keys. The type of the key must be atomic, the values can be any type, the type of the keys must the same for the same mapping, and the type of the values must be the same too.) MAP<键类型, 值类型> MAP<STRING, INT>
例如:map('a', 1, 'b', 2){'a':1, 'b':2}
STRUCT 一组命名的字段,字段类型可以不同(A set of named fields, the field types can be different) STRUCT<字段名1 : 类型1, 字段名2 : 类型2, ...> STRUCT<first: SMALLINT, second: FLOAT, third: STRING>
例如:named_struct('first', 10, 'second', 3.14, 'third', 'hello')
UNION 复杂数据类型,一次只能持有其可能数据类型中的一种(It is a Complex Data Type that can hold One of its Possible Data Types at Once.) UNIONTYPE<类型1, 类型2, ...> UNIONTYPE<INT, FLOAT, STRING>
示例值1(INT):create_union(0, 1, null, null)
示例值2(STRING):create_union(2, null, null, ‘a’)

五、Introduction to HiveServer2 (HS2)

(一)Concepts

HiveServer2服务允许多个客户端通过JDBC/ODBC/Thrift协议并发地连接到Hive。 HiveServer2是HiveServer1的继承者,HS1不能处理来自多个客户端的并发请求,所以HiveServer1已被弃用。

HiveServer2 (HS2) is a service that enables clients to execute queries against Hive. HiveServer2 is the successor to HiveServer1 which has been deprecated. HS2 supports multi-client concurrency and authentication. It is designed to provide better support for open API clients like JDBC and ODBC.

HS2支持多客户机并发性和身份验证。 它旨在为JDBC和ODBC等开放API客户机提供更好的支持。 HS2是作为复合服务运行的单个进程,它包括基于thrift的Hive服务(TCP或HTTP)和用于web UI的Jetty web服务器。

HS2 is a single process running as a composite service, which includes the Thrift-based Hive service (TCP or HTTP) and a Jetty web server for web UI.

(二)HS2 Architecture

1.Introduction

基于thrift的Hive服务是HS2的核心,负责为Hive查询提供服务(例如,来自Beeline)。 Thrift是一个用于构建跨平台服务的RPC框架。 它的堆栈由4层组成:服务器、传输、协议和处理器。

The Thrift-based Hive service is the core of HS2 and responsible for servicing the Hive queries (e.g., from Beeline). Thrift is an RPC framework for building cross-platform services. Its stack consists of 4 layers:Server, Transport, Protocol, and Processor.

在这里插入图片描述

2.related commands

#Start HS2:
hive --service hiveserver2
#Connect via Beeline:我们可以从各种客户端(如SQLine, Beeline或Squirrel或Web Interface)启动HiveServer2的客户端服务。
beeline> !connect jdbc:hive2://localhost:10000/default
Enter username for jdbc:hive2://localhost:10000: root
Enter password for jdbc:hive2://localhost:10000: ********

总结

Apache Hive 是一个开源的数据仓库软件,用于在 Hadoop(特别是 HDFS)或其他存储系统上高效管理、查询大规模结构化和半结构化数据。它支持多种编程语言(如 Java、Python)来开发用户自定义函数(UDF、UDAF、UDTF),并可通过不同客户端(Thrift Server、JDBC/ODBC Driver)进行访问。Hive 的数据组织采用 Table、Partition、Bucket 层次结构,支持原始和复杂数据类型。HiveServer2 (HS2) 是其核心服务,基于 Thrift 协议,负责处理查询并连接 Metastore 和 Hadoop 集群。

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐