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操作,下面就來看看如何使用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)境后可通過如下命令啟動(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'
說明:表類型為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寫入均相同。
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)沒有數(shù)據(jù)了
8. 刪除表
使用如下命令刪除Hudi表
drop table test_hudi_table;
使用show tables查看表是否存在
show tables;
可以看到已經(jīng)沒有表了
9. 總結(jié)
通過上面示例簡(jiǎn)單展示了通過Spark SQL Insert/Update/Delete Hudi表數(shù)據(jù),通過SQL方式可以非常方便地操作Hudi表,降低了使用Hudi的門檻。另外Hudi集成Spark SQL工作將繼續(xù)完善語法,盡量對(duì)標(biāo)Snowflake和BigQuery的語法,如插入多張表(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)文章!
相關(guān)文章
neo4j實(shí)現(xiàn)創(chuàng)建多個(gè)數(shù)據(jù)庫
這篇文章主要介紹了neo4j實(shí)現(xiàn)創(chuàng)建多個(gè)數(shù)據(jù)庫方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02關(guān)于Navicat連接MySql數(shù)據(jù)庫慢的問題
這篇文章主要介紹了關(guān)于Navicat連接MySql數(shù)據(jù)庫慢的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03如何解決VisualSVN Server 安裝提示錯(cuò)誤 Repositories is not a valid shor
最近在程序中安裝VisualSVN Server時(shí),總是提示“'Repositories' is not a valid short file name”這個(gè)問題,難為了好長(zhǎng)時(shí)間,最終解決,下面小編把我的解決辦法分享給大家,供大家參考2015-09-09數(shù)據(jù)庫學(xué)習(xí)建議之提高數(shù)據(jù)庫速度的十條建議
很多網(wǎng)站的重要信息都是保存在數(shù)據(jù)庫中,用戶通過提交訪問數(shù)據(jù)庫來獲取用戶信息。如果數(shù)據(jù)庫速度非常的快,有助于節(jié)省服務(wù)器的資源,那么如何優(yōu)化數(shù)據(jù)庫的速度呢,下面通過此篇文章一起學(xué)習(xí)數(shù)據(jù)庫學(xué)習(xí)建議之提高數(shù)據(jù)庫速度的十條建議2015-11-11樹形結(jié)構(gòu)數(shù)據(jù)庫表Schema設(shè)計(jì)的兩種方案
程序設(shè)計(jì)過程中,我們常常用樹形結(jié)構(gòu)來表征某些數(shù)據(jù)的關(guān)聯(lián)關(guān)系,如企業(yè)上下級(jí)部門、欄目結(jié)構(gòu)、商品分類等等,下面這篇文章主要給大家介紹了關(guān)于樹形結(jié)構(gòu)數(shù)據(jù)庫表Schema設(shè)計(jì)的兩種方案,需要的朋友可以參考下2021-09-09數(shù)據(jù)庫正規(guī)化和設(shè)計(jì)技巧
數(shù)據(jù)庫正規(guī)化和設(shè)計(jì)技巧...2007-06-06