码迷,mamicode.com
首页 > 其他好文 > 详细

hive concat_w实现将多行记录合并成一行

时间:2014-11-19 20:37:53      阅读:524      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   strong   数据   

建表如下:

# 创建商品与促销活动的映射表
hive -e "set mapred.job.queue.name=pms;
set hive.exec.reducers.max=32;
set mapred.reduce.tasks=32;

drop table if exists product_promotion;
create table product_promotion(product_id bigint, promotion_id String);

insert into table product_promotion 
select p2.product_id, p2.promotion_id 
from pms.promotionv2 p1 inner join pms.promotionv2_main_product_sku p2 
on (p1.id=p2.promotion_id)
where from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss') between p1.start_date and p1.end_date;"

数据表的记录如下:

5112 960024
5112 960025
5112 960026
5112 960027
5112 960028
5113 960043
5113 960044
5113 960045
5113 960046

对promotion_id进行合并:

select product_id, concat_ws('_',collect_set(promotion_id)) as promotion_ids from product_promotion group by product_id

执行结果:

hive > select product_id, concat_ws('_',collect_set(promotion_id)) as promotion_ids from product_promotion group by product_id;
OK
5112 960024_960025_960026_960027_960028
5113 960043_960044_960045_960046
Time taken: 3.116 seconds


这里的collect_set的作用是对promotion_id去重,值得注意的是,必须保证promotion_id的类型是string类型

hive concat_w实现将多行记录合并成一行

标签:style   blog   io   ar   color   os   sp   strong   数据   

原文地址:http://blog.csdn.net/yeweiouyang/article/details/41286469

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!