

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

# 使用 AWS Glue 任務和 Python 產生測試資料
<a name="generate-test-data-using-an-aws-glue-job-and-python"></a>

*Moinul Al-Mamun，Amazon Web Services*

## 總結
<a name="generate-test-data-using-an-aws-glue-job-and-python-summary"></a>

此模式說明如何建立以 Python 撰寫的 AWS Glue 任務，以快速且輕鬆地同時產生數百萬個範例檔案。範例檔案存放在 Amazon Simple Storage Service (Amazon S3) 儲存貯體中。快速產生大量範例檔案的功能對於測試或評估 AWS 雲端中的服務至關重要。例如，您可以對 Amazon S3 字首中的數百萬個小型檔案執行資料分析，以測試 AWS Glue Studio 或 AWS Glue DataBrew 任務的效能。

雖然您可以使用其他 AWS 服務來產生範例資料集，但我們建議您使用 AWS Glue。您不需要管理任何基礎設施，因為 AWS Glue 是無伺服器資料處理服務。您可以攜帶程式碼，並在 AWS Glue 叢集中執行。此外，AWS Glue 會佈建、設定和擴展執行任務所需的資源。您只需為您的任務在執行時使用的資源付費。

## 先決條件和限制
<a name="generate-test-data-using-an-aws-glue-job-and-python-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ AWS Command Line Interface (AWS CLI)[，已安裝](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)並[設定為](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)使用 AWS 帳戶

**產品版本**
+ Python 3.9
+ AWS CLI 第 2 版

**限制**

每次觸發的 AWS Glue 任務數目上限為 50。如需詳細資訊，請參閱 [AWS Glue 端點和配額](https://docs.aws.amazon.com/general/latest/gr/glue.html)。

## Architecture
<a name="generate-test-data-using-an-aws-glue-job-and-python-architecture"></a>

下圖說明以 AWS Glue 任務為中心的架構範例，該任務會將其輸出 （即範例檔案） 寫入 S3 儲存貯體。

![工作流程顯示 AWS CLI 啟動 AWS Glue 任務，將輸出寫入 S3 儲存貯體。](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/f35943e8-3b2b-410e-a3f0-05e1ebd357d0/images/452ccbda-71f2-42b8-976d-bcc968bb1dab.png)


圖表包含下列工作流程：

1. 您可以使用 AWS CLI、AWS 管理主控台或 API 來啟動 AWS Glue 任務。AWS CLI 或 API 可讓您自動化調用任務的平行化，並減少產生範例檔案的執行時間。

1. AWS Glue 任務會隨機產生檔案內容，將內容轉換為 CSV 格式，然後將內容儲存為通用字首下的 Amazon S3 物件。每個檔案小於 KB。AWS Glue 任務接受兩個使用者定義的任務參數： `START_RANGE`和 `END_RANGE`。您可以使用這些參數來設定檔案名稱，以及每個任務執行在 Amazon S3 中產生的檔案數目。您可以平行執行此任務的多個執行個體 （例如 100 個執行個體）。

## 工具
<a name="generate-test-data-using-an-aws-glue-job-and-python-tools"></a>
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種雲端型物件儲存服務，可協助您儲存、保護和擷取任何數量的資料。
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一種開放原始碼工具，可協助您透過命令列 shell 中的命令與 AWS 服務互動。
+ [AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/what-is-glue.html) 是全受管的擷取、轉換和載入 (ETL) 服務。它可協助您可靠地分類、清理、擴充和移動資料存放區和資料串流之間的資料。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 可透過控制已驗證並獲授權使用的人員，協助您安全地管理對 AWS 資源的存取。

## 最佳實務
<a name="generate-test-data-using-an-aws-glue-job-and-python-best-practices"></a>

實作此模式時，請考慮下列 AWS Glue 最佳實務：
+ **使用正確的 AWS Glue 工作者類型來降低成本。**我們建議您了解工作者類型的不同屬性，然後根據 CPU 和記憶體需求，為您的工作負載選擇正確的工作者類型。對於此模式，我們建議您使用 Python shell 任務作為任務類型，以將 DPU 降至最低並降低成本。如需詳細資訊，請參閱《[AWS Glue 開發人員指南》中的在 AWS Glue 中新增任務](https://docs.aws.amazon.com/glue/latest/dg/add-job.html)。 AWS Glue 
+ **使用正確的並行限制來擴展您的任務。**我們建議您根據時間需求和所需的檔案數量來建立 AWS Glue 任務的最大並行數量。
+ **首先開始產生少量檔案。**若要在建置 AWS Glue 任務時降低成本並節省時間，請從少量檔案 （例如 1，000) 開始。這可讓您更輕鬆地進行故障診斷。如果產生少量檔案成功，則可以擴展到更多檔案。
+ **請先在本機執行 。**若要在建置 AWS Glue 任務時降低成本並節省時間，請在本機開始開發並測試程式碼。如需設定 Docker 容器的說明，以協助您在 shell 和整合開發環境 (IDE) 中撰寫 AWS Glue 擷取、轉換和載入 (ETL) 任務，請參閱 [ AWS 大數據部落格上的使用容器文章在本機開發 AWS Glue ETL 任務](https://aws.amazon.com/blogs/big-data/developing-aws-glue-etl-jobs-locally-using-a-container/)。

如需更多 AWS Glue 最佳實務，請參閱 AWS Glue 文件中的[最佳實務](https://docs.aws.amazon.com/prescriptive-guidance/latest/serverless-etl-aws-glue/best-practices.html)。

## 史詩
<a name="generate-test-data-using-an-aws-glue-job-and-python-epics"></a>

### 建立目的地 S3 儲存貯體和 IAM 角色
<a name="create-a-destination-s3-bucket-and-iam-role"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 S3 儲存貯體以存放檔案。 | 建立 [S3 儲存貯](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html)體和其中的[字首](https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-prefixes.html)。此模式會將 `s3://{your-s3-bucket-name}/small-files/`位置用於示範用途。 | 應用程式開發人員 | 
| 建立和設定 IAM 角色。 | 您必須建立 AWS Glue 任務可用來寫入 S3 儲存貯體的 IAM 角色。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/generate-test-data-using-an-aws-glue-job-and-python.html) | 應用程式開發人員 | 

### 建立和設定 AWS Glue 任務以處理並行執行
<a name="create-and-configure-an-aws-glue-job-to-handle-concurrent-runs"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 AWS Glue 任務。 | 您必須建立產生內容的 AWS Glue 任務，並將其存放在 S3 儲存貯體中。<br />建立 [AWS Glue 任務](https://docs.aws.amazon.com/glue/latest/dg/console-jobs.html)，然後完成下列步驟來設定您的任務：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/generate-test-data-using-an-aws-glue-job-and-python.html) | 應用程式開發人員 | 
| 更新任務代碼。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/generate-test-data-using-an-aws-glue-job-and-python.html) | 應用程式開發人員 | 

### 從命令列或主控台執行 AWS Glue 任務
<a name="run-the-aws-glue-job-from-the-command-line-or-console"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 從命令列執行 AWS Glue 任務。 | 若要從 AWS CLI 執行您的 AWS Glue 任務，請使用您的值執行下列命令：<pre>cmd:~$ aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"0","--END_RANGE":"1000000"}'<br />cmd:~$ aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"1000000","--END_RANGE":"2000000"}'</pre>如需從 AWS 管理主控台執行 AWS Glue 任務的指示，請參閱此模式中 * AWS 管理主控台案例的執行 AWS Glue 任務*。如果您想要使用不同的參數一次執行多個執行，建議您使用 AWS CLI 來執行 AWS Glue 任務，如上述範例所示。<br />若要產生使用特定平行化因素產生定義數量的檔案所需的所有 AWS CLI 命令，請執行下列堡壘程式碼 （使用您的值）：<pre># define parameters<br />NUMBER_OF_FILES=10000000;<br />PARALLELIZATION=50; <br /> <br /># initialize<br />_SB=0;<br />      <br /># generate commands<br />for i in $(seq 1 $PARALLELIZATION); <br />do <br />      echo aws glue start-job-run --job-name create_small_files --arguments "'"'{"--START_RANGE":"'$(((NUMBER_OF_FILES/PARALLELIZATION) * (i-1) + _SB))'","--END_RANGE":"'$(((NUMBER_OF_FILES/PARALLELIZATION) * (i)))'"}'"'";<br />      _SB=1; <br />done</pre><br />如果您使用上述指令碼，請考慮下列事項：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/generate-test-data-using-an-aws-glue-job-and-python.html) 若要查看上述指令碼的輸出範例，請參閱此模式*的額外資訊*區段中的 *Shell 指令碼輸出*。 | 應用程式開發人員 | 
| 在 AWS 管理主控台中執行 AWS Glue 任務。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/generate-test-data-using-an-aws-glue-job-and-python.html) | 應用程式開發人員 | 
| 檢查 AWS Glue 任務的狀態。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/generate-test-data-using-an-aws-glue-job-and-python.html) | 應用程式開發人員 | 

## 相關資源
<a name="generate-test-data-using-an-aws-glue-job-and-python-resources"></a>

**參考**
+ [AWS 上開放資料的登錄檔](https://registry.opendata.aws/)
+ [用於分析的資料集](https://aws.amazon.com/marketplace/solutions/data-analytics/data-sets)
+ [在 AWS 上開啟資料](https://aws.amazon.com/opendata/)
+ [在 AWS Glue 中新增任務](https://docs.aws.amazon.com/glue/latest/dg/add-job.html)
+ [AWS Glue 入門](https://aws.amazon.com/glue/getting-started/)

**指南和模式**
+ [AWS Glue 最佳實務](https://docs.aws.amazon.com/prescriptive-guidance/latest/serverless-etl-aws-glue/best-practices.html)
+ [負載測試應用程式](https://docs.aws.amazon.com/prescriptive-guidance/latest/load-testing/welcome.html)

## 其他資訊
<a name="generate-test-data-using-an-aws-glue-job-and-python-additional"></a>

**基準測試**

此模式用於產生 1，000 萬個檔案，使用不同的平行化參數做為基準測試的一部分。下表顯示測試的輸出：


| 
| 
| 平行化 | 任務執行產生的檔案數目 | 任務持續時間 | 速度 | 
| --- |--- |--- |--- |
| 10 | 1,000,000 | 6 小時 40 分鐘 | 非常慢 | 
| 50 | 200,000 | 80 分鐘 | 適中 | 
| 100 | 100,000 | 40 分鐘 | 快速 | 

如果您想要讓程序更快，您可以在任務組態中設定更多並行執行。您可以根據您的需求輕鬆調整任務組態，但請記住，有 AWS Glue 服務配額限制。如需詳細資訊，請參閱 [AWS Glue 端點和配額](https://docs.aws.amazon.com/general/latest/gr/glue.html)。

**Shell 指令碼輸出**

下列範例顯示此模式中從*命令列故事執行 AWS Glue 任務*的 shell 指令碼輸出。

```
user@MUC-1234567890 MINGW64 ~
  $ # define parameters
  NUMBER_OF_FILES=10000000;
  PARALLELIZATION=50;
  # initialize
  _SB=0;
   
  # generate commands
  for i in $(seq 1 $PARALLELIZATION);
   do
         echo aws glue start-job-run --job-name create_small_files --arguments "'"'{"--START_RANGE":"'$(((NUMBER_OF_FILES/PARALLELIZATION)  (i-1) + SB))'","--ENDRANGE":"'$(((NUMBER_OF_FILES/PARALLELIZATION)  (i)))'"}'"'";
         _SB=1;
   done
   
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"0","--END_RANGE":"200000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"200001","--END_RANGE":"400000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"400001","--END_RANGE":"600000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"600001","--END_RANGE":"800000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"800001","--END_RANGE":"1000000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"1000001","--END_RANGE":"1200000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"1200001","--END_RANGE":"1400000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"1400001","--END_RANGE":"1600000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"1600001","--END_RANGE":"1800000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"1800001","--END_RANGE":"2000000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"2000001","--END_RANGE":"2200000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"2200001","--END_RANGE":"2400000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"2400001","--END_RANGE":"2600000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"2600001","--END_RANGE":"2800000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"2800001","--END_RANGE":"3000000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"3000001","--END_RANGE":"3200000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"3200001","--END_RANGE":"3400000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"3400001","--END_RANGE":"3600000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"3600001","--END_RANGE":"3800000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"3800001","--END_RANGE":"4000000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"4000001","--END_RANGE":"4200000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"4200001","--END_RANGE":"4400000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"4400001","--END_RANGE":"4600000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"4600001","--END_RANGE":"4800000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"4800001","--END_RANGE":"5000000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"5000001","--END_RANGE":"5200000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"5200001","--END_RANGE":"5400000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"5400001","--END_RANGE":"5600000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"5600001","--END_RANGE":"5800000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"5800001","--END_RANGE":"6000000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"6000001","--END_RANGE":"6200000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"6200001","--END_RANGE":"6400000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"6400001","--END_RANGE":"6600000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"6600001","--END_RANGE":"6800000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"6800001","--END_RANGE":"7000000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"7000001","--END_RANGE":"7200000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"7200001","--END_RANGE":"7400000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"7400001","--END_RANGE":"7600000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"7600001","--END_RANGE":"7800000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"7800001","--END_RANGE":"8000000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"8000001","--END_RANGE":"8200000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"8200001","--END_RANGE":"8400000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"8400001","--END_RANGE":"8600000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"8600001","--END_RANGE":"8800000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"8800001","--END_RANGE":"9000000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"9000001","--END_RANGE":"9200000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"9200001","--END_RANGE":"9400000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"9400001","--END_RANGE":"9600000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"9600001","--END_RANGE":"9800000"}'
  aws glue start-job-run --job-name create_small_files --arguments '{"--START_RANGE":"9800001","--END_RANGE":"10000000"}'
  
  user@MUC-1234567890 MINGW64 ~
```

**常見問答集**

*我應該使用多少個並行執行或平行任務？*

並行執行和平行任務的數量取決於您的時間需求和所需的測試檔案數量。建議您檢查正在建立的檔案大小。首先，檢查 AWS Glue 任務產生所需檔案數量所需的時間。然後，使用正確數量的並行執行來滿足您的目標。例如，如果您假設 100，000 個檔案需要 40 分鐘才能完成執行，但目標時間為 30 分鐘，則必須增加 AWS Glue 任務的並行設定。

*我可以使用此模式建立哪種類型的內容？*

您可以建立任何類型的內容，例如具有不同分隔符號的文字檔案 （例如 PIPE、JSON 或 CSV)。此模式使用 Boto3 寫入檔案，然後將檔案儲存在 S3 儲存貯體中。

*在 S3 儲存貯體中，我需要什麼層級的 IAM 許可？*

您必須擁有允許`Write`存取 S3 儲存貯體中物件的身分型政策。如需詳細資訊，請參閱 [Amazon S3 文件中的 Amazon S3：允許對 S3 儲存貯體中的物件進行讀取和寫入存取](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_s3_rw-bucket.html)。 Amazon S3 