我必須經常將 Dataframes 撰寫為 Hive 表。
df.write.mode('overwrite').format('hive').saveAsTable(f'db.{file_nm}_PT')
或者使用 Spark SQL 或 Hive SQL 將一個表復制到另一個表作為備份。
INSERT OVERWRITE TABLE db.tbl_bkp PARTITION (op_cd, rpt_dt)
SELECT * FROM db.tbl;
問題是:寫入 hive_saging_directory 需要 25% 的總時間,而 75% 或更多的時間用于將 ORC 檔案從暫存目錄移動到最終磁區目錄結構。
21/11/13 00:51:25 INFO hive.ql.metadata.Hive: New loading path = gs://sam_tables/teradata/tbl_bkp/.hive-staging_hive_2021-11-12_23-26-38_441_6664318328991520567-1/-ext-10000/rpt_dt=2019-10-24 with partSpec {rpt_dt=2019-10-24}
21/11/13 00:51:56 INFO hive.ql.metadata.Hive: Replacing src:gs://sam_tables/teradata/tbl_bkp/.hive-staging_hive_2021-11-12_23-26-38_441_6664318328991520567-1/-ext-10000/rpt_dt=2018-02-18/part-00058-95ee77f0-4e27-4765-a454-e5009c4f33f3.c000, dest: gs://sam_tables/teradata/tbl_bkp/rpt_dt=2018-02-18/part-00058-95ee77f0-4e27-4765-a454-e5009c4f33f3.c000, Status:true
21/11/13 00:51:56 INFO hive.ql.metadata.Hive: New loading path = gs://sam_tables/teradata/tbl_bkp/.hive-staging_hive_2021-11-12_23-26-38_441_6664318328991520567-1/-ext-10000/rpt_dt=2018-02-18 with partSpec {rpt_dt=2018-02-18}
21/11/13 00:52:31 INFO hive.ql.metadata.Hive: Replacing src:gs://sam_tables/teradata/tbl_bkp/.hive-staging_hive_2021-11-12_23-26-38_441_6664318328991520567-1/-ext-10000/rpt_dt=2019-01-29/part-00046-95ee77f0-4e27-4765-a454-e5009c4f33f3.c000, dest: gs://sam_tables/teradata/tbl_bkp/rpt_dt=2019-01-29/part-00046-95ee77f0-4e27-4765-a454-e5009c4f33f3.c000, Status:true
21/11/13 00:52:31 INFO hive.ql.metadata.Hive: New loading path = gs://sam_tables/teradata/tbl_bkp/.hive-staging_hive_2021-11-12_23-26-38_441_6664318328991520567-1/-ext-10000/rpt_dt=2019-01-29 with partSpec {rpt_dt=2019-01-29}
21/11/13 00:53:09 INFO hive.ql.metadata.Hive: Replacing src:gs://sam_tables/teradata/tbl_bkp/.hive-staging_hive_2021-11-12_23-26-38_441_6664318328991520567-1/-ext-10000/rpt_dt=2020-08-01/part-00020-95ee77f0-4e27-4765-a454-e5009c4f33f3.c000, dest: gs://sam_tables/teradata/tbl_bkp/rpt_dt=2020-08-01/part-00020-95ee77f0-4e27-4765-a454-e5009c4f33f3.c000, Status:true
21/11/13 00:53:09 INFO hive.ql.metadata.Hive: New loading path = gs://sam_tables/teradata/tbl_bkp/.hive-staging_hive_2021-11-12_23-26-38_441_6664318328991520567-1/-ext-10000/rpt_dt=2020-08-01 with partSpec {rpt_dt=2020-08-01}
21/11/13 00:53:46 INFO hive.ql.metadata.Hive: Replacing src:gs://sam_tables/teradata/tbl_bkp/.hive-staging_hive_2021-11-12_23-26-38_441_6664318328991520567-1/-ext-10000/rpt_dt=2021-07-12/part-00026-95ee77f0-4e27-4765-a454-e5009c4f33f3.c000, dest: gs://sam_tables/teradata/tbl_bkp/rpt_dt=2021-07-12/part-00026-95ee77f0-4e27-4765-a454-e5009c4f33f3.c000, Status:true
21/11/13 00:53:46 INFO hive.ql.metadata.Hive: New loading path = gs://sam_tables/teradata/tbl_bkp/.hive-staging_hive_2021-11-12_23-26-38_441_6664318328991520567-1/-ext-10000/rpt_dt=2021-07-12 with partSpec {rpt_dt=2021-07-12}
21/11/13 00:54:17 INFO hive.ql.metadata.Hive: Replacing src:gs://sam_tables/teradata/tbl_bkp/.hive-staging_hive_2021-11-12_23-26-38_441_6664318328991520567-1/-ext-10000/rpt_dt=2022-01-21/part-00062-95ee77f0-4e27-4765-a454-e5009c4f33f3.c000, dest: gs://sam_tables/teradata/tbl_bkp/rpt_dt=2022-01-21/part-00062-95ee77f0-4e27-4765-a454-e5009c4f33f3.c000, Status:true
21/11/13 00:54:17 INFO hive.ql.metadata.Hive: New loading path = gs://sam_tables/teradata/tbl_bkp/.hive-staging_hive_2021-11-12_23-26-38_441_6664318328991520567-1/-ext-10000/rpt_dt=2022-01-21 with partSpec {rpt_dt=2022-01-21}
21/11/13 00:54:49 INFO hive.ql.metadata.Hive: Replacing src:gs://sam_tables/teradata/tbl_bkp/.hive-staging_hive_2021-11-12_23-26-38_441_6664318328991520567-1/-ext-10000/rpt_dt=2018-01-20/part-00063-95ee77f0-4e27-4765-a454-e5009c4f33f3.c000, dest: gs://sam_tables/teradata/tbl_bkp/rpt_dt=2018-01-20/part-00063-95ee77f0-4e27-4765-a454-e5009c4f33f3.c000, Status:true
21/11/13 00:54:49 INFO hive.ql.metadata.Hive: New loading path = gs://sam_tables/teradata/tbl_bkp/.hive-staging_hive_2021-11-12_23-26-38_441_6664318328991520567-1/-ext-10000/rpt_dt=2018-01-20 with partSpec {rpt_dt=2018-01-20}
21/11/13 00:55:22 INFO hive.ql.metadata.Hive: Replacing src:gs://sam_tables/teradata/tbl_bkp/.hive-staging_hive_2021-11-12_23-26-38_441_6664318328991520567-1/-ext-10000/rpt_dt=2019-09-01/part-00037-95ee77f0-4e27-4765-a454-e5009c4f33f3.c000, dest: gs://sam_tables/teradata/tbl_bkp/rpt_dt=2019-09-01/part-00037-95ee77f0-4e27-4765-a454-e5009c4f33f3.c000, Status:true
此操作在實際 HDFS 上非常快,但在 Google 云 blob 上,此重命名實際上是復制粘貼 blob,速度非常慢。
我聽說過直接路徑寫入,請大家建議如何做到這一點?
uj5u.com熱心網友回復:
(不是這樣)簡答
情況很復雜。非常復雜。我想寫一個簡短的答案,但我可能會在幾個方面產生誤導。相反,我將嘗試對很長的答案進行簡短的總結。
- Hive 使用臨時目錄有一個很好的理由:原子性。您不希望用戶在重寫表時讀取該表,因此您可以寫入暫存目錄并在完成后重命名該目錄,如下所示。
- 問題是:云存盤是“物件存盤”,而不是像 HDFS 這樣的“分布式檔案系統”,因此檔案夾重命名等一些操作可能會慢得多。
- 每個云都有自己的存盤實作,有自己的特性和缺點,隨著時間的推移,他們甚至會提出新的變體來克服其中的一些缺點(例如,Azure 有 3 種不同的存盤變體:Blob 存盤、Datalake Storage Gen 1 和 Gen 2)。
- 因此,一個云上的最佳解決方案不一定是另一個云上的最佳解決方案。
- 各種云存盤的 FileSystem API 實作是 Spark 使用的 Hadoop 發行版的一部分。因此,可供您使用的解決方案還取決于您的 Spark 安裝使用的是哪個版本的 Hadoop。
- 僅限 Azure/GCS:您可以嘗試設定 [此選項]:( https://spark.apache.org/docs/3.1.1/cloud-integration.html#configuring ):
spark.hadoop.mapreduce.fileoutputcommitter.algorithm.version 2。它比 快,v1但也不推薦,因為它不是原子的,因此在部分故障的情況下不太安全。 v2目前是 Hadoop 中的默認值,但Spark 3 將其設定回v1默認值,并且Hadoop 社區中有一些討論要棄用它并v1再次設為默認值。- 基于為 S3 完成的類似輸出提交程式,還有一些正在進行的開發為 Azure 和 GCS 撰寫更好的輸出提交程式。
- 或者,您可以嘗試切換到云優先格式,例如 Apache Iceberg、Apache Hudi 或 Delta Lake。
- 我對這些還不是很熟悉,但是快速瀏覽Delta Lake 的檔案讓我相信他們必須處理相同型別的問題(云存盤不是真正的檔案系統),并且取決于您使用的是哪個云,它可能需要額外的配置,特別是在 GCP 上,該功能被標記為實驗性。
- 編輯: Apache Iceberg 沒有這個問題,因為它使用元資料檔案指向真實的資料檔案位置。因此,對表的更改是通過對單個元資料檔案的原子更改提交的。
- 我對 Apache Hudi 不是很熟悉,我找不到任何關于它們處理此類問題的提及。我必須進一步深入研究他們的設計架構才能確定。
現在,對于長篇大論的回答,也許我應該寫一篇博客文章......每當它完成時我都會在這里發布。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/360148.html
