问题
假如我有一个评分表a 里面的comment字段,里面存放的是每个ask_id的评分和评价标签,如下表所示:
ask_id | score | comment |
---|---|---|
1704143096503118720 | 1 | 0,2,4,6,7 |
我现在想将每个comment的标签一行拆解为多行,在hive和presto中,我该怎么做?
方法
hive
hive中的解决办法:lateral view explode(comment) b as new_comment
注意:b是新创建的爆炸表,new_comment是新表的存放的拆解字段。
代码:
select
a.comment,b.new_comment
from
a lateral view explode(comment) b as new_comment
presto
presto中的解决办法是cross join unnest(comment) as b(new_comment)
注意:(1)b是爆炸表名称,new_comment是爆炸的新字段
(2)有可能comment需要更改为split(comment,',')
代码:
select
a.ask_id,a.score,b.new_comment
from
a
cross join unnest(split(comment,',')) as b(new_comment)