hive - 如何使用其他表的数据更新 Hive 中表的某些列

标签 hive hiveql

我想从另一个表更新一个表中某些列的数据。

cust_tabl 中的这三列 cf_mng,cf_sds,cf_htg 没有数据。

我想更新 cust_tabl 的这三列 cf_mng,cf_sds,cf_htg 的数据 与custom_hist表的三列cust_cd_cnt_1,cust_cd_cnt_2,cust_cd_cnt_3的数据。

此表包含从 201505 到 201509 的数据。

CREATE TABLE custom_hist( 
 cust_no varchar(20),    
 cust_cd_cnt_1 float,  
 cust_cd_cnt_2 float,  
 cust_cd_cnt_3 float,  
 cust_dt date,
 cust_name string) 
 PARTITIONED BY (yyyymm int);

此表包含从 201403 到 201606 的数据。

CREATE TABLE cust_tabl(
cust_no string,  
cf_mng double,  
cf_sds double,  
cf_htg double,  
cust_loc string,  
cust_region string,  
cust_country string,
cust_reg_id smallint)
PARTITIONED BY (yyyymm int); 

请帮助我。

最佳答案

通过主键连接表并覆盖连接的分区。检查主键。连接基数应为 1:1 或 1:0,否则您应该应用一些 row_numberrank 或一些聚合(如 max())来限制连接后的行:

set hive.exec.dynamic.partition.mode=nonstrict;
set hive.exec.dynamic.partition=true;

insert overwrite table cust_tabl partition (yyyymm)
select 
      c.cust_no,
      coalesce(h.cust_cd_cnt_1,c.cf_mng) as cf_mng, --take history column if joined
      coalesce(h.cust_cd_cnt_2,c.cf_sds) as cf_sds, --take original if not joined
      coalesce(h.cust_cd_cnt_3,c.cf_htg) as cf_htg,
      c.cust_loc,  --original columns
      c.cust_region,
      c.cust_country,
      c.cust_reg_id,
      c.yyyymm     --partition is the last
  from cust_tabl c
       left join custom_hist h 
                 --assume this is the primary key:
                 on c.cust_no = h.cust_no and c.yyyymm = h.yyyymm;

关于hive - 如何使用其他表的数据更新 Hive 中表的某些列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40100937/

相关文章:

python - 配置单元:python UDF 给出 "Hive Runtime Error while closing operators"

scala - 如何使用 doobie 连接到 Hive

sql-server - 如何使用sqoop将数据导出到具有自动增量ID的Microsoft Sql Table?

hadoop - 从Hive加载数据到ES时获取 “EsHadoopIllegalArgumentException: Index [petrol/petrolCat] missing and settings”

hadoop - 同一个IBM账号下访问多个COS实例

sql - Hive - 计算数据库中每天的记录数

hadoop - Hive动态分区无法将分区写入最终子目录

hadoop - 如何在 hive 中获取时间戳的平均数(('yyyy-mm-dd hh:mm:ss')?

sql - 如何计算 Hive 表中人员之间一致的重叠百分比

mysql - Hive/MySQL - 需要帮助来增加输入列中的连续 1