我如何在 AWS Glue 2.0 ETL 作业中使用外部 Python 库?
我想在 AWS Glue 2.0 提取、转换、加载(ETL)作业中使用外部 Python 库。
解决方法
使用 AWS Glue 2.0 版,您可以在作业级别安装其他 Python 模块或不同版本。要添加新模块或更改现有模块的版本,使用值包含以逗号分隔的 Python 模块列表的 --additional-python-modules 作业参数键。这样,您的 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)
安装新模块或更新现有模块需要下载模块相关依赖项。这意味着您必须能够访问互联网才能完成这些任务。如果您无法访问互联网,请参阅 Building Python modules from a wheel for Spark ETL workloads using AWS Glue 2.0。
如需 AWS Glue 2.0 中已提供的其他 Python 模块的列表,请参阅 Python modules already provided in AWS Glue version 2.0。
使用 C 编写的库和扩展模块也受具有 --additional-python-modules 选项的 AWS Glue 2.0 支持。但是,有一部分 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 库?
相关信息
相关内容
- AWS 官方已更新 1 年前
- AWS 官方已更新 1 年前
- AWS 官方已更新 1 年前
- AWS 官方已更新 1 年前