hive 面试题

使用 Hive或者自定义 MR 实现如下逻辑
product_no lac_id moment start_time user_id county_id staytime city_id
13429100031 22554 8 2013-03-11 08:55:19.151754088 571 571 282 571
13429100082 22540 8 2013-03-11 08:58:20.152622488 571 571 270 571
13429100082 22691 8 2013-03-11 08:56:37.149593624 571 571 103 571
13429100087 22705 8 2013-03-11 08:56:51.139539816 571 571 220 571
13429100087 22540 8 2013-03-11 08:55:45.150276800 571 571 66 571
13429100082 22540 8 2013-03-11 08:55:38.140225200 571 571 133 571
13429100140 26642 9 2013-03-11 09:02:19.151754088 571 571 18 571
13429100082 22691 8 2013-03-11 08:57:32.151754088 571 571 287 571
13429100189 22558 8 2013-03-11 08:56:24.139539816 571 571 48 571
13429100349 22503 8 2013-03-11 08:54:30.152622440 571 571 211 571
字段解释:
product_no:用户手机号;
lac_id:用户所在基站;
start_time:用户在此基站的开始时间;
staytime:用户在此基站的逗留时间。

需求描述:

根据 lac_id和 start_time知道用户当时的位置,根据 staytime知道用户各个基站的逗留时长。根据轨迹合
并连续基站的 staytime。
最终得到每一个用户按时间排序在每一个基站驻留时长

期望输出举例:
13429100082 22540 8 2013-03-11 08:58:20.152622488 571 571 270 571
13429100082 22691 8 2013-03-11 08:56:37.149593624 571 571 390 571
13429100082 22540 8 2013-03-11 08:55:38.140225200 571 571 133 571
13429100087 22705 8 2013-03-11 08:56:51.139539816 571 571 220 571
13429100087 22540 8 2013-03-11 08:55:45.150276800 571 571 66 571

hive实现:

select t.p,t.l,t.start,t.stay from (select b.product_no p,b.lac_id l,b.start_time start,b.staytime stay from net_time_sum a right outer join net_time b on a.product_no=b.product_no and a.lac_id = b.lac_id where a.product_no is  NULL union all select t1.p,t1.l,t1.start,t1.stay from (select c.product_no p,c.lac_id l,c.start_time start,c.staytime+d.staytime stay from net_time c left outer join net_time d on c.product_no=d.product_no and c.lac_id=d.lac_id where unix_timestamp(c.start_time)+c.staytime>unix_timestamp(d.start_time) and c.start_time<d.start_time)t1)t order by t.p,t.start desc;

MR实现:

思路为:

1、第一次mr

按照product_no lac_id 分组,然后对相同的分组,values进行排序,如果第一次的记录start_time+staytime >第二次start_time 并且 1.start_time<2.start_time 则进行合并

2、第二次mr

实现对第一次的输出进行按照start_time 进行排序

原文地址:https://www.cnblogs.com/ggbond1988/p/5037704.html