十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
本篇內(nèi)容主要講解“Hive視圖和索引簡單介紹”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Hive視圖和索引簡單介紹”吧!
武威網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站設(shè)計等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
Hive 中的視圖和 RDBMS 中視圖的概念一致,都是一組數(shù)據(jù)的邏輯表示,本質(zhì)上就是一條 SELECT 語句的結(jié)果集。視圖是純粹的邏輯對象,沒有關(guān)聯(lián)的存儲 (Hive 3.0.0 引入的物化視圖除外),當(dāng)查詢引用視圖時,Hive 可以將視圖的定義與查詢結(jié)合起來,例如將查詢中的過濾器推送到視圖中。
不支持物化視圖
只查詢,不能做加載數(shù)據(jù)操作
視圖的創(chuàng)建,只是保存一份元數(shù)據(jù),查詢視圖才執(zhí)行對應(yīng)的子查詢
view定義中若包含了ORDER BY/LIMIT語句,當(dāng)查詢視圖時也進行了ORDER BY/LIMIT語句操作,view當(dāng)中定義的優(yōu)先級更高 ;
Hive視圖支持迭代視圖
CREATE VIEW [IF NOT EXISTS] [db_name.]view_name -- 視圖名稱 [(column_name [COMMENT column_comment], ...) ] --列名 [COMMENT view_comment] --視圖注釋 [TBLPROPERTIES (property_name = property_value, ...)] --額外信息 AS SELECT ...;
創(chuàng)建視圖注意事項
CREATE VIEW創(chuàng)建具有給定名稱的視圖。如果已經(jīng)存在具有相同名稱的表或視圖,則會引發(fā)錯誤。您可以使用IF NOT EXISTS跳過該錯誤。
刪除基表并不會刪除視圖,需要手動刪除視圖;
視圖是只讀的,不能用作LOAD / INSERT / ALTER的目標(biāo)
創(chuàng)建視圖時,如果未提供列名,則將從 SELECT 語句中自動派生列名;
一個視圖可能包含ORDER BY和LIMIT子句。如果參照查詢還包含這些條款,查詢級別子句進行評估后視圖條款(和之后在查詢的任何其它操作)。例如,如果視圖指定LIMIT 5,并且引用查詢執(zhí)行為(從v LIMIT 10中選擇*),那么最多將返回5行。
準備數(shù)據(jù)
-- 創(chuàng)建測試表create table default.user(
id string , -- 主鍵
sex string, -- 性別
name string -- 名稱);-- 導(dǎo)入數(shù)據(jù)insert into default.user (id, sex, name)values ("1","男","張三"),("2","女","小花"),("3","男","趙柳"),("4","男","李嘿嘿");創(chuàng)建一個測試視圖
hive (default)> create view if not exists default.user_view as select * from default.user;OK id sex nameTime taken: 0.181 seconds
-- 查詢視圖內(nèi)容呢select * from default.user_view;-- 查詢視圖結(jié)構(gòu)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;
語法:
ALTER VIEW [db_name.]view_name SET TBLPROPERTIES table_properties; table_properties: : (property_name = property_value, property_name = property_value, ...)
示例:
alter view default.user_view set tblproperties ('name'='DSJLG','GZH'='DSJLG')通過 desc formatted default.user_view;詳情信息

Hive 在 0.7.0 引入了索引的功能,索引的設(shè)計目標(biāo)是提高表某些列的查詢速度。如果沒有索引,帶有謂詞的查詢(如’WHERE table1.column = 10’)會加載整個表或分區(qū)并處理所有行。但是如果 column 存在索引,則只需要加載和處理文件的一部分。
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"]; --索引注釋
我們在使用之前上面創(chuàng)建好的user表對id字段創(chuàng)建名字為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;
此時索引表中是沒有數(shù)據(jù)的,需要重建索引才會有索引的數(shù)據(jù)。
hive (default)> ALTER index user_index on user rebuild ;Query ID = root_20201015081313_879ce697-a6a4-4c38-a1a9-0e72a52feb6b Total jobs = 1Launching Job 1 out of 1Number of reduce tasks not specified. Estimated from input data size: 1In order to change the average load for a reducer (in bytes): set hive.exec.reducers.bytes.per.reducer=In order to limit the maximum number of reducers: set hive.exec.reducers.max= In order to set a constant number of reducers: set mapreduce.job.reduces= Starting Job = job_1602711568359_0002, Tracking URL = http://node01:8088/proxy/application_1602711568359_0002/Kill Command = /export/servers/hadoop-2.6.0-cdh6.14.0/bin/hadoop job -kill job_1602711568359_0002 Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 12020-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 sec2020-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_tableTable 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 OKTime taken: 12.85 seconds
Hive 會啟動 MapReduce 作業(yè)去建立索引,建立好后查看索引表數(shù)據(jù)如下。三個表字段分別代表:索引列的值、該值對應(yīng)的 HDFS 文件路徑、該值在文件中的偏移量。
hive (default)> select * from user_index_table; OK user_index_table.id user_index_table._bucketname user_index_table._offsets1 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 在查詢時候是不會自動去使用索引的,需要開啟相關(guān)配置。開啟配置后,涉及到索引列的查詢就會使用索引功能去優(yōu)化查詢。
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;
刪除索引會刪除對應(yīng)的索引表。
DROP INDEX [IF EXISTS] index_name ON table_name;
如果存在索引的表被刪除了,其對應(yīng)的索引和索引表都會被刪除。如果被索引表的某個分區(qū)被刪除了,那么分區(qū)對應(yīng)的分區(qū)索引也會被刪除。
在指定列上建立索引,會產(chǎn)生一張索引表(Hive的一張物理表),里面字段包括:索引列的值、該值對應(yīng)的 HDFS 文件路徑、該值在文件中的偏移量。
在執(zhí)行索引字段查詢時候,首先額外生成一個MapReduce job,根據(jù)對索引列的過濾條件,從索引表中過濾出索引列的值對應(yīng)的hdfs文件路徑及偏移量,輸出到hdfs上的一個文件中,然后根據(jù)這些文件中的hdfs路徑和偏移量,篩選原始input文件,生成新的split,作為整個job的split,這樣就達到不用全表掃描的目的。
到此,相信大家對“Hive視圖和索引簡單介紹”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!