Apache Hive:基于Hadoop的分布式數據倉庫
Apache Hive 是一個基于 Apache Hadoop 構建的開源分布式數據倉庫系統,支持使用 SQL 執行 PB 級大規模數據分析與查詢。
主要功能
Apache Hive 提供的主要功能如下。
HiveServer2
HiveServer2 服務用于支持接收客戶端連接和查詢請求。
HiveServer2 支持多客戶端并發和身份驗證,基于 Thrift RPC 實現,允許客戶端使用 JDBC、ODBC 等連接方式。以下是一個使用 Beeline 客戶端工具連接 Apache Hive 的示例:
beeline -u "jdbc:hive2://host:10001/default"
Connected to: Apache Hive
jdbc:hive2://host:10001/>select count(*) from test_t1;
HiveServer2 服務同時還包含了一個基于 Jetty 的網站服務,用于提供 Web 瀏覽器訪問方式。
Hive Metastore
Hive Metastore(HMS)提供了一個管理元數據的集中式資料庫,并且通過 API 服務提供客戶端查詢。
Hive Metastore 已經成為了構建數據湖的核心基礎模塊,這些數據湖充分融合了包括 Apache Spark 和 Presto 在內的多樣化開源生態系統。
ACID
對于 Apache ORC 格式的數據表,Apache Hive 提供了完整的 ACID 事務支持;對其他所有數據格式,僅支持追加(Insert-Only)操作。
數據壓縮
Apache Hive 的數據壓縮(Data Compaction)是針對支持 ACID 事務的表(通常是 ORC 格式表)的優化機制,用于提高查詢性能并減少存儲開銷。例如:
jdbc:hive2://> alter table test_t1 compact "MAJOR";
Done!
jdbc:hive2://> alter table test_t1 compact "MINOR";
Done!
jdbc:hive2://> show compactions;
Iceberg集成
Apache Hive 提供了 Apache Iceberg 數據表的原生支持,用戶可以直接通過 Hive 的 SQL 接口創建、管理和查詢 Iceberg 表,而無需依賴外部工具或復雜配置。
低延遲分析處理
Apache Hive 通過低延遲分析處理(LLAP,Low Latency Analytical Processing)實現交互式與亞秒級 SQL 查詢。
Apache Hive LLAP 通過持久化服務與智能緩存填補了傳統 Hive 在實時分析場景的短板,使其能夠兼顧高吞吐批處理與低延遲交互查詢。
查詢優化
Apache Hive 利用 Apache Calcite 框架提供的基于成本優化(CBO)方式實現 SQL 查詢的性能優化。
以下是一個使用 EXPLAIN 命令獲取執行計劃的示例:
jdbc:hive2://> explain cbo select ss.ss_net_profit, sr.sr_net_loss from store_sales ss join store_returns sr on (ss.ss_item_sk=sr.sr_item_sk) limit 5;
+---------------------------------------------+
Explain
+---------------------------------------------+
CBO PLAN:
HiveSortLimit(fetch=[5])
HiveProject(ss_net_profit=[$1], sr_net_loss=[$3])
??HiveJoin(condition=[=($0, $2)], joinType=[inner])
????HiveProject(ss_item_sk=[$2], ss_net_profit=[$22])
????HiveFilter(condition=[IS NOT NULL($2)])
??????HiveTableScan(table=[[tpcds_text_10, store_sales]], table:alias=[ss])
????HiveProject(sr_item_sk=[$2], sr_net_loss=[$19])
????HiveFilter(condition=[IS NOT NULL($2)])
??????HiveTableScan(table=[[tpcds_text_10, store_returns]], table:alias=[sr])
+---------------------------------------------+
數據復制
Apache Hive 的引導式復制(Bootstrap Replication)和增量復制(Incremental Replication)實現了高效數據備份與恢復。
jdbc:hive2://> repl dump src with (
.. .>'hive.repl.dump.version'='2',
.. .>'hive.repl.rootdir'='hdfs://<host>:<port>/user/replDir/d1'
.. .>);
Done!
jdbc:hive2://> repl load src into tgt with (
.. .>'hive.repl.rootdir'='hdfs://<host>:<port>/user/replDir/d1'
.. .>);
Done!
快速試用
接下來我們使用 Docker 快速體驗 Apache Hive。
首先,獲取最新的鏡像:
docker pull apache/hive:4.0.1
然后設置版本變量:
export HIVE_VERSION=4.0.1
啟動 HiveServer2 服務,使用嵌入式 Derby 數據庫作為元數據存儲:
docker run -d -p 10000:10000 -p 10002:10002 --env SERVICE_NAME=hiveserver2 --name hive4 apache/hive:${HIVE_VERSION}
注意,這種方式在服務關閉時會丟棄所有的數據;如果想要持久存儲數據表,可以使用外部數據庫和存儲。
接下來利用 Beeline 客戶端連接數據庫:
docker exec -it hive4 beeline -u 'jdbc:hive2://localhost:10000/'
或者也可以通過瀏覽器進行訪問:http://localhost:10002/
在 Beeline 客戶端中執行以下 SQL 語句:
show tables;
createtable hive_example(a string, b int) partitioned by(c int);
altertable hive_example addpartition(c=1);
insertinto hive_example partition(c=1)values('a',1),('a',2),('b',3);
selectcount(distinct a)from hive_example;
selectsum(b)from hive_example;