如何在 AWS Glue 1.0 或 0.9 ETL 作業中使用外部 Python 程式庫?

2 分的閱讀內容
0

我想在 AWS Glue 1.0 或 0.9 擷取、轉換和載入 (ETL) 作業中使用外部 Python 程式庫。

簡短描述

若要在 Apache Spark ETL 作業中使用外部程式庫,請執行下列動作:

  1. 將程式庫檔案封裝在 .zip 檔案中 (除非程式庫包含在單一 .py 檔案中)。

  2. 將套件上傳到 Amazon Simple Storage Service (Amazon S3)。

  3. 在作業或 JobRun 中使用程式庫

解決方法

以下是如何在 Spark ETL AWS Glue 1.0 或 0.9 ETL 作業中使用外部程式庫的範例。

**重要:**如果您想要在 AWS Glue 2.0 任務中使用外部程式庫,請參閱如何在 AWS Glue 2.0 ETL 作業中使用外部 Python 程式庫?如果您想要在 Python Shell 作業中使用外部程式庫,請依照提供您自己的 Python 程式庫中的步驟執行。

1.    為 boto3 建立 Python 2 或 Python 3 程式庫。確定您使用的 AWS Glue 版本支援您為程式庫選擇的 Python 版本。AWS Glue 1.0 版支援 Python 2 和 Python 3,而 AWS Glue 版本 0.9 只支援 Python 2。

**注意:**Spark 作業的程式庫和延伸模組必須以 Python 編寫。在 Glue 0.9 或 1.0 中不支援以 C 語言撰寫的程式庫 (例如 pandas)。如果您需要使用以 C 語言撰寫的程式庫,請將 AWS Glue 至少升級至 2.0 版,並使用 --additional-python-modules 選項。如需詳細資訊,請參閱如何在 AWS Glue 2.0 ETL 作業中使用外部 Python 程式庫?

  1. 啟動 Amazon Elastic Compute Cloud (Amazon EC2) Linux 執行個體

  2. 使用 SSH 連線至 Linux 執行個體

  3. 執行以下命令來安裝 Python 和 Boto3。如需詳細資訊,請參閱 Boto3 快速入門文件。

sudo yum groupinstall "Development Tools"
sudo yum -y install openssl-devel
wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz
tar xvf Python-3.6.9.tgz
cd Python-3.6.9/
./configure --enable-optimizations
sudo make install
sudo pip install boto3
  1. 確認 Python site-packages 目錄的位置:
python -m site

您會收到類似下列內容的輸出:

/usr/lib/python3.6/site-packages
  1. 將外部程式庫檔案封裝在.zip 檔案中,除非程式庫包含在單一 .py 檔案中。.zip 檔案必須包含 \ _\ _init\ _\ _.py ** 檔案,且套件目錄必須位於封存的根目錄。__init__.py** 檔案可以是空的。如需詳細資訊,請參閱套件的 Python 文件。

範例:

cd /usr/lib/python3.6/site-packages
sudo zip -r -X "/home/ec2-user/site-packages.zip" *
  1. 將套件上傳到 Amazon S3:
aws s3 cp /home/ec2-user/site-packages.zip s3://awsexamplebucket/
  1. 在作業或 JobRun 中使用程式庫

若要在開發端點中使用外部程式庫,請執行下列動作:

  1. 如前所述,封裝程式庫並將檔案上傳到 Amazon S3。

  2. 建立開發端點。對於 Python 程式庫路徑,請輸入套件的 Amazon S3 路徑。如需詳細資訊,請參閱在開發端點中載入 Python 程式庫


相關資訊

搭配 AWSGlue 使用 Python 程式庫

AWS 官方
AWS 官方已更新 1 年前