網站首頁 編程語言 正文
1. 摘要
社區小伙伴一直期待的Hudi整合Spark SQL的PR正在積極Review中并已經快接近尾聲,Hudi集成Spark SQL預計會在下個版本正式發布,在集成Spark SQL后,會極大方便用戶對Hudi表的DDL/DML操作,下面就來看看如何使用Spark SQL操作Hudi表。
2. 環境準備
首先需要將PR拉取到本地打包,生成SPARK_BUNDLE_JAR(hudi-spark-bundle_2.11-0.9.0-SNAPSHOT.jar)包
2.1 啟動spark-sql
在配置完spark環境后可通過如下命令啟動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 設置并發度
由于Hudi默認upsert/insert/delete的并發度是1500,對于演示的小規模數據集可設置更小的并發度。
set hoodie.upsert.shuffle.parallelism = 1; set hoodie.insert.shuffle.parallelism = 1; set hoodie.delete.shuffle.parallelism = 1;
同時設置不同步Hudi表元數據
set hoodie.datasource.meta.sync.enable=false;
3. Create Table
使用如下SQL創建表
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,分區字段為dt,合并字段默認為ts。
創建Hudi表后查看創建的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表本地目錄結構,生成的元數據、分區和數據與Spark Datasource寫入均相同。
4.2 Select
使用如下SQL查詢Hudi表數據
select * from test_hudi_table
查詢結果如下
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表數據
select * from test_hudi_table
查詢結果如下,可以看到price已經變成了20.0
查看Hudi表的本地目錄結構如下,可以看到在update之后又生成了一個deltacommit
,同時生成了一個增量log文件。
6. Delete
6.1 Delete
使用如下SQL將id=1的記錄刪除
delete from test_hudi_table where id = 1
查看Hudi表的本地目錄結構如下,可以看到delete之后又生成了一個deltacommit
,同時生成了一個增量log文件。
6.2 Select
再次查詢Hudi表
select * from test_hudi_table;
查詢結果如下,可以看到已經查詢不到任何數據了,表明Hudi表中已經不存在任何記錄了。
7. Merge Into
7.1 Merge Into Insert
使用如下SQL向test_hudi_table
插入數據
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表數據
select * from test_hudi_table
查詢結果如下,可以看到Hudi表中存在一條記錄
7.4 Merge Into Update
使用如下SQL更新數據
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
查詢結果如下,可以看到Hudi表中的分區已經更新了
7.6 Merge Into Delete
使用如下SQL刪除數據
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
查詢結果如下,可以看到Hudi表中已經沒有數據了
8. 刪除表
使用如下命令刪除Hudi表
drop table test_hudi_table;
使用show tables查看表是否存在
show tables;
可以看到已經沒有表了
9. 總結
通過上面示例簡單展示了通過Spark SQL Insert/Update/Delete Hudi表數據,通過SQL方式可以非常方便地操作Hudi表,降低了使用Hudi的門檻。另外Hudi集成Spark SQL工作將繼續完善語法,盡量對標Snowflake和BigQuery的語法,如插入多張表(INSERT ALL WHEN condition1 INTO t1 WHEN condition2 into t2),變更Schema以及CALL Cleaner、CALL Clustering等Hudi表服務。
原文鏈接:https://www.cnblogs.com/leesf456/p/14802281.html
相關推薦
- 2022-02-26 微信小程序 - 將頁面可分享到朋友圈功能(兩步完成)
- 2022-11-01 Git操作規范之tag的使用技巧詳解_相關技巧
- 2022-09-26 Linux系統監控(top,ps,netstat,kill命令),定時任務,系統啟動(系統的啟動級別
- 2022-08-07 詳解Qt使用QImage類實現圖像基本操作_C 語言
- 2022-05-13 C++ DeviceIoControl 獲取硬盤序列號
- 2022-02-12 Xpath中text()方法獲取列表為空問題解決方法
- 2024-03-19 Linux中 find 命令詳解
- 2022-03-14 Maven項目中遇見的一些問題(maven項目報錯)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支