【问题标题】:how to get updated with new record to hive main table from temp table如何使用新记录更新到临时表中的配置单元主表
【发布时间】:2019-03-13 08:39:59
【问题描述】:

我是 Hive 的新手。我有一个主表 t1 和临时表 t2。临时表每天都会被传入的数据上传。传入的数据可以是更新的记录,也可以是新的记录。

table t2(temp):               table t1(main)
id   name                      id      name
1   vinni                      1       vikki
3   anna                       2       amita

我希望我的主表有旧记录、临时表中的更新记录和临时表中的新记录。

我的主表应该有如下记录:

id  name
1   vinni
2   amita
3   anna

我尝试通过完全外连接来实现,但这不是最佳解决方案。那么如何通过使用左外连接来实现这一点。最后,我不希望我的临时表记录,并且该表可以在其数据加载到主表后被删除。

【问题讨论】:

标签: hadoop hive


【解决方案1】:

您可以使用两个表的并集来执行此操作,然后为已存在的 id 选择更新的记录,例如

with union_table as (
  select *, 0 as new_flag from t1
  union all
  select *, 1 as new_flag from t2
),
stage_table as (
  select *, max(new_flag) over (partition by id) as max_flag
    from union_table
),
stage_table2 as (
  select id, name, new_flag
    from stage_table
   where new_flag = max_flag
)
select id, name
  from stage_table2

Windowing max 将帮助您了解是否需要将特定 id 替换为更新后的值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多