一行轉多行 及多行轉一行的 hive語句


 
注意 :|,; 是特殊符號,要用 "\\|", "\\;"來表示。
 
一行轉多行
usertags 里面有很多項,每項之間以逗號分隔
 
create table tag_count2 as
select tag,count(gid) from (
select gid,tag from (select b.gid ,b.usertags from zhangb.gid_tags b group by b.gid,b.usertags) a
lateral view explode(split(a.usertags, ',')) t as tag
) p where substr(p.gid,1,7)='ANDROID' group by p.tag;
 
# 其中的 t 是把a表中的usertags 由一行變成多行后形成的新表的別稱,是表的名稱
 
導出數據:
insert overwrite local directory '/home/zhangb/tag_count'row format delimited fields terminated by '\t'
select distinct * from zhangb.tag_count
 
原格式:
gid1    abc,abd,abf
gid2    abc,snm
...
處理后輸出格式:
gid1   abc
gid1   abd
gid1   abf
gid2   abc
gid2  snm
...
 
 
***多行轉一行
1、演示多列轉為單行
數據文件及內容: student.txt
xiaoming|english|92.0
xiaoming|chinese|98.0
xiaoming|math|89.5
huahua|chinese|80.0
huahua|math|89.5

創建表studnet:
create table student(name string,subject string,score decimal(4,1))
row format delimited
fields terminated by '|';

導入數據:
load data local inpath '/home/hadoop/hivetestdata/student.txt' into table student;


列轉為行演示:
hive (hive)> select name,concat_ws(',',collect_set(subject)) from student group by name;
huahua chinese,math
xiaoming english,chinese,math


hive (hive)> select name,concat_ws(',',collect_set(concat(subject,'=',score))) from student group by name;
huahua chinese=80,math=89.5
xiaoming english=92,chinese=98,math=89.5


免責聲明!

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



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