一、Hive Lateral View
- 1.Lateral View用于和UDTF函數(explode、split)結合來使用。
- 2.首先通過UDTF函數拆分成多行,再将多行結果組合成一個支援别名的虛拟表。
- 3.主要解決在select使用UDTF做查詢過程中,查詢隻能包含單個UDTF,不能包含其他字段、以及多個UDTF的問題
文法:
select explode(likes) from psn2;指令可将likes數組拆分。explode拆分,隻能接一個字段。
案例:
統計人員表中一共有多少種愛好、城市?
從psn2表中查詢,并将結果輸入到虛拟表中:
拆分likes數組,定義别名,結果放在myCol1中。
拆分address集合,定義别名,k、v分别放在myCol2和myCol3中
select count(distinct(myCol1)), count(distinct(myCol2)) from psn2
LATERAL VIEW explode(likes) myTable1 AS myCol1
LATERAL VIEW explode(address) myTable2 AS myCol2, myCol3;
二、Hive視圖
特點:
- 1.不支援物化視圖
- 2.隻能查詢,不能做加載資料操作(寫入)
- 3.視圖的建立,隻是儲存一份中繼資料,查詢視圖時才執行對應的子查詢
- 4.view定義中若包含了ORDER BY/LIMIT語句,當查詢視圖時也進行ORDER BY/LIMIT語句操作,view當中定義的優先級更高
- 5.view支援疊代視圖
建立視圖文法:
CREATE VIEW [IF NOT EXISTS] [db_name.]view_name
[(column_name [COMMENT column_comment], ...) ]
[COMMENT view_comment]
[TBLPROPERTIES (property_name = property_value, ...)]
AS SELECT ... ;
查詢視圖:
删除視圖:
三、Hive索引
解決查詢問題,避免全表掃描,提高檢索的性能。索引具體的添加,要根據資料來定。
1.建立索引:
as:指定索引器;
in table:指定索引表,若不指定預設生成在default__psn2_t1_index__表中
create index t1_index on table psn2(name)
as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' with deferred rebuild
in table t1_index_table;
或者可以這樣寫,不指定索引表,即預設生成default__psn2_t1_index__:
create index t2_index on table psn2(name)
as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' with deferred rebuild;
此時索引表中都是空的,還要生成索引資訊。
2.重建索引:
建立索引之後,必須重建索引才能生效。
索引表查詢結果:
hive> select * from t1_index_table;
OK
t1_index_table.name t1_index_table._bucketname t1_index_table._offsets t1_index_table.age
小明1 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [0] 10
小明2 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [56] 10
小明3 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [112] 10
小明4 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [168] 10
小明5 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [224] 10
小明6 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [275] 10
小明7 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [331] 10
小明8 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [381] 10
小明9 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [431] 10
張三1 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [0] 20
張三2 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [58] 20
張三3 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [116] 20
張三4 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [174] 20
張三5 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [232] 20
張三6 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [285] 20
張三7 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [343] 20
張三8 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [395] 20
張三9 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [447] 20
Time taken: 0.127 seconds, Fetched: 18 row(s)
有了索引,查詢才會更快:
hive> select * from psn2 where name='小明2';
OK
psn2.id psn2.name psn2.likes psn2.address psn2.age
2 小明2 ["lol","book","movie"] {"beijing":"longze","shanghai":"pudong"} 10
Time taken: 0.227 seconds, Fetched: 1 row(s)
3.查詢索引
hive> show index on psn2;
OK
idx_name tab_name col_names idx_tab_name idx_type comment
t1_index psn2 name t1_index_table compact
t2_index psn2 name default__psn2_t2_index__ compact
Time taken: 0.146 seconds, Fetched: 2 row(s)
4.删除索引