Hive進階:一種比較傳統的Hive數據表更新方案

咱們知道在Hive0.14版本以前是不支持update和delete操做的,以後的Hive數據表必需要知足必定的條件,好比ORC存儲、ACID支持等,才能夠進行update和delete操做,本篇文章講一下傳統的hive數據表若是經過寫SQL的方式實現數據的更新。
數據庫


1.這裏新建兩張表student10,student10_temp(臨時表,可當作業務庫),建表語句以下:app

//學生信息表create table student10(id string,name string,age string) row format delimited fields terminated by',' stored as textfile;//學生信息表臨時表,業務關係數據庫的變化,通常先導入臨時表create table student10_temp(id string,name string,age string) row format delimited fields terminated by',' stored as textfile;


2.數據準備,可理解成業務庫更新前全量數據,文件test.txt,數據以下:
ide

[root@salver158 ~]# cat /tmp/test.txt 1,name1,162,name2,173,name3,18


3.這裏先將test.txt中的數據加載到student10_temp表中,執行命令:
工具

hive> load data local inpath '/tmp/test.txt'  into table student10_temp;Loading data to table lujs.student10_tempTable lujs.student10_temp stats: [numFiles=1, numRows=0, totalSize=33, rawDataSize=0]OKTime taken: 1.275 seconds

    執行insert into將student10_temp,全量插入到student10表中:oop

hive> insert into table student10 select id,name,age from student10_temp;

    到這裏兩張表數據相同,這就至關於咱們生產上,第一次進行了全量數據的導入;這時候咱們更新了student10_temp表(這裏模擬業務關係數據庫中刪除一條數據,插入了兩條數據),這裏直接加載文件test1.txt:spa

[root@salver158 tmp]# cat  /tmp/test1.txt 2,name2,173,name3,5104,name4,195,name5,20

    其實這個student10_temp通常表明的是關係型數據庫的變化,而後可經過sqoop等數據抽取工具,全量抽取關係數據庫已變化的數據到student10_temp臨時表中。code

hive> select * from student10_temp;OK2  name2  173  name3  5104  name4  195  name5  20


4.咱們能夠經過SQL進行數據比對,而後把非增量、非更新的數據覆蓋到student10表中,執行完SQL後可保證student10和student10_temp數據不重複(這裏表必需要有主鍵,否則無法關聯)orm


    找出非增量、非更新數據,通常就是已刪除數據,執行命令:
string

select b.id,b.name,b.age from student10 a left join student10_temp b on a.id=b.id  where b.id is nul

    將非增量、非更新數據覆蓋到student10表,執行命令:it

insert overwrite table student10 select b.id,b.name,b.age from student10 a left join student10_temp b on a.id=b.id  where b.id is null

    執行完後,表中只有非增量、非更新數據,通常就是業務庫已刪除數據,這裏咱們能夠選擇保留作個標識;


5.通過步驟4的比對去重以後,則能夠把臨時表數據加載進來,執行命令:

hive> insert into table student10  select  id,name,age from student10_temp;

    查看最終數據,包含非增量、非更新數據,以及更新和增量數據:

hive> select * from student10;OK1  name1  162  name2  173  name3  5104  name4  195  name5  20Time taken: 0.436 seconds, Fetched: 5 row(s)


    我這裏只是簡單的一種數據更新方案,中間可能問題,數據更新以前最後都要作好備份,以避免丟失數據。


補充SQL:


    1.比對新增、更新數據SQL,這裏能夠執行步驟4以前查看,可在student10表中添加一個標識字段來標識,是已刪除、更新、仍是新增數據,這裏就不在演示,有興趣本身去研究下:

hive> select a.id,a.name,a.age,CASE WHEN b.id IS null THEN '新增' ELSE '更新'  END  FROM  student10_temp a LEFT JOIN  student10  b on a.id=b.id;Query ID = root_20200310194355_d5428597-77a7-40e6-8b56-6b636c3e137eTotal jobs = 1Launching Job 1 out of 1Status: Running (Executing on YARN cluster with App id application_1583204243863_0026)
--------------------------------------------------------------------------------        VERTICES      STATUS  TOTAL  COMPLETED  RUNNING  PENDING  FAILED  KILLED--------------------------------------------------------------------------------Map 1 ..........   SUCCEEDED      1          1        0        0       0       0Map 2 ..........   SUCCEEDED      1          1        0        0       0       0--------------------------------------------------------------------------------VERTICES: 02/02  [==========================>>] 100%  ELAPSED TIME: 6.59 s     --------------------------------------------------------------------------------OK2  name2  17  更新3  name3  510  更新4  name4  19  新增5  name5  20  新增
相關文章
相關標籤/搜索