溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

傳統(tǒng)hive數(shù)據(jù)表怎么通過(guò)寫(xiě)SQL方式實(shí)現(xiàn)數(shù)據(jù)的更新

發(fā)布時(shí)間:2021-12-10 09:30:47 來(lái)源:億速云 閱讀:1206 作者:小新 欄目:大數(shù)據(jù)

這篇文章主要為大家展示了“傳統(tǒng)hive數(shù)據(jù)表怎么通過(guò)寫(xiě)SQL方式實(shí)現(xiàn)數(shù)據(jù)的更新”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“傳統(tǒng)hive數(shù)據(jù)表怎么通過(guò)寫(xiě)SQL方式實(shí)現(xiàn)數(shù)據(jù)的更新”這篇文章吧。

1.這里新建兩張表student10,student10_temp(臨時(shí)表,可看成業(yè)務(wù)庫(kù)),建表語(yǔ)句如下:

//學(xué)生信息表create table student10(id string,name string,age string) row format delimited fields terminated by',' stored as textfile;//學(xué)生信息表臨時(shí)表,業(yè)務(wù)關(guān)系數(shù)據(jù)庫(kù)的變化,一般先導(dǎo)入臨時(shí)表create table student10_temp(id string,name string,age string) row format delimited fields terminated by',' stored as textfile;

2.數(shù)據(jù)準(zhǔn)備,可理解成業(yè)務(wù)庫(kù)更新前全量數(shù)據(jù),文件test.txt,數(shù)據(jù)如下:

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

3.這里先將test.txt中的數(shù)據(jù)加載到student10_temp表中,執(zhí)行命令:

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

    執(zhí)行insert into將student10_temp,全量插入到student10表中:

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

    到這里兩張表數(shù)據(jù)相同,這就相當(dāng)于我們生產(chǎn)上,第一次進(jìn)行了全量數(shù)據(jù)的導(dǎo)入;這時(shí)候我們更新了student10_temp表(這里模擬業(yè)務(wù)關(guān)系數(shù)據(jù)庫(kù)中刪除一條數(shù)據(jù),插入了兩條數(shù)據(jù)),這里直接加載文件test1.txt:

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

    其實(shí)這個(gè)student10_temp一般代表的是關(guān)系型數(shù)據(jù)庫(kù)的變化,然后可通過(guò)sqoop等數(shù)據(jù)抽取工具,全量抽取關(guān)系數(shù)據(jù)庫(kù)已變化的數(shù)據(jù)到student10_temp臨時(shí)表中。

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

4.我們可以通過(guò)SQL進(jìn)行數(shù)據(jù)比對(duì),然后把非增量、非更新的數(shù)據(jù)覆蓋到student10表中,執(zhí)行完SQL后可保證student10和student10_temp數(shù)據(jù)不重復(fù)(這里表必須要有主鍵,不然沒(méi)法關(guān)聯(lián))

    找出非增量、非更新數(shù)據(jù),一般就是已刪除數(shù)據(jù),執(zhí)行命令:

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

    將非增量、非更新數(shù)據(jù)覆蓋到student10表,執(zhí)行命令:

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

    執(zhí)行完后,表中只有非增量、非更新數(shù)據(jù),一般就是業(yè)務(wù)庫(kù)已刪除數(shù)據(jù),這里我們可以選擇保留做個(gè)標(biāo)識(shí);

5.經(jīng)過(guò)步驟4的比對(duì)去重之后,則可以把臨時(shí)表數(shù)據(jù)加載進(jìn)來(lái),執(zhí)行命令:

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

    查看最終數(shù)據(jù),包含非增量、非更新數(shù)據(jù),以及更新和增量數(shù)據(jù):

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

    我這里只是簡(jiǎn)單的一種數(shù)據(jù)更新方案,中間可能問(wèn)題,數(shù)據(jù)更新之前最后都要做好備份,以免丟失數(shù)據(jù)。

補(bǔ)充SQL:

    1.比對(duì)新增、更新數(shù)據(jù)SQL,這里可以執(zhí)行步驟4之前查看,可在student10表中添加一個(gè)標(biāo)識(shí)字段來(lái)標(biāo)識(shí),是已刪除、更新、還是新增數(shù)據(jù),這里就不在演示,有興趣自己去研究下:

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  新增

以上是“傳統(tǒng)hive數(shù)據(jù)表怎么通過(guò)寫(xiě)SQL方式實(shí)現(xiàn)數(shù)據(jù)的更新”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI