

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

# 使用 Apache Spark 运行处理作业
<a name="use-spark-processing-container"></a>

Apache Spark 是用于大规模数据处理的统一分析引擎。Amazon SageMaker AI 提供预构建的 Docker 镜像，其中包括 Apache Spark 和运行分布式数据处理任务所需的其他依赖项。以下是如何使用 Apache Spark 运行亚马逊 SageMaker 处理任务的示例。

借助 [Amaz SageMaker on Python 软件开发工具包](https://github.com/aws/sagemaker-python-sdk#installing-the-sagemaker-python-sdk)，您可以使用 Spark 框架轻松应用数据转换和提取功能（功能工程）。有关使用 SageMaker Python 软件开发工具包运行 Spark 处理任务的信息，请参阅[亚马逊 P SageMaker ython 软件开发工具包](https://sagemaker.readthedocs.io/en/stable/)中使用 [Spark 进行数据处理](https://sagemaker.readthedocs.io/en/stable/amazon_sagemaker_processing.html#data-processing-with-spark)。

包含源代码和 Spark 镜像的 DockerFiles 的代码存储库可在上找到。[GitHub](https://github.com/aws/sagemaker-spark-container)

 您可以使用 [https://sagemaker.readthedocs.io/en/stable/api/training/processing.html#sagemaker.spark.processing.PySparkProcessor](https://sagemaker.readthedocs.io/en/stable/api/training/processing.html#sagemaker.spark.processing.PySparkProcessor) 或 [https://sagemaker.readthedocs.io/en/stable/api/training/processing.html#sagemaker.spark.processing.SparkJarProcessor](https://sagemaker.readthedocs.io/en/stable/api/training/processing.html#sagemaker.spark.processing.SparkJarProcessor) 类，在处理作业中运行 Spark 应用程序。请注意，您可以 MaxRuntimeInSeconds 将最大运行时间限制设置为 5 天。关于执行时间和使用的实例数量，简单的 Spark 工作负载会注意到实例数量与完成时间之间的近线性关系。

 以下代码示例说明如何运行调用脚 PySpark 本的处理作业。`preprocess.py`

```
from sagemaker.spark.processing import PySparkProcessor

spark_processor = PySparkProcessor(
    base_job_name="spark-preprocessor",
    framework_version="2.4",
    role=role,
    instance_count=2,
    instance_type="ml.m5.xlarge",
    max_runtime_in_seconds=1200,
)

spark_processor.run(
    submit_app="preprocess.py",
    arguments=['s3_input_bucket', bucket,
               's3_input_key_prefix', input_prefix,
               's3_output_bucket', bucket,
               's3_output_key_prefix', output_prefix]
)
```

 要深入了解，请参阅使用 Apache Spark 进行分布式数据处理和 SageMaker 处理[示例笔记本](https://sagemaker-examples.readthedocs.io/en/latest/sagemaker_processing/spark_distributed_data_processing/sagemaker-spark-processing.html)。

 如果您没有使用 [Amazon A SageMaker I Python SDK](https://sagemaker.readthedocs.io/) 及其处理器类之一来检索预先构建的图像，则可以自己检索这些图像。 SageMaker 预构建的 Docker 镜像存储在亚马逊弹性容器注册表 (Amazon ECR) Container Registry 中。有关可用的预构建 Docker 映像的完整列表，请参阅[可用的映像](https://github.com/aws/sagemaker-spark-container/blob/master/available_images.md)文档。

 要详细了解如何将 SageMaker Python 开发工具包与处理容器配合使用，请参阅[亚马逊 A SageMaker I Python 软件开发工具包](https://sagemaker.readthedocs.io/en/stable/)。