hive merge into 批量更新測試


一、使用條件

hive2.2.0及之后的版本支持使用merge into 語法,使用源表數據批量目標表的數據。使用該功能還需做如下配置

1、參數配置
set hive.support.concurrency = true;
set hive.enforce.bucketing = true;
set hive.exec.dynamic.partition.mode = nonstrict;
set hive.txn.manager = org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
set hive.compactor.initiator.on = true;
set hive.compactor.worker.threads = 1;
set hive.auto.convert.join=false;
set hive.merge.cardinality.check=false; -- 目標表中出現重復匹配時要設置該參數才行
2、建表要求

Hive對使用Update功能的表有特定的語法要求, 語法要求如下: (1)要執行Update的表中, 建表時必須帶有buckets(分桶)屬性 (2)要執行Update的表中, 需要指定格式,其余格式目前贊不支持, 如:parquet格式, 目前只支持ORCFileformat和AcidOutputFormat (3)要執行Update的表中, 建表時必須指定參數('transactional' = true);

DROP TABLE IF EXISTS dim_date_10000;
create table dim_date_10000(
date_key       string                 comment'如:2018-08-08'
,day             int                 comment'日(1~31)'
,month           int                 comment'月,如:8'
,month_name     string       comment'月名稱,如:8月'
,year            int                   comment'年,如:2018'
,year_month       int                   comment'年月,如201808'
,week_of_year   string                   comment'年內第幾周 2018-1'
,week            int                 comment'周(1~7)'
,week_name       string         comment'周,如星期三'
,quarter         int                 comment'季(1~4)'
)
CLUSTERED BY (date_key) INTO 10 buckets
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
STORED AS orc
TBLPROPERTIES('transactional'='true');
3、批量更新語法
 MERGE INTO <target table> AS T USING <source expression/table> AS S
ON <``boolean` `expression1>
WHEN MATCHED [AND <``boolean` `expression2>] THEN UPDATE SET <set clause list>
WHEN MATCHED [AND <``boolean` `expression3>] THEN DELETE
WHEN NOT MATCHED [AND <``boolean` `expression4>] THEN INSERT VALUES<value list>

二、批量更新語法對比

對比在hive1.1.0 使用overwrite ,hive2.3.5使用merge into的方式 ,對不同量級的數據進行更新時的語法及效率。

1、更新語法

hive 2.3.5 merge into相較與Hive1.1.0 overwrite 更新方式語法更簡潔。

Hive1.1.0
insert overwrite table dim_date_100w
-- 舊的改變了的數據
select t2.date_key,t2.day,t2.month,t2.month_name,t2.year,t2.year_month,t2.week_of_year,t2.week,t2.week_name,1001 as quarter
from dim_date_100w t1
join dim_date_1w t2 on t1.date_key=t2.date_key
-- 舊的不變的數據
union all
select t1.*
from dim_date_100w t1
left join dim_date_1w t2 on t1.date_key=t2.date_key
where t2.date_key is null
-- 新增的數據
union all
select t1.*
from dim_date_1w t1
left join dim_date_100w t2 on t1.date_key=t2.date_key
where t2.date_key is null
;
Hive2.3.5
MERGE INTO dim_date_100w AS T USING dim_date_1w AS S
ON t.date_key=s.date_key
WHEN MATCHED THEN UPDATE SET quarter=1001
WHEN NOT MATCHED THEN INSERT VALUES(S.date_key,S.day,S.month,S.month_name,S.year,S.year_month,S.week_of_year,S.week,S.week_name,S.quarter);
2、更新用時

兩種更新方式在10w及10000w時更新用時相差不多,在1000w時Hive2.3.5用時只需Hive1.1.0的一半。

目標表數據量 源表數據量 Hive1.1.0 批量更新用時 Hive2.3.5 批量更新用時
10w條 1w 90s 80s
1000W 1w 330s 160s
1000w 100w 340s 180s
10000w 1w 640s 610s
10000w 100w 700s 630s


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM