咱們在寫HQL有沒有遇到過數據量特別大的時候好比,使用HQL 處理起來很是複雜,很是慢,這時候咱們可使用Hive給加個索引來提升咱們的速度。多了就不說了,咱們直接開始。node
Hive 中的視圖和 RDBMS 中視圖的概念一致,都是一組數據的邏輯表示,本質上就是一條 SELECT
語句的結果集。視圖是純粹的邏輯對象,沒有關聯的存儲 (Hive 3.0.0 引入的物化視圖除外),當查詢引用視圖
時,Hive 能夠將視圖的定義與查詢結合起來,例如將查詢中的過濾器推送到視圖
中。git
一份元數據
,查詢視圖才執行對應的子查詢CREATE VIEW [IF NOT EXISTS] [db_name.]view_name -- 視圖名稱
[(column_name [COMMENT column_comment], ...) ] --列名
[COMMENT view_comment] --視圖註釋
[TBLPROPERTIES (property_name = property_value, ...)] --額外信息
AS SELECT ...;
建立視圖注意事項程序員
IF NOT EXISTS
跳過該錯誤。LOAD
/ INSERT
/ ALTER
的目標準備數據github
-- 建立測試表
create table default.user(
id string , -- 主鍵
sex string, -- 性別
name string -- 名稱
);
-- 導入數據
insert into default.user (id, sex, name)
values ("1","男","張三"),("2","女","小花"),("3","男","趙柳"),("4","男","李嘿嘿");
建立一個測試視圖面試
hive (default)> create view if not exists default.user_view as select * from default.user;
OK
id sex name
Time taken: 0.181 seconds
-- 查詢視圖內容呢
select * from default.user_view;
-- 查詢視圖結構
desc default.user_view;
-- 查詢視圖詳細信息
desc formatted default.user_view;
-- 查詢視圖 沒有指定的方式跟查詢全部表同樣
show tables;
-- 模板
DROP VIEW [IF EXISTS] [db_name.]view_name;
-- 刪除視圖
DROP VIEW IF EXISTS user_view;
語法:apache
ALTER VIEW [db_name.]view_name SET TBLPROPERTIES table_properties;
table_properties:
: (property_name = property_value, property_name = property_value, ...)
示例:app
alter view default.user_view set tblproperties ('name'='DSJLG','GZH'='DSJLG')
經過 desc formatted default.user_view;
詳情信息ide
Hive 在 0.7.0 引入了索引的功能,索引的設計目標是提升表某些列的查詢速度。若是沒有索引,帶有謂詞的查詢(如'WHERE table1.column = 10')會加載整個表或分區並處理全部行
。可是若是 column 存在索引,則只須要加載和處理文件的一部分
。oop
CREATE INDEX index_name --索引名稱
ON TABLE base_table_name (col_name, ...) --創建索引的列
AS index_type --索引類型
[WITH DEFERRED REBUILD] --重建索引
[IDXPROPERTIES (property_name=property_value, ...)] --索引額外屬性
[IN TABLE index_table_name] --索引表的名字
[
[ ROW FORMAT ...] STORED AS ...
| STORED BY ...
] --索引錶行分隔符 、 存儲格式
[LOCATION hdfs_path] --索引表存儲位置
[TBLPROPERTIES (...)] --索引表表屬性
[COMMENT "index comment"]; --索引註釋
咱們在使用以前上面建立好的user
表對id
字段建立名字爲user_index
,索引存儲在user_index_table
索引表中測試
create index user_index on table user(id) as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler'
with deferred rebuild in table user_index_table;
此時索引表中是沒有數據的,須要重建索引纔會有索引的數據。
hive (default)> ALTER index user_index on user rebuild ;
Query ID = root_20201015081313_879ce697-a6a4-4c38-a1a9-0e72a52feb6b
Total jobs = 1
Launching Job 1 out of 1
Number of reduce tasks not specified. Estimated from input data size: 1
In order to change the average load for a reducer (in bytes):
set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
set mapreduce.job.reduces=<number>
Starting Job = job_1602711568359_0002, Tracking URL = http://node01:8088/proxy/application_1602711568359_0002/
Kill Command = /export/servers/hadoop-2.6.0-cdh5.14.0/bin/hadoop job -kill job_1602711568359_0002
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 1
2020-10-15 08:13:47,425 Stage-1 map = 0%, reduce = 0%
2020-10-15 08:13:48,546 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 1.66 sec
2020-10-15 08:13:49,576 Stage-1 map = 100%, reduce = 100%, Cumulative CPU 2.5 sec
MapReduce Total cumulative CPU time: 2 seconds 500 msec
Ended Job = job_1602711568359_0002
Loading data to table default.user_index_table
Table default.user_index_table stats: [numFiles=1, numRows=4, totalSize=231, rawDataSize=227]
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1 Reduce: 1 Cumulative CPU: 2.5 sec HDFS Read: 12945 HDFS Write: 581944 SUCCESS
Total MapReduce CPU Time Spent: 2 seconds 500 msec
OK
Time taken: 12.85 seconds
Hive 會啓動 MapReduce
做業去創建索引,創建好後查看索引表數據以下。三個表字段分別表明:索引列的值
、該值對應的 HDFS 文件路徑
、該值在文件中的偏移量
。
hive (default)> select * from user_index_table;
OK
user_index_table.id user_index_table._bucketname user_index_table._offsets
1 hdfs://node01:8020/user/hive/warehouse/user/000000_0 [0]
2 hdfs://node01:8020/user/hive/warehouse/user/000000_0 [13]
3 hdfs://node01:8020/user/hive/warehouse/user/000000_0 [26]
4 hdfs://node01:8020/user/hive/warehouse/user/000000_0 [39]
Time taken: 0.047 seconds, Fetched: 4 row(s)
默認狀況下,雖然創建了索引,可是 Hive 在查詢時候是不會自動去使用索引
的,須要開啓相關配置
。開啓配置後,涉及到索引列的查詢就會使用索引功能去優化查詢
。
SET hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
SET hive.optimize.index.filter=true;
SET hive.optimize.index.filter.compact.minsize=0;
show index on user;
刪除索引會刪除對應的索引表。
DROP INDEX [IF EXISTS] index_name ON table_name;
若是存在索引的表被刪除了,其對應的索引和索引表都會被刪除。若是被索引表的某個分區被刪除了,那麼分區對應的分區索引也會被刪除。
在指定列
上創建索引,會產生一張索引表(Hive的一張物理表
),裏面字段包括:索引列的值
、該值對應的 HDFS 文件路徑
、該值在文件中的偏移量
。
在執行索引字段查詢時候,首先額外生成一個MapReduce job
,根據對索引列的過濾條件,從索引表中過濾出索引列的值對應的hdfs文件路徑及偏移量
,輸出到hdfs上的一個文件中,而後根據這些文件中的hdfs路徑和偏移量,篩選原始input文件
,生成新的split
,做爲整個job的split,這樣就達到不用全表掃描的目的。
今天給你們分享了Hive中經常使用的視圖和說索引,索引雖然能幫助咱們提升查詢效率和分組效率但它也有缺點的,建立好索引是沒法自動rebuild
也就意味着修改數據和添加數據都須要手動執行rebuild
。若是頻繁修改的數據就不建議使用索引
了。信本身,努力和汗水總會能獲得回報的。我是大數據老哥,咱們下期見~~~
獲取Flink面試題,Spark面試題,程序員必備軟件,hive面試題,Hadoop面試題,Docker面試題,簡歷模板等資源請去GitHub自行下載 https://github.com/lhh2002/Framework-Of-BigData