hive自增字段维护

hive生成自增ID,分为全量数据和增量数据两种情况

  • 全量数据
with tmp as(
    SELECT  key
    FROM src_table
    WHERE dt = DATE_SUB(current_date,1)
    order by key
)
insert into table dest_table partition(dt=DATE_SUB(current_date,1))
select key, (cast((row_number() over(order by key)) as DECIMAL(11,0)) + cast(1000000000 as DECIMAL(11,0)))  as id
from tmp;
  • 增量数据
 with tmp as(
        select a.id
        from
            (
            SELECT  key
            FROM src_table 
            WHERE dt = DATE_SUB(current_date,1)
            order by key
            ) a
        left join 
            (
                select key
                from   dest_table 
                where  dt = DATE_SUB(current_date,2)
            ) b
        on a.key=b.key
        where b.key is null
    )
    insert into table dest_table partition(dt=DATE_SUB(current_date,1))
    select t1.key, (cast((row_number() over(order by t1.key)) as DECIMAL(11,0)) + cast(t2.max_id as DECIMAL(11,0)))  as id
    from   tmp t1 
    cross join 
    (
        select max(id) as max_id 
        from     dest_table 
        where  dt = DATE_SUB(current_date,2)
    ) t2

你可能感兴趣的:(hive,hive,sql,数据库)