

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

# 在本機開發和測試 AWS Glue 任務指令碼
<a name="aws-glue-programming-etl-libraries"></a>

當您開發和測試 AWS Glue for Spark 任務指令碼時，有多個可用選項：
+ AWS Glue Studio 主控台
  + Visual editor (視覺化編輯器)
  + 指令碼編輯器
  + AWS Glue Studio 筆記本
+ 互動式工作階段
  + Jupyter 筆記本
+ Docker 映像檔
  + 本機開發
  + 遠端開發

您可根據自己的需求選擇上述任何選項。

如果您不偏好程式碼或更少的程式碼體驗，則 Glue Studio AWS 視覺化編輯器是不錯的選擇。

如果您偏好互動式筆記本體驗， AWS Glue Studio 筆記本是不錯的選擇。如需詳細資訊，請參閱[搭配使用筆記本與 AWS Glue Studio 和 AWS Glue](https://docs.aws.amazon.com/glue/latest/ug/notebooks-chapter.html)。如果想要使用自己的本機環境，那麼互動式工作階段是個不錯的選擇。如需詳細資訊，請參閱[搭配 Glue AWS 使用互動式工作階段](https://docs.aws.amazon.com/glue/latest/dg/interactive-sessions-chapter.html)。

如果您偏好採用本機/遠端開發，那麼 Docker 映像檔是個不錯的選擇。這可協助您在偏好的任何地方開發和測試 AWS Glue for Spark 任務指令碼，而不會產生 AWS Glue 成本。

如果您偏好不使用 Docker 進行本機開發，在本機安裝 AWS Glue ETL 程式庫目錄是不錯的選擇。

## 使用 Glue Studio AWS 進行開發
<a name="develop-using-studio"></a>

Glue Studio 視覺化編輯器是一種圖形界面，可讓您輕鬆地在 AWS Glue 中建立、執行和監控擷取、轉換和載入 (ETL) AWS 任務。您可以視覺化地編寫資料轉換工作流程，並在 AWS Glue 的 Apache Spark 型無伺服器 ETL 引擎上順暢地執行它們。您可以在任務的每個步驟中檢查結構描述和資料結果。如需詳細資訊，請參閱 [AWS Glue Studio 使用者指南](https://docs.aws.amazon.com/glue/latest/ug/what-is-glue-studio.html)。

## 使用互動式工作階段進行開發
<a name="develop-using-interactive-sessions"></a>

使用互動式工作階段允許您自行選擇要建置和測試應用程式的環境。如需詳細資訊，請參閱[搭配 Glue AWS 使用互動式工作階段](https://docs.aws.amazon.com/glue/latest/dg/interactive-sessions-chapter.html)。

# 使用 Docker 映像在本機開發和測試 AWS Glue 任務
<a name="develop-local-docker-image"></a>

 對於生產就緒的資料平台，開發程序和 AWS Glue 任務的 CI/CD 管道是關鍵主題。您可以在 Docker 容器中彈性開發和測試 AWS Glue 任務。在 Docker Hub 上 AWS Glue 託管 Docker 映像，以使用其他公用程式設定您的開發環境。您可以使用 ETL 程式庫來使用您偏好的 AWS Glue IDE、筆記本或 REPL。本主題說明如何使用 Docker 映像在 Docker 容器中開發和測試 5.0 AWS Glue 版任務。

## 可用的 Docker 映像檔
<a name="develop-local-available-docker-images-ecr"></a>

 下列 Docker 映像可在 Amazon ECR AWS Glue 上使用： [https://gallery.ecr.aws/glue/aws-glue-libs](https://gallery.ecr.aws/glue/aws-glue-libs)
+  對於 5.0 AWS Glue 版： `public.ecr.aws/glue/aws-glue-libs:5`
+ 對於 4.0 AWS Glue 版： `public.ecr.aws/glue/aws-glue-libs:glue_libs_4.0.0_image_01`
+ 對於 3.0 AWS Glue 版： `public.ecr.aws/glue/aws-glue-libs:glue_libs_3.0.0_image_01`
+ 對於 2.0 AWS Glue 版： `public.ecr.aws/glue/aws-glue-libs:glue_libs_2.0.0_image_01`

**注意**  
 AWS Glue Docker 映像與 x86\$164 和 arm64 相容。

 在此範例中，我們使用 `public.ecr.aws/glue/aws-glue-libs:5` 並在本機電腦 (Mac、Windows 或 Linux) 上執行容器。此容器映像已針對 5.0 AWS Glue 版 Spark 任務進行測試。該映像檔包含下列項目：
+  Amazon Linux 2023 
+  AWS Glue ETL 程式庫 
+  Apache Spark 3.5.4 
+  開放式資料表格式程式庫；Apache Iceberg 1.7.1、Apache Hudi 0.15.0 和 Delta Lake 3.3.0 
+  AWS Glue Data Catalog 用戶端 
+  Amazon Redshift Apache Spark 連接器 
+  Amazon DynamoDB Apache Hadoop 連接器 

 若要設定容器，請從 ECR 公共映像庫提取映像，然後執行容器。本主題示範如何使用下列方法執行您的容器，視您的需求而定：
+ `spark-submit`
+ REPL Shell `(pyspark)`
+ `pytest`
+ Visual Studio 程式碼

## 先決條件
<a name="develop-local-docker-image-prereq"></a>

在開始之前，請務必安裝 Docker 並確保 Docker 常駐程式正在執行。如需安裝說明，請參閱 [Mac](https://docs.docker.com/docker-for-mac/install/) 或 [Linux](https://docs.docker.com/engine/install/) 專用的 Docker 文件。執行 Docker 的機器託管 AWS Glue 容器。亦請確保執行 Docker 的主機上至少有 7 GB 的磁碟空間可供映像檔使用。

 如需在本機開發 AWS Glue 程式碼時限制的詳細資訊，請參閱[本機開發限制。 ](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-libraries.html#local-dev-restrictions)

### 設定 AWS
<a name="develop-local-docker-image-config-aws-credentials"></a>

若要從容器啟用 AWS API 呼叫，請依照下列步驟設定 AWS 登入資料。在下列各節中，我們將使用此 AWS 具名設定檔。

1.  [ 建立 AWS 具名設定檔 ](https://docs.aws.amazon.com//cli/latest/userguide/cli-configure-files.html)。

1.  開啟 Windows 上的 `cmd` 或 Mac/Linux 上的終端，並在終端執行下列命令：

   ```
   PROFILE_NAME="<your_profile_name>"
   ```

在下列各節中，我們會使用此 AWS 具名設定檔。

### 
<a name="develop-local-docker-pull-image-from-ecr-public"></a>

 如果在 Windows 上執行 Docker，請選擇 Docker 圖示 (按一下滑鼠右鍵)，並選擇**切換至 Linux 容器...**，然後再提取映像檔。

執行以下命令可從 ECR Public 中提取映像檔：

```
docker pull public.ecr.aws/glue/aws-glue-libs:5 
```

## 執行容器
<a name="develop-local-docker-image-setup-run"></a>

然後再使用此映像檔執行容器。您可根據自己的需求選擇下列任何選項。

### spark-submit
<a name="develop-local-docker-image-setup-run-spark-submit"></a>

您可以在容器上執行 `spark-submit`命令，以執行 AWS Glue 任務指令碼。

1.  撰寫指令碼並將其儲存為下面範例中的 `sample.py`，然後使用下列命令將其儲存在 `/local_path_to_workspace/src/` 型錄下：

   ```
   $ WORKSPACE_LOCATION=/local_path_to_workspace
   $ SCRIPT_FILE_NAME=sample.py
   $ mkdir -p ${WORKSPACE_LOCATION}/src
   $ vim ${WORKSPACE_LOCATION}/src/${SCRIPT_FILE_NAME}
   ```

1.  這些變數用於下面的 docker run 命令。下面 spark-submit 命令中使用的範例程式碼 (sample.py) 包含在本主題結尾的附錄中。

    執行下列命令可在容器上執行 `spark-submit` 命令以提交新的 Spark 應用程式：

   ```
   $ docker run -it --rm \
       -v ~/.aws:/home
       /hadoop/.aws \
       -v $WORKSPACE_LOCATION:/home/hadoop/workspace/ \
       -e AWS_PROFILE=$PROFILE_NAME \
       --name glue5_spark_submit \
       public.ecr.aws/glue/aws-glue-libs:5 \
       spark-submit /home/hadoop/workspace/src/$SCRIPT_FILE_NAME
   ```

1. (選用) 設定 `spark-submit` 以符合您的環境。例如，可以將相依項與 `--jars` 組態一起傳遞。如需詳細資訊，請參閱 Spark 文件中的[動態載入 Spark 屬性](https://spark.apache.org/docs/latest/configuration.html)。

### REPL Shell (Pyspark)
<a name="develop-local-docker-image-setup-run-repl-shell"></a>

 您可以執行 REPL (`read-eval-print loops`) Shell 進行互動式開發。執行以下命令可在容器上執行 PySpark 命令以啟動 REPL Shell：

```
$ docker run -it --rm \
    -v ~/.aws:/home/hadoop/.aws \
    -e AWS_PROFILE=$PROFILE_NAME \
    --name glue5_pyspark \
    public.ecr.aws/glue/aws-glue-libs:5 \
    pyspark
```

 您將會看到下列輸出：

```
Python 3.11.6 (main, Jan  9 2025, 00:00:00) [GCC 11.4.1 20230605 (Red Hat 11.4.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /__ / .__/\_,_/_/ /_/\_\   version 3.5.4-amzn-0
      /_/

Using Python version 3.11.6 (main, Jan  9 2025 00:00:00)
Spark context Web UI available at None
Spark context available as 'sc' (master = local[*], app id = local-1740643079929).
SparkSession available as 'spark'.
>>>
```

 使用此 REPL Shell，您能以互動方式編寫程式碼和進行測試。

### Pytest
<a name="develop-local-docker-image-setup-run-pytest"></a>

 對於單元測試，您可以將 `pytest` 用於 AWS Glue Spark 任務指令碼。執行下列命令以完成前置作業。

```
$ WORKSPACE_LOCATION=/local_path_to_workspace
$ SCRIPT_FILE_NAME=sample.py
$ UNIT_TEST_FILE_NAME=test_sample.py
$ mkdir -p ${WORKSPACE_LOCATION}/tests
$ vim ${WORKSPACE_LOCATION}/tests/${UNIT_TEST_FILE_NAME}
```

 執行下列命令，以使用 `docker run` 執行 `pytest`：

```
$ docker run -i --rm \
    -v ~/.aws:/home/hadoop/.aws \
    -v $WORKSPACE_LOCATION:/home/hadoop/workspace/ \
    --workdir /home/hadoop/workspace \
    -e AWS_PROFILE=$PROFILE_NAME \
    --name glue5_pytest \
    public.ecr.aws/glue/aws-glue-libs:5 \
    -c "python3 -m pytest --disable-warnings"
```

 在 `pytest` 完成執行單元測試後，您的輸出將如下所示：

```
============================= test session starts ==============================
platform linux -- Python 3.11.6, pytest-8.3.4, pluggy-1.5.0
rootdir: /home/hadoop/workspace
plugins: integration-mark-0.2.0
collected 1 item

tests/test_sample.py .                                                   [100%]

======================== 1 passed, 1 warning in 34.28s =========================
```

### 將容器設為使用 Visual Studio Code
<a name="develop-local-docker-image-setup-visual-studio"></a>

 若要使用 Visual Studio Code 設定容器，請完成下列步驟：

1. 安裝 Visual Studio Code。

1. 安裝 [Python](https://marketplace.visualstudio.com/items?itemName=ms-python.python)。

1. 安裝 [Visual Studio Code Remote - Containers](https://code.visualstudio.com/docs/remote/containers)

1. 在 Visual Studio Code 中開啟 workspace (工作區) 資料夾。

1. 按 `Ctrl+Shift+P` (Windows/Linux) 或 `Cmd+Shift+P` (Mac)。

1. 輸入 `Preferences: Open Workspace Settings (JSON)`。

1. 按 Enter。

1. 將下列 JSON 貼上並儲存。

   ```
   {
       "python.defaultInterpreterPath": "/usr/bin/python3.11",
       "python.analysis.extraPaths": [
           "/usr/lib/spark/python/lib/py4j-0.10.9.7-src.zip:/usr/lib/spark/python/:/usr/lib/spark/python/lib/",
       ]
   }
   ```

 若要設定容器，請執行以下操作：

1. 執行 Docker 容器。

   ```
   $ docker run -it --rm \
       -v ~/.aws:/home/hadoop/.aws \
       -v $WORKSPACE_LOCATION:/home/hadoop/workspace/ \
       -e AWS_PROFILE=$PROFILE_NAME \
       --name glue5_pyspark \
       public.ecr.aws/glue/aws-glue-libs:5 \
       pyspark
   ```

1. 啟動 Visual Studio Code。

1.  選擇左側選單中的 **Remote Explorer** (遠端檔案總管)，然後選擇 `amazon/aws-glue-libs:glue_libs_4.0.0_image_01`。

1.  按一下滑鼠右鍵，然後選擇**在目前視窗中連接**。  
![\[按滑鼠右鍵時，會出現一個視窗，其中包含「在目前視窗中連接」選項。\]](http://docs.aws.amazon.com/zh_tw/glue/latest/dg/images/vs-code-other-containers.png)

1.  如果出現下列對話方塊，請選擇**了解**。  
![\[視窗警告，其中包含「連接至容器可能會執行任意程式碼」訊息。\]](http://docs.aws.amazon.com/zh_tw/glue/latest/dg/images/vs-code-warning-got-it.png)

1. 打開 `/home/handoop/workspace/`。  
![\[含有「工作區」選項的視窗下拉式清單會反白顯示。\]](http://docs.aws.amazon.com/zh_tw/glue/latest/dg/images/vs-code-open-workspace.png)

1.  建立 a AWS Glue PySpark 指令碼，然後選擇**執行**。

   您會看到指令碼成功執行。  
![\[指令碼成功執行。\]](http://docs.aws.amazon.com/zh_tw/glue/latest/dg/images/vs-code-run-successful-script.png)

## AWS Glue 4.0 和 AWS Glue 5.0 Docker 映像檔之間的變更
<a name="develop-local-docker-glue4-glue5-changes"></a>

 AWS Glue 4.0 和 AWS Glue 5.0 Docker 映像檔之間的主要變更：
+  在 AWS Glue 5.0 中，批次和串流任務都有單一容器映像。這與 Glue 4.0 不同，其中有一個映像用於批次，另一個用於串流。
+  在 AWS Glue 5.0 中，容器的預設使用者名稱為 `hadoop`。在 AWS Glue 4.0 中，預設使用者名稱為 `glue_user`。
+  在 AWS Glue 5.0 中，已從映像中移除數個額外的程式庫，包括 JupyterLab 和 Livy。您可以手動進行安裝。
+  在 AWS Glue 5.0 中，預設會預先載入所有 Iceberg、Hudi 和 Delta 程式庫，且`DATALAKE_FORMATS`不再需要環境變數。在 AWS Glue 4.0 之前，環境變數`DATALAKE_FORMATS`環境變數用於指定應載入哪些特定資料表格式。

 上述清單特定於 Docker 映像檔案。若要進一步了解 AWS Glue 5.0 更新，請參閱適用於 [Apache Spark 的 Introducing AWS Glue 5.0 ](https://aws.amazon.com/blogs/big-data/introducing-aws-glue-5-0-for-apache-spark/) 和[AWS Glue 適用於 Spark 任務的遷移至 5.0 AWS Glue 版。](https://docs.aws.amazon.com/glue/latest/dg/migrating-version-50.html)

## 考量事項
<a name="develop-local-docker-considerations"></a>

 請注意，使用 AWS Glue 容器映像在本機開發任務指令碼時，不支援下列功能。
+  [任務書籤](https://docs.aws.amazon.com/glue/latest/dg/monitor-continuations.html) 
+  AWS Glue Parquet 寫入器 ([在 中使用 Parquet 格式 AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-format-parquet-home.html)) 
+  [FillMissingValues 轉換](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-api-crawler-pyspark-transforms-fillmissingvalues.html) 
+  [FindMatches 轉換](https://docs.aws.amazon.com/glue/latest/dg/machine-learning.html#find-matches-transform) 
+  [向量化 SIMD CSV 讀取器](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-format-csv-home.html#aws-glue-programming-etl-format-simd-csv-reader) 
+  屬性 [customJdbcDriverS3Path](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-connect.html#aws-glue-programming-etl-connect-jdbc)，用於從 Amazon S3 路徑載入 JDBC 驅動器 
+  [AWS Glue 資料品質](https://docs.aws.amazon.com/glue/latest/dg/glue-data-quality.html) 
+  [敏感資料偵測](https://docs.aws.amazon.com/glue/latest/dg/detect-PII.html) 
+  AWS Lake Formation 以許可為基礎的登入資料販賣 

## 附錄：新增 JDBC 驅動器和 Java 程式庫
<a name="develop-local-docker-image-appendix"></a>

 若要新增目前容器中無法使用的 JDBC 驅動器，您可以使用所需的 JAR 檔案在工作區下建立新的型錄，並在 docker run 命令中將型錄掛載至 `/opt/spark/jars/`。位於容器內 `/opt/spark/jars/` 下的 JAR 檔案會自動新增至 Spark Classpath，並在任務執行期間可供使用。

 例如，使用下列 docker run 命令，將 JDBC 驅動器 jar 新增至 PySpark REPL Shell。

```
docker run -it --rm \
    -v ~/.aws:/home/hadoop/.aws \
    -v $WORKSPACE_LOCATION:/home/hadoop/workspace/ \
    -v $WORKSPACE_LOCATION/jars/:/opt/spark/jars/ \
    --workdir /home/hadoop/workspace \
    -e AWS_PROFILE=$PROFILE_NAME \
    --name glue5_jdbc \
    public.ecr.aws/glue/aws-glue-libs:5 \
    pyspark
```

 如**考量**中反白顯示，`customJdbcDriverS3Path`連線選項無法用於從 AWS Glue 容器映像中的 Amazon S3 匯入自訂 JDBC 驅動程式。