简体   繁体   English

如何在 Hive 中使用递归查询

[英]How to use the recursive query in Hive

It has blank data.它有空白数据。

ID          Page            Timestamp   Sequence
Orestes     Login           152356      1
Orestes     Account view    152368  
Orestes     Transfer        152380  
Orestes     Account view    162382      2 
Orestes     Loan            162393  
Antigone    Login           152382      1
Antigone    Transfer        152390

I wanna change it like below.我想像下面那样改变它。

ID          Page            Timestamp   Sequence
Orestes     Login           152356      1
Orestes     Account view    152368      1
Orestes     Transfer        152380      1
Orestes     Account view    162382      2 
Orestes     Loan            162393      2
Antigone    Login           152382      1
Antigone    Transfer        152390      1

I have tried...我试过了...

with r1
as
(select id, page, timestamp, lag(sequence) over (partition id order by timestamp) as sequence from log)
r2
as
(select id, page, timestamp, sequence from log)
insert into test1
select a.id, a.page, a.timestamp, case when a.sequence is not null then a.sequence
                                       when b.sequence is not null then b.sequence 
                                       else a.sequence
                                   end
from r1 a join r2 b on a.id=b.id and a.timestamp=b.timestamp
;
create table test2 like test1
;
with r1
as
(select id, page, timestamp, lag(sequence) over (partition id order by timestamp) as sequence from test1)
r2
as
(select id, page, timestamp, sequence from test1)
insert into test2
select a.id, a.page, a.timestamp, case when a.sequence is not null then a.sequence
                                       when b.sequence is not null then b.sequence 
                                       else a.sequence
                                   end
from r1 a join r2 b on a.id=b.id and a.timestamp=b.timestamp
;
create table test3 like test2
;
and it repeat to fill another blank until my fingers are numb...

How do I fill in the blanks to the immediate preceding figures as shown above?如上图所示,如何填空前面的数字? I think I should use the recursive query, but can not find a way.我想我应该使用递归查询,但是找不到办法。

You don't need a recursive query at all.您根本不需要递归查询。

There is two function in Hive which can help you: Hive 中有两个功能可以帮助您:

So you query should look like:所以你的查询应该是这样的:

create table tmp_table like original_table;

insert into tmp_table
SELECT
    id, 
    page, 
    ts,
    COALESCE(sequence, 
             LAST_VALUE(sequence, TRUE) OVER(ORDER BY ts ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW))
FROM original_table;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM