hivesql: row and column conversion

1. Row to column (combine multiple rows of data into one column)

1.1 source table person_info:

name constellation blood_type
Zhang San Aries A
Li Si Sagittarius A
Wang Wu Aries B
Zhao Liu Aries A
Liu Qi Sagittarius A

1.2 Requirements: Classify people with the same constellation and blood type together, as shown in the following table:

cbt name
Archer, A Li Si, Liu Qi
Aries, A Zhang San, Zhao Liu
Aries, B Wang Wu

1.3 function:

concat(字段1,分割符,字段2) --将一行的多个字符串合并成一个字符串
concat_ws(分割符,字段1) --将一列的多个字符串按指定分隔符合并成一个字符串 group by ...
collect_set(字段) --合并的字段去重 搭配使用  concat_ws(',',collect_set(name))
collect_list(字段)  --合并的字段不去重   concat_ws(',',collect_list(name))

1.4 code:

select
cbt,
concat_ws(',',collect_list(name)) as name
from
(
  select
  concat(constellation,',',blood_type) as cbt,
  name
  from person_info
) tmp
group by cbt
;

1.5 Results display:

OK
+--------+--------+--+
|  cbt   |  name  |
+--------+--------+--+
| 射手座,A  | 李四,刘七  |
| 白羊座,A  | 张三,赵六  |
| 白羊座,B  | 王五     |
+--------+--------+--+
3 rows selected (72.361 seconds)
0: jdbc:hive2://wxt01:10000> 

2. Column to row (explode a column of data and convert it into multiple rows of data through a certain spacer)

2.1 source table movie

name category
"suspect tracking" Suspense, action, science fiction, plot
《Lie to me》 Suspense, cops, action, psychology, plot
"Wolf Warrior 2" War, action, disaster

2.2 Requirements: Expand the array data in the movie classification. The results are as follows:

《疑犯追踪》      悬疑
《疑犯追踪》      动作
《疑犯追踪》      科幻
《疑犯追踪》      剧情
《Lie to me》   悬疑
《Lie to me》   警匪

2.4 function

explode(字段) --将hive中一列中复杂的array或map结构拆分成多行(炸裂)
--使用 lateral view explode(category)

2.3 code

select
name,
category_name
from movie
lateral view explode(category) a as category_name
;

2.4 Results display

OK
+--------------+----------------+--+
|     name     | category_name  |
+--------------+----------------+--+
| 《疑犯追踪》       | 悬疑             |
| 《疑犯追踪》       | 动作             |
| 《疑犯追踪》       | 科幻             |
| 《疑犯追踪》       | 剧情             |
| 《Lie to me》  | 悬疑             |
| 《Lie to me》  | 警匪             |
| 《Lie to me》  | 动作             |
| 《Lie to me》  | 心理             |
| 《Lie to me》  | 剧情             |
| 《战狼2| 战争             |
| 《战狼2| 动作             |
| 《战狼2| 灾难             |
+--------------+----------------+--+
12 rows selected (0.393 seconds)
0: jdbc:hive2://wxt01:10000> 

This article refers to www.51doit.cn

Guess you like

Origin blog.csdn.net/weixin_47699191/article/details/114702834