Apache?Hudi集成Spark?SQL操作hide表
1. 摘要
社區(qū)小伙伴一直期待的Hudi整合Spark SQL的PR正在積極Review中并已經(jīng)快接近尾聲,Hudi集成Spark SQL預(yù)計(jì)會(huì)在下個(gè)版本正式發(fā)布,在集成Spark SQL后,會(huì)極大方便用戶對(duì)Hudi表的DDL/DML操作,下面就來(lái)看看如何使用Spark SQL操作Hudi表。
2. 環(huán)境準(zhǔn)備
首先需要將PR拉取到本地打包,生成SPARK_BUNDLE_JAR(hudi-spark-bundle_2.11-0.9.0-SNAPSHOT.jar)包
2.1 啟動(dòng)spark-sql
在配置完spark環(huán)境后可通過(guò)如下命令啟動(dòng)spark-sql
spark-sql --jars $PATH_TO_SPARK_BUNDLE_JAR --conf 'spark.serializer=org.apache.spark.serializer.KryoSerializer' --conf 'spark.sql.extensions=org.apache.spark.sql.hudi.HoodieSparkSessionExtension'
2.2 設(shè)置并發(fā)度
由于Hudi默認(rèn)upsert/insert/delete的并發(fā)度是1500,對(duì)于演示的小規(guī)模數(shù)據(jù)集可設(shè)置更小的并發(fā)度。
set hoodie.upsert.shuffle.parallelism = 1; set hoodie.insert.shuffle.parallelism = 1; set hoodie.delete.shuffle.parallelism = 1;
同時(shí)設(shè)置不同步Hudi表元數(shù)據(jù)
set hoodie.datasource.meta.sync.enable=false;
3. Create Table
使用如下SQL創(chuàng)建表
create table test_hudi_table ( id int, name string, price double, ts long, dt string ) using hudi partitioned by (dt) options ( primaryKey = 'id', type = 'mor' ) location 'file:///tmp/test_hudi_table'
說(shuō)明:表類型為MOR,主鍵為id,分區(qū)字段為dt,合并字段默認(rèn)為ts。
創(chuàng)建Hudi表后查看創(chuàng)建的Hudi表
show create table test_hudi_table

4. Insert Into
4.1 Insert
使用如下SQL插入一條記錄
insert into test_hudi_table select 1 as id, 'hudi' as name, 10 as price, 1000 as ts, '2021-05-05' as dt
insert完成后查看Hudi表本地目錄結(jié)構(gòu),生成的元數(shù)據(jù)、分區(qū)和數(shù)據(jù)與Spark Datasource寫(xiě)入均相同。

4.2 Select
使用如下SQL查詢Hudi表數(shù)據(jù)
select * from test_hudi_table
查詢結(jié)果如下

5. Update
5.1 Update
使用如下SQL將id為1的price字段值變更為20
update test_hudi_table set price = 20.0 where id = 1
5.2 Select
再次查詢Hudi表數(shù)據(jù)
select * from test_hudi_table
查詢結(jié)果如下,可以看到price已經(jīng)變成了20.0

查看Hudi表的本地目錄結(jié)構(gòu)如下,可以看到在update之后又生成了一個(gè)deltacommit,同時(shí)生成了一個(gè)增量log文件。

6. Delete
6.1 Delete
使用如下SQL將id=1的記錄刪除
delete from test_hudi_table where id = 1
查看Hudi表的本地目錄結(jié)構(gòu)如下,可以看到delete之后又生成了一個(gè)deltacommit,同時(shí)生成了一個(gè)增量log文件。

6.2 Select
再次查詢Hudi表
select * from test_hudi_table;
查詢結(jié)果如下,可以看到已經(jīng)查詢不到任何數(shù)據(jù)了,表明Hudi表中已經(jīng)不存在任何記錄了。

7. Merge Into
7.1 Merge Into Insert
使用如下SQL向test_hudi_table插入數(shù)據(jù)
merge into test_hudi_table as t0 using ( select 1 as id, 'a1' as name, 10 as price, 1000 as ts, '2021-03-21' as dt ) as s0 on t0.id = s0.id when not matched and s0.id % 2 = 1 then insert *
7.2 Select
查詢Hudi表數(shù)據(jù)
select * from test_hudi_table
查詢結(jié)果如下,可以看到Hudi表中存在一條記錄

7.4 Merge Into Update
使用如下SQL更新數(shù)據(jù)
merge into test_hudi_table as t0 using ( select 1 as id, 'a1' as name, 12 as price, 1001 as ts, '2021-03-21' as dt ) as s0 on t0.id = s0.id when matched and s0.id % 2 = 1 then update set *
7.5 Select
查詢Hudi表
select * from test_hudi_table
查詢結(jié)果如下,可以看到Hudi表中的分區(qū)已經(jīng)更新了

7.6 Merge Into Delete
使用如下SQL刪除數(shù)據(jù)
merge into test_hudi_table t0 using ( select 1 as s_id, 'a2' as s_name, 15 as s_price, 1001 as s_ts, '2021-03-21' as dt ) s0 on t0.id = s0.s_id when matched and s_ts = 1001 then delete
查詢結(jié)果如下,可以看到Hudi表中已經(jīng)沒(méi)有數(shù)據(jù)了

8. 刪除表
使用如下命令刪除Hudi表
drop table test_hudi_table;
使用show tables查看表是否存在
show tables;
可以看到已經(jīng)沒(méi)有表了

9. 總結(jié)
通過(guò)上面示例簡(jiǎn)單展示了通過(guò)Spark SQL Insert/Update/Delete Hudi表數(shù)據(jù),通過(guò)SQL方式可以非常方便地操作Hudi表,降低了使用Hudi的門檻。另外Hudi集成Spark SQL工作將繼續(xù)完善語(yǔ)法,盡量對(duì)標(biāo)Snowflake和BigQuery的語(yǔ)法,如插入多張表(INSERT ALL WHEN condition1 INTO t1 WHEN condition2 into t2),變更Schema以及CALL Cleaner、CALL Clustering等Hudi表服務(wù)。
以上就是Apache Hudi集成Spark SQL操作hide表的詳細(xì)內(nèi)容,更多關(guān)于Apache Hudi集成Spark SQL的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信