【问题标题】:Update column in hive table based on another table基于另一个表更新配置单元表中的列
【发布时间】:2018-01-30 15:58:36
【问题描述】:

我有一种情况,我需要根据另一个表中的某些条件更新列的值。数据如下:

ID  Date   Amount
00  02/01  0
00  02/01  0
01  05/01  100
01  05/01  0

另一个表包含以下内容:

ID  Date   Amount
00  02/01  0
00  02/01  0

我需要更改第二张表中的日期列以匹配第一张表中 ID '01' 的日期值。我尝试了加入它的选项,但它似乎无法正常工作。什么是最简单的解决方案?

【问题讨论】:

  • 除非您在表上设置了事务属性,否则无法更新表。
  • 我知道 Hive 的限制......但如果可能的话,我正在寻找其他解决方案。

标签: hadoop hive hiveql


【解决方案1】:
insert overwrite table table2 
select t1.id, 
       t2.Date,
       t2.amount 
from table2 t2 left join table t1 
     on t1.id=t2.id

如果您在 table1 中获得 ID 缺失的空值,您可以包含 when 情况

insert overwrite table table2 
select case when(t1.id is null) then 0 else t1.id end, 
       t2.Date,
       t2.amount 
from table2 t2 left join table t1 
     on t1.id=t2.id

希望这能解决您的问题。

【讨论】:

    【解决方案2】:

    您可以创建一个新表然后删除旧表,因为除非该表设置了事务属性,否则无法更新表。

    create new_table2 
    location 'HDFS path' as 
    select t2.id,d.date,t2.amount
    from table2 t2 
    cross join (select max(date) as date from table1 where id='01') d;
    /*This assumes there is one distinct date for id=01 in table1*/
    
    drop table table2 purge;
    

    【讨论】:

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