背景:
根據(jù)甲方要求,需要對(duì)大數(shù)據(jù)平臺(tái)指定表(hive、impala表)的歷史數(shù)據(jù)[2021-01-01至2023-03-29]指定字段進(jìn)行批量更新,然后把表同步到Oracle。先更新大數(shù)據(jù)平臺(tái)上的表,再把更新完成的表同步到Oracle。hive有8張表更新,其中4張大表【分區(qū)表】(數(shù)據(jù)量分別為:1038738976、260958144、25860509、2867005),另外4張小表(幾萬(wàn)、二十幾萬(wàn)的樣子)。
一、小表更新,不用按月\按分區(qū)更新,直接全量更新。
insert overwrite table 表a (字段1,字段2,...,字段n)
select
字段1,字段2,...,
nvl(t2.projectbelong,t1.projectbelong) projectbelong,
...,
字段n
from 表a t1
left join 表b t2 on t1.root_item_code=t2.desc1;
二、大表更新,拿其中一張表舉例:按月\按分區(qū)更新
方法一:
insert overwrite table 表1 partition (date_month = '2021-01',date_day,org_code)
select
字段1,字段2,...,
nvl(t2.projectbelong,t1.projectbelong) projectbelong,
...,
字段n,
--t1.date_month,
t1.date_day,
t1.org_code
from (select * from 表1 where date_month = '2021-01') t1
left join 表2 t2 on t1.root_item_code=t2.desc1;
替換date_month日期即可。
方法二:
使用impala外部命令:impala-shell
1、創(chuàng)建impala.sql腳本,內(nèi)容如下:
Linux上,使用vim:
vim impala.sql
?寫(xiě)入以下內(nèi)容:
insert overwrite table 表1 partition (date_month = '${var:CURR_TIME}',date_day,org_code)
select
字段1,字段2,...,
nvl(t2.projectbelong,t1.projectbelong) projectbelong,
...,
字段n,
--t1.date_month,
t1.date_day,
t1.org_code
from (select * from 表1 where date_month = '${var:CURR_TIME}') t1
left join 表2 t2 on t1.root_item_code=t2.desc1;
2、impala外部命令:
impala-shell -f impala.sql -d tianma_bi --var CURR_TIME='2021-01';
3、多個(gè)月份,使用vim命令創(chuàng)建bash文件:impala.sh
impala-shell -f impala.sql -d tianma_bi --var CURR_TIME='2021-01';
impala-shell -f impala.sql -d tianma_bi --var CURR_TIME='2021-02';
impala-shell -f impala.sql -d tianma_bi --var CURR_TIME='2021-03';
4、執(zhí)行sh文件:
sh impala.sh
更新總結(jié):m-分鐘、s-秒
1、百萬(wàn)級(jí)表數(shù)據(jù)總量:500萬(wàn),更新用時(shí)約20s
2、千萬(wàn)級(jí)表數(shù)據(jù)總量:2500萬(wàn),更新用時(shí)約1m
3、億級(jí)表數(shù)據(jù)總量:分區(qū)更新
50萬(wàn),更新用時(shí)約17s
100萬(wàn),更新用時(shí)約32s
500萬(wàn),更新用時(shí)約2m10s
1000萬(wàn),更新用時(shí)約5m
1500萬(wàn),更新用時(shí)約6m
2000萬(wàn),更新用時(shí)約6m30s
5000萬(wàn),更新用時(shí)約16m
7500萬(wàn),更新用時(shí)約22m
根據(jù)以上,估算1億數(shù)據(jù)更新用時(shí)約35m
方法三:也可使用merge into
說(shuō)明:Hive在2.2版本之后開(kāi)始支持Merge操作,并且Merge只能在支持ACID的表上執(zhí)行。低版本的hive中有很多函數(shù)或者語(yǔ)句不支持使用,比如merge into
舉個(gè)例子:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-473102.html
MERGE INTO merge_data.transactions AS T
USING merge_data.merge_source AS S
ON T.ID = S.ID and T.tran_date = S.tran_date
WHEN MATCHED AND (T.TranValue != S.TranValue AND S.TranValue IS NOT NULL) THEN UPDATE SET
TranValue = S.TranValue
,last_update_user = 'merge_update'
WHEN MATCHED AND S.TranValue IS NULL THEN DELETE
WHEN NOT MATCHED THEN INSERT VALUES (
S.ID
, S.TranValue
, 'merge_insert'
, S.tran_date
);
建議使用merge into,效率更快一些。?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-473102.html
到了這里,關(guān)于hive表數(shù)據(jù)更新insert overwrite/merge into的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!