如何在 AWS Glue 2.0 ETL 工作中使用外部 Python 程式庫?
我想在 AWS Glue 2.0 擷取、轉換和載入 (ETL) 工作中使用外部 Python 程式庫。
解決方案
使用 AWS Glue 2.0 版,您可以在工作層級安裝其他 Python 模組或不同版本。若要新增模組或變更現有模組的版本,請使用 --additional-python-modules 工作參數索引鍵,其具有包含以逗號區隔 Python 模組清單的值。這可讓您的 AWS Glue 2.0 ETL 工作使用 Python 套件安裝程式 (pip3) 安裝其他模組。
若要為 AWS Glue 工作安裝其他 Python 模組:
- 開啟 AWS Glue 主控台。
- 在導覽窗格中,選擇工作。
- 選取您想要在其中新增 Python 模組的工作。
- 選擇動作,然後選擇編輯工作。
- 展開安全組態、指令碼程式庫和工作參數 (選用) 區段。
- 在工作參數下,執行下列動作:
對於索引鍵,輸入 --additional-python-modules。
對於值,輸入 pymysql==1.0.2, s3://aws-glue-add-modules/nltk-3.6.2-py3-none-any.whl。 - 選擇儲存。
下列步驟提供安裝兩個不同模組的範例:
- 透過網際網路安裝 PyMySQL
- 從 Amazon Simple Storage Service (Amazon S3) 上的 wheel 檔案安裝自然語言工具箱 (NLTK)
安裝新模組或更新現有模組需要下載模組相關的相依項。這表示您必須具有網際網路存取權才可完成其中一項任務。如果您沒有網際網路存取權,請參閱使用 AWS Glue 2.0 從 wheel 為 Spark ETL 工作負載建立 Python 模組。
如需 AWS Glue 2.0 中已提供的其他 Python 模組清單,請參閱 AWS Glue 2.0 版中已提供的 Python 模組。
AWS Glue 2.0 也支援使用 --additional-python-modules 選項以 C 語言編寫的程式庫和擴充模組。但是,Python 模組的子集 (例如 spacy 和 grpc) 需要根使用者許可才可安裝。如果沒有根使用者許可,這些模組的編譯在安裝過程中會失敗。AWS Glue 在套件安裝期間不會提供根使用者存取權。解決方案是將二進位檔案預先編譯成與 AWS Glue 相容的 wheel,然後安裝該 wheel。
若要以 C 語言編譯程式庫,編譯器必須與目標作業系統及處理器架構相容。如果程式庫是根據不同的作業系統或處理器架構編譯,則 wheel 並未安裝在 AWS Glue 中。因為 AWS Glue 為受管服務,所以不會提供叢集存取權來開發這些相依項。若要預先編譯需要根使用者許可的 C 語言 Python 模組,請執行下列動作:
**注意事項:**這些步驟會提供安裝 grpcio 模組的範例。
1. 啟動具有足夠磁碟區空間供程式庫使用的 Amazon Elastic Compute Cloud (Amazon EC2) Linux 執行個體。
2. 在執行個體上安裝 Docker 容器,設定非 sudo 存取權,然後啟動 docker。
sudo yum install docker -y sudo usermod -a -G docker ec2-user sudo service docker start
3. 建立檔案 dockerfile_grpcio 並將下列項目複製到檔案中:
# Base for AWS Glue FROM amazonlinux RUN yum update -y RUN yum install shadow-utils.x86_64 -y RUN yum install -y java-1.8.0-openjdk.x86_64 RUN yum install -y python3 RUN yum install -y cython doxygen numpy scipy gcc autoconf automake libtool zlib-devel openssl-devel maven wget protobuf-compiler cmake make gcc-c++ # Additional components needed for grpcio WORKDIR /root RUN yum install python3-devel -y RUN yum install python-devel -y RUN pip3 install wheel # Install grpcio and related modules RUN pip3 install Cython RUN pip3 install cmake scikit-build RUN pip3 install grpcio # Create a directory for the wheel RUN mkdir wheel_dir # Create the wheel RUN pip3 wheel grpcio -w wheel_dir
4. 執行 docker build 以構置您的 Dockerfile。執行下列命令以重新啟動 Docker 常駐程式:
$ sudo service docker restart $ docker build -f dockerfile_grpcio .
docker build 命令完成時,即會顯示一則包含 Docker 映像檔 ID 的成功訊息。例如 "Successfully built 1111222233334444"。 記下要在下一步中使用的 Docker 映像檔 ID。
5. 從 Docker 容器中擷取 wheel 檔案。執行下列命令以擷取 .whl 檔案:
# Get the docker image ID $ docker image ls # Run the container $ docker run -dit 111122223334444 # Verify the location of the wheel file and retrieve the name of the wheel file $ docker exec -t -i 5555666677778888 ls /root/wheel_dir/ # Copy the wheel out of docker to EC2 $ docker cp 5555666677778888:/root/wheel_dir/doc-example-wheel .
確保取代上述命令中的下列值:
- 將 1111222233334444 取代為 Docker 映像檔 ID
- 將 5555666677778888 取代為容器 ID
- 將 doc-example-wheel 取代為產生的 wheel 檔案名稱
6. 執行下列命令以將 wheel 上傳至 Amazon S3:
aws s3 cp doc-example-wheel s3://path/to/wheel/ aws s3 cp grpcio-1.32.0-cp37-cp37m-linux_x86_64.whl s3://aws-glue-add-modules/grpcio/
確保取代上述命令中的下列值:
- 將 doc-example-wheel 取代為產生的 wheel 檔案名稱
- 將 grpcio-1.32.0-cp37-cp37m-linux_x86_64.whl 取代為 Python 套件檔案的名稱
7. 對於 AWS Glue ETL 工作,在 AWS Glue 主控台的工作參數下,執行下列動作: 對於索引鍵,輸入 --additional-python-modules。
對於值,輸入 s3://aws-glue-add-modules/grpcio/grpcio-1.32.0-cp37-cp37m-linux_x86_64.whl。
**注意事項:**確保將 grpcio-1.32.0-cp37-cp37m-linux_x86_64.whl 取代為 Python 套件檔案的名稱。
**重要事項:**AWS Glue 0.9 版和 1.0 版不支援以 C 語言編寫的 Python 模組。若要在 AWS Glue 0.9 和 1.0 中安裝外部 Python 程式庫,請參閱如何在 AWS Glue 1.0 或 0.9 ETL 工作中使用外部 Python 程式庫?
相關資訊
相關內容
- 已提問 9 個月前lg...
- 已提問 1 年前lg...
- 已提問 10 個月前lg...
- AWS 官方已更新 2 年前
- AWS 官方已更新 1 年前
- AWS 官方已更新 1 年前
- AWS 官方已更新 1 年前