hive行轉列,列轉行


 

實例一:來源:

https://www.cnblogs.com/kimbo/p/6208973.html

行轉列 (對某列拆分,一列拆多行)
使用函數:lateral view explode(split(column, ',')) num
eg: 如表:t_row_to_column_tmp 數據如下,對tag列進行拆分
結果:
轉行 (根據主鍵,進行多行合並一列)
使用函數:concat_ws(',',collect_set(column))  
說明:collect_list 不去重,collect_set 去重。 column 的數據類型要求是 string
eg:如表:t_column_to_row ,根據id,對tag_new 進行合並

 

 實例二:

表合並,行轉列

collect_set這就用到了hive中的行轉列的知識,需要用到兩個內置UDF: collect_set, concat_ws,
建表:
create table user_basic_info(id string, name string);
create table user_address(name string, address string);
 
加載數據:
load data local inpath '/home/jthink/work/workspace/hive/row_col_tran/data1' into table user_basic_info;
load data local inpath '/home/jthink/work/workspace/hive/row_col_tran/data2' into table user_address;
 
執行合並:
select max(ubi.id), ubi.name, concat_ws(',', collect_set(ua.address)) as address from user_basic_info ubi join user_address ua on ubi.name=ua.name group by ubi.name;
 

表拆分,列轉行

 

 

 

 

 


免責聲明!

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



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