Hive
explode+lateral
group by+collect_list
一、列轉(zhuǎn)行 (對(duì)某列拆分,形成新列)
使用函數(shù):lateral view explode(split(column, ‘,’)) num
eg: 如表:t_row_to_column_tmp 數(shù)據(jù)如下,對(duì)tag列進(jìn)行拆分
select id,tag,tag_new
from t_row_to_column_tmp
lateral view explode(split(tag, ',')) num as tag_new
where id=212022894;
二、行轉(zhuǎn)列 (根據(jù)主鍵,對(duì)某列進(jìn)行合并)
使用函數(shù):concat_ws(‘,’,collect_set(column))
說(shuō)明:collect_list 不去重,collect_set 去重。 column 的數(shù)據(jù)類(lèi)型要求是 string
eg:如表:t_column_to_row ,根據(jù)id,對(duì)tag_new 進(jìn)行合并
select id,
concat_ws(',',collect_set(tag_new)) as tag_col
from t_column_to_row
group by id;
select id,
concat_ws(',',collect_list(tag_new)) as tag_col
from t_column_to_row
group by id;
Impala
Impala 不支持 hive COLLECT_SET函數(shù)的方式,使用GROUP_CONCAT函數(shù)+SPLIT_PART函數(shù)替代
## IMPALA
SELECT SCORE,SPLIT_PART(GROUP_CONCAT(NAME),',',1) FROM TEST.STUDENT GROUP BY SCORE
## HIVE
SELECT SCORE,COLLECT_SET(NAME)[0] FROM TEST.STUDENT GROUP BY SCORE
例子:
select
a.zhusulvguan,group_concat(b.peopleid,',') ,group_concat(b.peopleid2,',')
from
table_lvguan a join table_people b on b.zhusulvguanId = a.zhusulvguanId
where
group by a.zhusulvguan
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-517485.html
select
a.zhusulvguan,concat(group_concat(b.peopleid,','),',',group_concat(b.peopleid2,',')) peopleidall
from
table_lvguan a join table_people b on b.zhusulvguanId = a.zhusulvguanId
where group by a.zhusulvguan
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-517485.html
總結(jié):
①group_concat(column[,char])函數(shù):把同組中指定的column放到一行中[通過(guò)char連接],并且去重(列名去重,值不去重)。
②既然是同組數(shù)據(jù)的操作,那么group_concat()就要配合group by使用。特別的,group by 分組依據(jù)并不強(qiáng)制要求和group_concat(column)指定的column相同(個(gè)數(shù),字段名)。
③concat(column1,‘cahr’,column2):column1和column2的值通過(guò)cahr連接后合并
到了這里,關(guān)于Hive和Impala的行列轉(zhuǎn)換的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!