Hive 行列轉換


一、列轉行 (對某列拆分,形成新列)

使用函數:lateral view explode(split(column, ',')) num

eg: 如表:t_row_to_column_tmp 數據如下,對tag列進行拆分

SQL代碼:

select id,tag,tag_new

  from t_row_to_column_tmp

lateral view explode(split(tag, ',')) num as tag_new

where id=212022894;

 

二、行轉列 (根據主鍵,對某列進行合並)

使用函數:concat_ws(',',collect_set(column))  

說明:collect_list 不去重,collect_set 去重。 column 的數據類型要求是 string

eg:如表:t_column_to_row ,根據id,對tag_new 進行合並

 

SQL代碼1:

select id,

         concat_ws(',',collect_set(tag_new)) as tag_col

 from t_column_to_row

group by id;

 

SQL代碼2:

select id,

         concat_ws(',',collect_list(tag_new)) as tag_col

 from t_column_to_row

group by id;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM