

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建置 ETL 服務管道，使用 AWS Glue 從 Amazon S3 遞增載入資料至 Amazon Redshift
<a name="build-an-etl-service-pipeline-to-load-data-incrementally-from-amazon-s3-to-amazon-redshift-using-aws-glue"></a>

*Rohan Jamadagni 和 Arunabha Datta，Amazon Web Services*

## 總結
<a name="build-an-etl-service-pipeline-to-load-data-incrementally-from-amazon-s3-to-amazon-redshift-using-aws-glue-summary"></a>

此模式提供如何設定 Amazon Simple Storage Service (Amazon S3) 以實現最佳資料湖效能的指引，然後使用 AWS Glue，將增量資料變更從 Amazon S3 載入 Amazon Redshift。 

Amazon S3 中的來源檔案可以有不同的格式，包括逗號分隔值 (CSV)、XML 和 JSON 檔案。此模式說明如何使用 AWS Glue 將來源檔案轉換為成本最佳化和效能最佳化格式，例如 Apache Parquet。您可以直接從 Amazon Athena 和 Amazon Redshift Spectrum 查詢 Parquet 檔案。您也可以將 Parquet 檔案載入 Amazon Redshift、彙總它們，以及與消費者共用彙總資料，或使用 Amazon Quick Sight 視覺化資料。

## 先決條件和限制
<a name="build-an-etl-service-pipeline-to-load-data-incrementally-from-amazon-s3-to-amazon-redshift-using-aws-glue-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ 具有正確權限且包含 CSV、XML 或 JSON 檔案的 S3 來源儲存貯體。

**假設**
+ CSV、XML 或 JSON 來源檔案已載入 Amazon S3，可從設定 AWS Glue 和 Amazon Redshift 的帳戶存取。
+ 如 [Amazon Redshift 文件](https://docs.aws.amazon.com/redshift/latest/dg/t_Loading-data-from-S3.html)所述，遵循載入檔案、分割檔案、壓縮和使用資訊清單的最佳實務。
+ 來源檔案結構未變更。
+ 來源系統能夠遵循 Amazon S3 中定義的資料夾結構，將資料擷取至 Amazon S3。
+ Amazon Redshift 叢集跨越單一可用區域。（此架構是適當的，因為 AWS Lambda、AWS Glue 和 Amazon Athena 是無伺服器。) 為了實現高可用性，叢集快照會定期拍攝。

**限制**
+ 檔案格式僅限於 [ AWS Glue 目前支援的](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-format.html)檔案格式。
+ 不支援即時下游報告。

## Architecture
<a name="build-an-etl-service-pipeline-to-load-data-incrementally-from-amazon-s3-to-amazon-redshift-using-aws-glue-architecture"></a>

**來源技術堆疊**
+ 具有 CSV、XML 或 JSON 檔案的 S3 儲存貯體

**目標技術堆疊**
+ S3 資料湖 （具有分割 Parquet 檔案儲存）
+ Amazon Redshift

**目標架構**

