hive 大数据 除重问题研究

Hive 典型的中表内数据除重写法

insert overwrite table store

  select t.p_key,t.sort_word from 

    ( select p_key,

           sort_word ,

           row_number()over(distribute by p_key sort by sort_word) as rn

     from store) t

     where t.rn=1;

 

 

存量表: store

增量表:  incre 

 

字段:

1. p_key   除重主键

2. w_sort  排序依据

3. info    其他信息

 

方法一(union all + row_number()over ):

 

insert overwrite table limao_store  

  select p_key,sort_word 

    from ( select tmp1.*, row_number() over(distribute by sort_word sort by p_key desc) rownum 

             from ( select *

                      from limao_store   

                    union all   

                    select *

                      from limao_incre 

                   ) tmp1 

         ) hh 

   where hh.rownum = 1;

 

 

分析, 长表排序

 

 

方法二(left outer join + union all):

注意:  hive 不支持 顶层 union all ,而且union all 结果必须有别名

insert overwrite table limao_store 

select t.p_key,t.sort_word from (

  select s.p_key,s.sort_word from limao_store s left outer join limao_incre i on(s.p_key=i.p_key) where i.p_key=null

  union all

  select p_key,sort_word from limao_incre);

  

 

 

分析:  不能识别 incre中的重复数据   长表关联 ,  表宽度加倍

 

 

方法三(left outer join + insert into)

 

insert overwrite table store 

    select s.* from store s left outer join incre i on(s.p_key=i.p_key) where i.p_key=null    

    insert into table jm_g_l_cust_secu_acct  

    select * from jm_g_l_cust_secu_acct_tmp;

 

  分析:  insert into 最好不用。 使用insert into 在hdfs中的表现为,在表(分区)文件夹下,建立新的文件

存放insert into数据, 造成文件碎片,降低以后该表查询效率。

    

 

==================================================================================

 

 

use nets_life;

create table limao_store 

(

    p_key string,

    sort_word string

)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;

 

create table limao_incre 

(

    p_key string,

    sort_word string

)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;

 

 

建表语句

 

use nets_life;

create table limao_store 

(

    p_key string,

    sort_word string

)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;

 

create table limao_incre 

(

    p_key string,

    sort_word string

)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;

 

=====================================================

 

================================================================================================

总结:  方法二和方法三原理相同。 方法三建议避免

 

方法二、方法三 暗含逻辑:

    1.增量同步数据(incre)和存量数据(store)冲突时,总是认为增量数据为最新的

    2.无论增量数据表  还是 存量数据表, 表内没有重复字段

 

方法一, 不暗含上述逻辑。 全部合并,严格按排序字段排名取第一

 

一千万数据 store 和 一百万数据 incre 测试结果

 

方法一:  Time taken: 317.677 seconds

方法二:  Time taken: 106.032 seconds

 

总结: 方法二时间使用上大幅度少于方法一,但没有内部除重功能,只能用于比较除重。

==============================================

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值