

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

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

Apache Spark 是用于大规模数据处理的统一分析引擎。Amazon SageMaker AI 提供了预构建的 Docker 映像，包括 Apache Spark 和运行分布式数据处理作业所需的其他依赖项。下面举例说明如何使用 Apache Spark 运行 Amazon SageMaker Processing 作业。

通过 [Amazon SageMaker Python SDK](https://github.com/aws/sagemaker-python-sdk#installing-the-sagemaker-python-sdk)，您可以使用 Spark 框架轻松应用数据转换并提取特征（特征工程）。有关使用 SageMaker Python SDK 运行 Spark 处理作业的信息，请参阅 [Amazon SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable/) 中的[使用 Spark 进行数据处理](https://sagemaker.readthedocs.io/en/stable/amazon_sagemaker_processing.html#data-processing-with-spark)。

包含 Spark 映像的源代码和 Dockerfile 的代码存储库可在 [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 Processing 进行分布式数据处理”[示例笔记本](https://sagemaker-examples.readthedocs.io/en/latest/sagemaker_processing/spark_distributed_data_processing/sagemaker-spark-processing.html)。

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

 要了解有关结合使用 SageMaker Python SDK 和处理容器的更多信息，请参阅 [Amazon SageMaker AI Python SDK](https://sagemaker.readthedocs.io/en/stable/)。