hive的行列转换

行转列(把多个行合并)

比如把:

id    tag

1  12

1  23

2  67

2   78

2  76

行转列之后:

id  tag

1  12,23

2  67,78,76

使用函数为:concat_ws(',',collect_set(column))  // 中间用 ',' 号隔开 

说明:collect_list 不去重,collect_set 去重。 column 的数据类型要求是 string

例:

  select id concat_ws(',',collect_set(tag)) as tag_col from test group by id;

列转行(把一列,分开为几行)

转换之前:

id  tag

1  12,23

2  67,78,76

转换之后:

id    tag

1  12

1  23

2    67

2    78

2  76

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

说明:按 ',' 号分割

例:

  select id tag_new from test lateral view explode(split(tag,',')) num as tag_new

 

posted @ 2018-10-13 20:22  董秀才  阅读(1446)  评论(0编辑  收藏  举报