

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 AWS Glue 作业和 Python 生成测试数据
<a name="generate-test-data-using-an-aws-glue-job-and-python"></a>

*Moinul Al-Mamun，Amazon Web Services*

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

此模式向您展示如何通过创建用 Python 编写的 AWS Glue 作业，快速轻松地同时生成数百万个示例文件。示例文件存储在 Amazon Simple Storage Service（Amazon S3）存储桶中。能够快速生成大量示例文件对于在 Amazon Web Services Cloud 中测试或评估服务非常重要。例如，您可以通过对数百万个 Amazon S3 前缀的小文件进行数据分析来测试 AWS Glue Studio 或 AWS Glue DataBrew 任务的性能。

尽管您可以使用其他 Amazon Web Services 生成示例数据集，但我们建议您使用 AWS Glue。您无需管理任何基础设施，因为 AWS Glue 是一项无服务器数据处理服务。您只需带上代码并在 AWS Glue 集群中运行即可。此外，AWS Glue 还预调配、配置和扩展运行作业所需资源。您只需为作业运行时使用的资源付费。

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

**先决条件**
+ 一个有效的 Amazon Web Services account
+ AWS 命令行界面（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)以使用 Amazon Web Services account 

**产品版本**
+ Python 3.9
+ AWS CLI 版本 2

**限制**

每个触发器的最大 AWS Glue 作业数是 50。有关更多信息，请参阅 [AWS Glue 端点和限额](https://docs.aws.amazon.com/general/latest/gr/glue.html)。

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

下图描绘了一个以 AWS Glue 作业为中心的示例架构，该作业将其输出（即示例文件）写入 S3 存储桶。

![工作流显示 AWS CLI 启动将输出写入 S3 存储桶的 AWS Glue 任务。](http://docs.aws.amazon.com/zh_cn/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 对象存储在通用前缀下。每个文件小于 1 千字节。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 命令行界面（AWS CLI）](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)是一种开源工具，它可帮助您通过命令行 Shell 中的命令与 Amazon Web Services 交互。
+ [AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/what-is-glue.html) 是一项完全托管的提取、转换、加载（ETL）服务。它可以帮助您在数据存储和数据流之间对数据进行可靠地分类、清理、扩充和移动。
+ [AWS Identity and Access Management (AWS IAM) ](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html)通过控制验证和授权使用您 AWS 资源的用户，帮助您安全地管理对您 AWS 资源的访问。

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

在实施此模式时，请考虑以下 AWS Glue 最佳实践：
+ **使用正确的 AWS Glue Worker 类型来降低成本。**我们建议您了解 Worker 类型的不同属性，然后根据 CPU 和内存要求为工作负载选择正确的 Worker 类型。对于这种模式，我们建议您使用 Python Shell 作业作为作业类型，以最大限度地减少 DPU 并降低成本。有关更多信息，请参阅 AWS Glue 开发人员指南中的[在 AWS Glue 中添加作业](https://docs.aws.amazon.com/glue/latest/dg/add-job.html)。
+ **使用正确的并发限制来扩展作业。**我们建议您根据自己的时间要求和所需文件数量来确定 AWS Glue 作业的最大并发度。
+ **首先开始生成少量文件。**为了在构建 AWS Glue 作业时降低成本并节省时间，请从少量文件（例如 1,000 个）开始。这样可以更轻松地进行故障排除。如果成功生成少量文件，则可以扩展到更多数量的文件。
+ **首先在本地运行。**为了在构建 AWS Glue 作业时降低成本并节省时间，请在本地开始开发并测试代码。有关设置 Docker 容器以帮助您在 Shell 和集成式开发环境（IDE）中测试提取、转换、加载（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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建 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 角色。 | 您必须创建 IAM 角色，这样 AWS Glue 作业可以使用该角色写入 S3 存储桶。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建 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_cn/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_cn/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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 从命令行运行 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 命令（在使用特定的并行化系数生成指定数量的文件时所需），请运行以下 bash 代码（使用您的值）：<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_cn/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_cn/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_cn/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>

**参考**
+ [Registry of Open Data on 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>

**基准测试**

作为基准测试的一部分，该模式用于使用不同的并行化参数生成 1000 万个文件。下表显示测试输出：


| 
| 
| 并行化 | 作业运行生成的文件数 | 作业时长 | Speed | 
| --- |--- |--- |--- |
| 10 | 1000000 | 6 小时 40 分钟 | 很慢 | 
| 50 | 200,000 | 80 分钟 | 中 | 
| 100 | 100000 | 40 minutes | 快速 | 

如果要加快处理速度，可以在作业配置中配置更多的并发运行。您可以根据自己的要求轻松调整作业配置，但请记住，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 权限？*

您必须具有基于身份的策略，该策略允许对 S3 存储桶中的对象进行 `Write` 访问。有关更多信息，请参阅 Amazon S3 文档中的 [Amazon S3：允许对 S3 存储桶中的对象进行读写访问](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_s3_rw-bucket.html)。