![\[使用 AWS Glue 將增量變更從 Amazon S3 載入 Amazon Redshift 的架構。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/105b58ec-56c1-464a-8e69-f625360caa14/images/626aa365-e6e6-4874-a873-1c71adbe5306.png)


 

**資料流程**

![\[使用 AWS Glue 將增量變更從 Amazon S3 載入 Amazon Redshift 的資料流程。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/105b58ec-56c1-464a-8e69-f625360caa14/images/29569e48-9f2d-4f48-bc59-1f33949d01ca.png)


## 工具
<a name="build-an-etl-service-pipeline-to-load-data-incrementally-from-amazon-s3-to-amazon-redshift-using-aws-glue-tools"></a>
+ [Amazon S3](https://aws.amazon.com/s3/) – Amazon Simple Storage Service (Amazon S3) 是一種高度可擴展的物件儲存服務。Amazon S3 可用於各種儲存解決方案，包括網站、行動應用程式、備份和資料湖。
+ [AWS Lambda](https://aws.amazon.com/lambda/) – AWS Lambda 可讓您執行程式碼，而無需佈建或管理伺服器。AWS Lambda 是一種事件驅動的服務；您可以設定程式碼，以從其他 AWS 服務自動啟動。
+ [Amazon Redshift](https://aws.amazon.com/redshift/) – Amazon Redshift 是全受管的 PB 級資料倉儲服務。使用 Amazon Redshift，您可以使用標準 SQL 查詢資料倉儲和資料湖中數 PB 的結構化和半結構化資料。
+ [AWS Glue](https://aws.amazon.com/glue/) – AWS Glue 是一種全受管 ETL 服務，可讓您更輕鬆地準備和載入資料以進行分析。AWS Glue 會探索您的資料，並將相關聯的中繼資料 （例如資料表定義和結構描述） 存放在 AWS Glue Data Catalog 中。您的目錄資料可立即搜尋、查詢，並可用於 ETL。
+ [AWS Secrets Manager](https://aws.amazon.com/secrets-manager/) – AWS Secrets Manager 有助於保護和集中管理應用程式或服務存取所需的秘密。服務會存放資料庫登入資料、API 金鑰和其他秘密，無需以純文字格式硬式編碼敏感資訊。Secrets Manager 也提供金鑰輪換，以滿足安全和合規需求。它具有 Amazon Redshift、Amazon Relational Database Service (Amazon RDS) 和 Amazon DocumentDB 的內建整合。您可以使用 Secrets Manager 主控台、命令列界面 (CLI) 或 Secrets Manager API 和 SDKs 來存放和集中管理秘密。
+ [Amazon Athena](https://aws.amazon.com/athena/) – Amazon Athena 是一種互動式查詢服務，可讓您輕鬆分析存放在 Amazon S3 中的資料。Athena 無伺服器並與 AWS Glue 整合，因此可以直接查詢使用 AWS Glue 編目的資料。Athena 會彈性擴展以提供互動式查詢效能。

## 史詩
<a name="build-an-etl-service-pipeline-to-load-data-incrementally-from-amazon-s3-to-amazon-redshift-using-aws-glue-epics"></a>

### 建立 S3 儲存貯體和資料夾結構
<a name="create-the-s3-buckets-and-folder-structure"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 分析來源系統的資料結構和屬性。 | 針對有助於 Amazon S3 資料湖的每個資料來源執行此任務。 | 資料工程師 | 
| 定義分割區和存取策略。 | 此策略應以資料擷取的頻率、差異處理和耗用需求為基礎。請確定 S3 儲存貯體未向公眾開放，且存取權僅由特定服務角色型政策控制。如需詳細資訊，請參閱 [Amazon S3 說明文件](https://docs.aws.amazon.com/AmazonS3/latest/user-guide/using-folders.html)。 | 資料工程師 | 
| 為每個資料來源類型建立單獨的 S3 儲存貯體，並為處理過的 (Parquet) 資料為每個來源建立單獨的 S3 儲存貯體。 | 為每個來源建立單獨的儲存貯體，然後根據來源系統的資料擷取頻率建立資料夾結構，例如 `s3://source-system-name/date/hour`。對於已處理 （轉換為 Parquet 格式） 檔案，請建立類似的結構，例如 `s3://source-processed-bucket/date/hour`。如需建立 S3 儲存貯體的詳細資訊，請參閱 [Amazon S3 文件](https://docs.aws.amazon.com/AmazonS3/latest/user-guide/create-bucket.html)。 | 資料工程師 | 

### 在 Amazon Redshift 中建立資料倉儲
<a name="create-a-data-warehouse-in-amazon-redshift"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 使用適當的參數群組以及維護和備份策略啟動 Amazon Redshift 叢集。 | 建立 Amazon Redshift 叢集時，針對管理員使用者憑證使用 Secrets Manager 資料庫秘密。如需有關建立和調整 Amazon Redshift 叢集大小的資訊，請參閱 [Amazon Redshift 文件](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/event-publishing-redshift-cluster.html)和[調整雲端資料倉儲大小](https://d1.awsstatic.com/whitepapers/Size-Cloud-Data-Warehouse-on-AWS.pdf)白皮書。 | 資料工程師 | 
| 建立 IAM 服務角色並將其連接至 Amazon Redshift 叢集。 | AWS Identity and Access Management (IAM) 服務角色可確保存取 Secrets Manager 和來源 S3 儲存貯體。如需詳細資訊，請參閱[授權](https://docs.aws.amazon.com/redshift/latest/mgmt/authorizing-redshift-service.html)和[新增角色](https://docs.aws.amazon.com/redshift/latest/dg/c-getting-started-using-spectrum-add-role.html)的 AWS 文件。 | 資料工程師 | 
| 建立資料庫結構描述。 | 遵循資料表設計的 Amazon Redshift 最佳實務。根據使用案例，選擇適當的排序和分佈索引鍵，以及最佳的壓縮編碼。如需最佳實務，請參閱 [AWS 文件](https://docs.aws.amazon.com/redshift/latest/dg/c_designing-tables-best-practices.html)。 | 資料工程師 | 
| 設定工作負載管理。 | 根據您的需求設定工作負載管理 (WLM) 佇列、短查詢加速 (SQA) 或並行擴展。如需詳細資訊，請參閱《Amazon Redshift 文件》中的[實作工作負載管理](https://docs.aws.amazon.com/redshift/latest/dg/cm-c-implementing-workload-management.html)。 | 資料工程師 | 

### 在 Secrets Manager 中建立秘密
<a name="create-a-secret-in-secrets-manager"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立新的秘密，將 Amazon Redshift 登入憑證存放在 Secrets Manager 中。 | 此秘密會存放管理員使用者以及個別資料庫服務使用者的登入資料。如需說明，請參閱 [Secrets Manager 文件](https://docs.aws.amazon.com/secretsmanager/latest/userguide/manage_create-basic-secret.html)。選擇 **Amazon Redshift 叢集**做為秘密類型。此外，在**秘密輪換**頁面上，開啟輪換。這將在 Amazon Redshift 叢集中建立適當的使用者，並以定義的間隔輪換金鑰秘密。 | 資料工程師 | 
| 建立 IAM 政策以限制 Secrets Manager 存取。 | 限制 Secrets Manager 只能存取 Amazon Redshift 管理員和 AWS Glue。 | 資料工程師 | 

### 設定 AWS Glue
<a name="configure-aws-glue"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在 AWS Glue Data Catalog 中，新增 Amazon Redshift 的連線。 | 如需說明，請參閱 [AWS Glue 文件](https://docs.aws.amazon.com/glue/latest/dg/console-connections.html)。 | 資料工程師 | 
| 建立並連接 IAM 服務角色，讓 AWS Glue 存取 Secrets Manager、Amazon Redshift 和 S3 儲存貯體。 | 如需詳細資訊，請參閱 [AWS Glue 文件](https://docs.aws.amazon.com/glue/latest/dg/create-service-policy.html)。 | 資料工程師 | 
| 定義來源的 AWS Glue Data Catalog。 | 此步驟涉及在 AWS Glue Data Catalog 中建立資料庫和所需的資料表。您可以使用爬蟲程式來編目 AWS Glue 資料庫中的資料表，或將其定義為 Amazon Athena 外部資料表。您也可以透過 AWS Glue Data Catalog 存取 Athena 中定義的外部資料表。如需在 Athena 中[定義 Data Catalog ](https://docs.aws.amazon.com/glue/latest/dg/populate-data-catalog.html)和[建立外部資料表](https://docs.aws.amazon.com/athena/latest/ug/creating-tables.html)的詳細資訊，請參閱 AWS 文件。 | 資料工程師 | 
| 建立 AWS Glue 任務來處理來源資料。 | AWS Glue 任務可以是 Python shell 或 PySpark，以標準化、刪除重複和清除來源資料檔案。若要最佳化效能並避免必須查詢整個 S3 來源儲存貯體，請依日期分割 S3 儲存貯體，並依年、月、日和小時細分，做為 AWS Glue 任務的下推述詞。如需詳細資訊，請參閱 [AWS Glue 文件](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-partitions.html)。將已處理和轉換的資料載入 Parquet 格式的已處理 S3 儲存貯體分割區。您可以從 Athena 查詢 Parquet 檔案。 | 資料工程師 | 
| 建立 AWS Glue 任務以將資料載入 Amazon Redshift。 | AWS Glue 任務可以是 Python shell 或 PySpark，透過維護資料載入資料，然後進行完整重新整理。如需詳細資訊，請參閱 [AWS Glue 文件](https://docs.aws.amazon.com/glue/latest/dg/author-job.html)和*其他資訊*一節。 | 資料工程師 | 
| （選用） 視需要使用觸發條件來排程 AWS Glue 任務。 | 增量資料載入主要是由導致 AWS Lambda 函數呼叫 AWS Glue 任務的 Amazon S3 事件所驅動。針對需要時間型而非事件型排程的任何資料載入，使用 AWS Glue 觸發型排程。 | 資料工程師 | 

### 建立 Lambda 函數
<a name="create-a-lambda-function"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立並連接 AWS Lambda 的 IAM 服務連結角色，以存取 S3 儲存貯體和 AWS Glue 任務。 | 為 AWS Lambda 建立 IAM 服務連結角色，其中包含讀取 Amazon S3 物件和儲存貯體的政策，以及存取 AWS Glue API 以啟動 AWS Glue 任務的政策。如需詳細資訊，請參閱 [知識中心](https://aws.amazon.com/premiumsupport/knowledge-center/lambda-execution-role-s3-bucket/)。 | 資料工程師 | 
| 建立 Lambda 函數，根據定義的 Amazon S3 事件執行 AWS Glue 任務。 | Lambda 函數應該透過建立 Amazon S3 資訊清單檔案來啟動。Lambda 函數應將 Amazon S3 資料夾位置 （例如 source\$1bucket/year/month/date/hour) 做為參數傳遞至 AWS Glue 任務。AWS Glue 任務會使用此參數做為下推述詞，以最佳化檔案存取和任務處理效能。如需詳細資訊，請參閱 [AWS Glue 文件](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-python-calling.html)。 | 資料工程師 | 
| 建立 Amazon S3 PUT 物件事件以偵測物件建立，並呼叫個別的 Lambda 函數。 | Amazon S3 PUT 物件事件只能透過建立資訊清單檔案來啟動。資訊清單檔案控制 Lambda 函數和 AWS Glue 任務並行，並以批次方式處理負載，而不是處理抵達 S3 來源儲存貯體特定分割區的個別檔案。如需詳細資訊，請參閱 [Lambda 文件](https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html)。 | 資料工程師 | 

## 相關資源
<a name="build-an-etl-service-pipeline-to-load-data-incrementally-from-amazon-s3-to-amazon-redshift-using-aws-glue-resources"></a>
+ [Amazon S3 文件](https://docs.aws.amazon.com/AmazonS3/latest/gsg/GetStartedWithS3.html)
+ [AWS Glue 文件](https://docs.aws.amazon.com/glue/latest/dg/what-is-glue.html)
+ [Amazon Redshift 文件](https://docs.aws.amazon.com/redshift/latest/gsg/getting-started.html)
+ [AWS Lambda](https://aws.amazon.com/lambda/)
+ [Amazon Athena](https://aws.amazon.com/athena/)
+ [AWS Secrets Manager](https://aws.amazon.com/secrets-manager/)

## 其他資訊
<a name="build-an-etl-service-pipeline-to-load-data-incrementally-from-amazon-s3-to-amazon-redshift-using-aws-glue-additional"></a>

**upsert 和完整重新整理的詳細方法**

**Upsert**：這適用於需要歷史彙總的資料集，取決於業務使用案例。根據您的業務需求，遵循[更新和插入新資料](https://docs.aws.amazon.com/redshift/latest/dg/t_updating-inserting-using-staging-tables-.html) (Amazon Redshift 文件） 中所述的方法之一。

**完整重新整理**：這適用於不需要歷史彙總的小型資料集。請遵循下列其中一種方法：

1. 截斷 Amazon Redshift 資料表。

1. 從預備區域載入目前的分割區

或：

1. 使用目前的分割區資料建立暫存資料表。

1. 捨棄目標 Amazon Redshift 資料表。

1. 將暫存資料表重新命名為目標資料表。