

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

# 搭配 EMR Serverless 使用 Python 程式庫
<a name="using-python-libraries"></a>

當您在 Amazon EMR Serverless 應用程式上執行 PySpark 任務時， 會將各種 Python 程式庫封裝為相依性。若要這樣做，請使用原生 Python 功能、建置虛擬環境，或直接將 PySpark 任務設定為使用 Python 程式庫。此頁面涵蓋每個方法。

## 使用原生 Python 功能
<a name="using-native-python-features"></a>

當您設定下列組態時，請使用 PySpark 將 Python 檔案 (`.py`)、壓縮的 Python 套件 (`.zip`) 和 Egg 檔案 (`.egg`) 上傳至 Spark 執行器。

```
--conf spark.submit.pyFiles=s3://amzn-s3-demo-bucket/EXAMPLE-PREFIX/<.py|.egg|.zip file>
```

如需如何針對 PySpark 任務使用 Python 虛擬環境的詳細資訊，請參閱[使用 PySpark 原生功能](https://spark.apache.org/docs/latest/api/python/tutorial/python_packaging.html#using-pyspark-native-features)。

使用 EMR 筆記本時，您可以執行下列程式碼，在筆記本中提供 Python 相依性：

```
    %%configure -f
 {
    "conf": {
    "spark.submit.pyFiles":"s3:///amzn-s3-demo-bucket/EXAMPLE-PREFIX/<.py|.egg|.zip file>
                   }
 }
```

## 建置 Python 虛擬環境
<a name="building-python-virtual-env"></a>

若要封裝 PySpark 任務的多個 Python 程式庫，請建立隔離的 Python 虛擬環境。

1. 若要建置 Python 虛擬環境，請使用下列命令。顯示的範例會將 套件`scipy`和 安裝`matplotlib`到虛擬環境套件中，並將封存複製到 Amazon S3 位置。
**重要**  
您必須在與 EMR Serverless 中使用的 Python 版本相同的類似 Amazon Linux 2 環境中執行下列命令，也就是 Amazon EMR 6.6.0 版的 Python 3.7.10。您可以在 [EMR Serverless Samples](https://github.com/aws-samples/emr-serverless-samples/tree/main/examples/pyspark/dependencies) GitHub 儲存庫中找到範例 Dockerfile。

   ```
   # initialize a python virtual environment
   python3 -m venv pyspark_venvsource
   source pyspark_venvsource/bin/activate
   
   # optionally, ensure pip is up-to-date
   pip3 install --upgrade pip
   
   # install the python packages
   pip3 install scipy
   pip3 install matplotlib
   
   # package the virtual environment into an archive
   pip3 install venv-pack
   venv-pack -f -o pyspark_venv.tar.gz
   
   # copy the archive to an S3 location
   aws s3 cp pyspark_venv.tar.gz s3://amzn-s3-demo-bucket/EXAMPLE-PREFIX/
   
   # optionally, remove the virtual environment directory
   rm -fr pyspark_venvsource
   ```

1. 使用屬性集提交 Spark 任務，以使用 Python 虛擬環境。

   ```
   --conf spark.archives=s3://amzn-s3-demo-bucket/EXAMPLE-PREFIX/pyspark_venv.tar.gz#environment 
   --conf spark.emr-serverless.driverEnv.PYSPARK_DRIVER_PYTHON=./environment/bin/python
   --conf spark.emr-serverless.driverEnv.PYSPARK_PYTHON=./environment/bin/python 
   --conf spark.executorEnv.PYSPARK_PYTHON=./environment/bin/python
   ```

   請注意，如果您不覆寫原始 Python 二進位檔，上一序列設定中的第二個組態將會是 `--conf spark.executorEnv.PYSPARK_PYTHON=python`。

   如需如何為 PySpark 任務使用 Python 虛擬環境的詳細資訊，請參閱[使用 Virtualenv](https://spark.apache.org/docs/latest/api/python/tutorial/python_packaging.html#using-virtualenv)。如需如何提交 Spark 任務的更多範例，請參閱 [執行 EMR Serverless 任務時使用 Spark 組態](jobs-spark.md)。

## 設定 PySpark 任務以使用 Python 程式庫
<a name="configuring-pyspark-jobs"></a>

使用 Amazon EMR 6.12.0 版及更高版本，您可以直接設定 EMR Serverless PySpark 任務，以使用熱門的資料科學 Python 程式庫，例如 [pandas](https://pandas.pydata.org/docs/user_guide/index.html)、[NumPy](https://numpy.org/doc/stable/user/index.html) 和 [PyArrow](https://arrow.apache.org/docs/python/index.html)，而無需任何其他設定。

下列範例示範如何封裝 PySpark 任務的每個 Python 程式庫。

------
#### [ NumPy ]

NumPy 是適用於科學運算的 Python 程式庫，可為數學、排序、隨機模擬和基本統計資料提供多維陣列和操作。若要使用 NumPy，請執行下列命令：

```
import numpy
```

------
#### [ pandas ]

pandas 是建置在 NumPy 上的 Python 程式庫。pandas 程式庫為資料科學家提供 [DataFrame](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html) 資料結構和資料分析工具。若要使用 pandas，請執行下列命令：

```
import pandas
```

------
#### [ PyArrow ]

PyArrow 是 Python 程式庫，可管理記憶體內單欄式資料，以提升任務效能。PyArrow 是以 Apache Arrow 跨語言開發規格為基礎，這是以單欄式格式表示和交換資料的標準方法。若要使用 PyArrow，請執行下列命令：

```
import pyarrow
```

------