hive之表生成函式

2021-10-06 15:23:32 字數 788 閱讀 1470

表生成函式和聚合函式的感覺是相反的 表生成函式可以把單列擴充套件到多列

explode 函式

1.explode(array)

可以返回0到多行的結果,每行對應的是array陣列中的乙個元素。

2.用於map型別資料時

由於map是key-value結構的,所以它在轉換的時候會轉換成兩列,一列是key轉換而成的,一列是value轉換而成的。

explode(map_col) as (may_key_col, may_value_col)

3.侷限性:

不能關聯原有的表中的其他字段。

不能與group by、cluster by、distribute by、sort by聯用。

不能進行udtf巢狀.

4.想要解決udtf問題可以使用lateral view

select o.*, table_view.new_col

from table_origin o

lateral view udtf(expression) table_view as `new_col_1`, `new_col_2

lateral view 表示將udtf**的字段放在虛擬表中, 然後和主表table_origin進行關聯。

udtf(expression):復合邏輯規則的udtf函式,最常用的explode

table_view : 對應的虛擬表的表名

new_col: 虛擬表裡存放的有效字段

from子句後面也可以跟多個lateral view語句,使用空格間隔就可以了

Hive 之建立表

下面來繼續分享並記錄hive相關 1 建立表 1 建立表有兩個字段 2 建立表帶有分割槽 2 檢視表 檢視所有表的列表 hive show tables 通過正規表示式檢視表 hive show tables s 檢視表字段 hive describe fruits 3 更改或刪除表 1 修改表名 ...

hive函式之 條件函式

語法 if boolean testcondition,t valuetrue,t valuefalseornull 返回值 t 說明 當條件testcondition為true時,返回valuetrue 否則返回valuefalseornull hive selectif 1 2,100,200 ...

hive之建立桶表

1.建立桶表,用id進行分桶,分3個桶,行結束符用 hive create table t6 id int,name string,age int clustered by id into 3 buckets row format delimited fields terminated by 2.載...