首页 > 解决方案 > PySpark。如何确保每日增量数据在 HIVE 中没有重复的 UUID 作为 PK

问题描述

例如,我在 Hive 中以 UUID 作为主键创建了一个表

create table if not exists mydb.mytable as SELECT uuid() as uni_id, c.name, g.city, g.country 
FROM client c 
INNER JOIN geo g ON c.geo_id = g.id

每天都需要往mytable中插入数据,如何保证每天的增量数据没有重复的UUID作为PK?

标签: pysparkhiveuuid

解决方案


如果通过 UUID,您正在寻找的是一系列Universally Unique Identifier,那么我认为您可以使用自动增量 id。在纯HQL中,可以通过row_numer和cross join来实现。

insert overwrite table dest_tbl 
select
    a.rn + b.mid as id, col1, col2,...
from (
    select 
        *, row_number() over(order by rand()) as rn
    from src_tbl
) a 
join (select max(id) as mid from dst_tbl) b

推荐阅读