簡介:
本文主要講解如何透過insert overwrite更新資料
背景
對於大資料中的大多數儲存格式,支援隨機更新非常複雜。它需要掃描大型檔案,
MaxCompute推出了最新的功能
Transactional表可以支援update和delete語句,但是update和delete功能不適用於高頻更新、刪除資料或實時寫入目標表場景,同時對於非Transactional表無法執行update和delete。本文主要講解如何透過insert overwrite更新資料。
1。建表插入資料
create table update_table(ID int, tranValue string, last_update_user string) PARTITIONED by(dt STRING ) LIFECYCLE 1;INSERT INTO update_table PARTITION (dt=“20210510”) VALUES(1, ‘value_01’, ‘creation’),(2, ‘value_02’, ‘creation’),(3, ‘value_03’, ‘creation’),(4, ‘value_04’, ‘creation’),(5, ‘value_05’, ‘creation’),(6, ‘value_06’, ‘creation’),(7, ‘value_07’, ‘creation’),(8, ‘value_08’, ‘creation’),(9, ‘value_09’, ‘creation’),(10, ‘value_10’,‘creation’);
2。更新一條資料
當id是1的時候更新成value_011
——更新一條資料INSERT OVERWRITE TABLE update_table PARTITION( dt)SELECT id ,CASE WHEN id=1 THEN “value_011” ELSE TranValue END TranValue ,last_update_user ,dtFROM update_tableWHERE dt = “20210510”;
3。更新多條資料
根據增量表更新,首先建立增量表插入資料
create table update_table_inc(ID int, TranValue string, last_update_user string) LIFECYCLE 1;INSERT INTO update_table_inc VALUES (5, ‘value_11’, ‘creation’),(6, NULL, ‘20170410’),(7, ‘value22’, ‘20170413’);
id是5和7更新TranValue,由於6的TranValue是null不更新
INSERT OVERWRITE TABLE update_table PARTITION( dt)SELECT a。id ,CASE WHEN a。id=b。id and b。TranValue is not null THEN b。TranValue ELSE a。TranValue END TranValue ,CASE WHEN a。id=b。id and b。TranValue is not null THEN b。last_update_user ELSE a。last_update_user END last_update_user ,dtFROM update_table aLEFT JOIN update_table_inc bON a。id = b。idWHERE a。dt = “20210510”;
4。刪除資料
——刪除資料INSERT OVERWRITE TABLE update_table PARTITION( dt)SELECT * FROM update_tableWHERE dt = “20210510” and id !=4;