工作中Hive的行列转换

hive的行列转换,个人理解就是一变多,多变一的过程。
一,行转列
在项目中遇见这样一个问题,原始数据经过处理以后是这样的
在这里插入图片描述
前者是话题ID,后面是与话题相似的前30个话题ID
因为要过滤掉用户已经接触过的话题ID,所以,需要将这个数据变成以下的格式
17796 16224
17796 17385

在hive中创建映射表

create external table category_result(topicid int,simtopicid array<string> ) 
row format delimited fields terminated by '\001' 
collection items terminated by ',' 
location '/recs/category/category_result';

行转列

create table tmp as 
select topicid,simtopic from category_result lateral view explode(simtopicid) tmp1 as  simtopic ;

结果如下:
在这里插入图片描述
再过滤掉用户已经接触过了的

create table user_recs_categroy as 
select a.userinfoid,simtopicid from 
(select userinfoid,simtopicid from user_recs_categroy_tmp1)a
left join 
(select userinfoid,topicid from recs_userRalationTopic)b
on
a.simtopicid=b.topicid where b.topicid is null;

得到下表:
在这里插入图片描述
二,列转行
对上表的结果数据进行处理得到这种类型的数据
152541 15478,15435

select userinfoid,concat_ws(',',collect_set(simtopicid)) simtopics 
from user_recs_categroy 
group by userinfoid
limit 20

结果如下
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值