1.hive的行列轉換

1、建表

  1. create table if not exists temp.lateral_test  
  2. (id    string,    
  3.  value string  
  4. )    
  5. ROW format delimited FIELDS TERMINATED BY ',' ;  

2、插入數據
  1. load date local inpath 'latearl.txt' overwrite into table temp.lateral_test;  
  2.   
  3. select * from lateral_test;  
數據如下:

3.1、collect_set:去重、變數組(列傳行)

  1. select id,collect_set(value) as a from temp.lateral_test group by id  

 

3.2、數組前加序號訪問對應元素,從0開始

  1. select id,  
  2.         a[0] a0,  
  3.         a[1] a1   
  4. from   
  5.     (select id,collect_set(value) as a from temp.lateral_test group by id) b  



4、利用lateral view  explode 對3.1的數據實現行轉列(k、hh別名不可少)

  1. select id,  
  2.         hh  
  3. from   
  4.     (select id,collect_set(value)as a from temp.lateral_test group by id)t  
  5. lateral view explode(a)k as hh  


PS:explode 可以把單行數組類型數據轉爲列形式:

  1. select explode(split(concat_ws(',','1','2','3','4'),','))  

---------------------------------------------------------------------------

**

Pivot using Hivemall to_map function.

**

SELECT
  uid,
  kv['c1'] AS c1,
  kv['c2'] AS c2,
  kv['c3'] AS c3
FROM (
  SELECT uid, to_map(key, value) kv
  FROM vtable
  GROUP BY uid
) t
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

uid c1 c2 c3
101 11 12 13
102 21 22 23

Unpivot

SELECT t1.uid, t2.key, t2.value
FROM htable t1
LATERAL VIEW explode (map(
  'c1', c1,
  'c2', c2,
  'c3', c3
)) t2 as key, value
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

uid key value
101 c1 11
101 c2 12
101 c3 13
102 c1 21
102 c2 22
102 c3 23


----------------------------------------------------------------------------------------------------------

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


2、演示單行轉爲多列
數據文件及內容:student2.txt
huahua|chinese=80,math=89.5
xiaoming|english=92,chinese=98,math=89.5

創建表:
create table student2(name string,subject_score_list string)
row format delimited

fields terminated by '|';

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

行轉爲列演示:
hive (hive)> select * from student2;
student2.name student2.subject_score_list
huahua             chinese=80,math=89.5
xiaoming          english=92,chinese=98,math=89.5


hive (hive)> select name, subject_list from student2 stu2 lateral view explode(split(stu2.subject_score_list,','))stu_subj as subject_list; ----別名一定不要忘記
huahua    chinese=80
huahua    math=89.5
xiaoming english=92
xiaoming chinese=98
xiaoming math=89.5


Impala的行列轉換請查看: http://blog.csdn.net/jiangshouzhuang/article/details/46809931
發佈了13 篇原創文章 · 獲贊 25 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章