【问题标题】:Hive join between two tables两个表之间的 Hive 连接
【发布时间】:2020-03-03 15:52:57
【问题描述】:

这是个问题: 我有这个临时表:

key0    key1    timestamp   partition_key
5   5   2020-03-03 14:42:21.548 1
5   4   2020-03-03 14:40:11.871 1
4   3   2020-03-03 14:43:47.602 2

还有这个目标表:

key0    key1    timestamp   partition_key
5   4   2020-03-03 13:43:16.695 1
5   5   2020-03-03 13:45:24.793 1
5   2   2020-03-03 13:47:30.668 1
5   1   2020-03-03 13:48:30.669 1
4   3   2020-03-03 13:53:47.602 2
43  3   2020-03-03 14:00:14.016 2

我想得到这个输出:

key0    key1    timestamp   partition_key
5   5   2020-03-03 14:42:21.548 1
5   4   2020-03-03 14:40:11.871 1
5   2   2020-03-03 13:47:30.668 1
5   1   2020-03-03 13:48:30.669 1
4   3   2020-03-03 14:43:47.602 2
43  3   2020-03-03 14:00:14.016 2

在时间戳字段中,我想要 key0、key1 和 partition_key 时的最新记录。另外,我想要目标表中已经存在但暂存表中不存在的记录

我首先尝试了这个查询:

select 
t1.key0,
t1.key1,
t1.timestamp,
t2.partition_key
from staging_table t2 
left outer join target_table t1 on 
t1.key0=t2.key0 AND
t1.key1=t2.key1 AND
t1.timestamp=t2.timestamp; 






【问题讨论】:

    标签: sql hadoop join hive


    【解决方案1】:

    这看起来像一个优先查询——从暂存中获取所有内容,然后从目标中获取不匹配的行。我要推荐union all

    select s.*
    from staging s
    union all
    select t.*
    from target t left join
         staging s
         on t.key0 = s.key0 and t.key1 = s.key1
    where s.key0 is null;
    

    这确实假设 staging 具有最新的行——这在您的示例数据中是正确的。如果没有,我会这样表述:

    select key0, key1, timestamp, partition_key
    from (select st.*,
                 row_number() over (partition by key0, key1 order by timestamp desc) as seqnum
          from ((select s.* from source s
                ) union all
                (select t.* from target t
                )
               ) st
         ) st
    where seqnum = 1;
    

    【讨论】:

      【解决方案2】:

      你需要FULL JOIN

      select COALESCE(t1.key0, T2.key0) AS key0, COALESCE(t1.key1, T2.KEY1) AS KEY1,
             COALESCE(t1.timestamp, T2.timestamp) AS timestamp, 
             COALESCE(t1.partition_key, t2.partition_key) AS partition_key
      t2.partition_key
      from staging_table t2 FULL JOIN 
           target_table t1
           on t1.key0 = t2.key0 AND t1.key1 = t2.key1 AND
              t1.timestamp = t2.timestamp; 
      

      【讨论】:

        【解决方案3】:

        我认为您只需要left joincoalesce()

        select 
            t.key0,
            t.key1,
            coalesce(s.timestamp, t.timestamp) timestamp,
            t.partition_key
        from target_table t 
        left join  staging_table s 
            on  s.key0 = t2.key0 
            and s.key1 = t.key1 
            and s.partition_key = t.partition_key
        

        对于target_table 中的每条记录,这将搜索staging_table 中具有相同(key0, key1, partition_key 的记录)。如果有这样的记录,我们将使用它的timestamp 代替target_table 中的timestamp

        【讨论】:

          猜你喜欢
          • 2021-10-26
          • 2013-08-06
          • 2013-12-10
          • 2012-04-25
          • 1970-01-01
          • 1970-01-01
          • 2014-12-04
          • 2019-02-25
          • 2014-02-15
          相关资源
          最近更新 更多