如何使用当前版本的 boto3 运行 AWS Glue Python Spark 作业?

How to run an AWS Glue Python Spark Job with the Current Version of boto3?

我正在尝试 运行 AWS Glue spark 作业中的最新版本的 boto3 以访问 Glue 默认版本中不可用的方法。

为了获取 boto3 的默认版本并验证我想要访问的方法不可用,我 运行 这个代码块除了我的 print 语句外都是样板文件:

import sys
import boto3
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job

## @params: [JOB_NAME]
args = getResolvedOptions(sys.argv, ['JOB_NAME'])

sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)

athena = boto3.client('athena')
print(boto3.__version__) # verify the default version boto3 imports
print(athena.list_table_metadata) # method I want to verify I can access in Glue

job.commit()

哪个returns

1.12.4

Traceback (most recent call last): File "/tmp/another_sample", line 20, in print(athena.list_table_metadata) File "/home/spark/.local/lib/python3.7/site-packages/botocore/client.py", line 566, in getattr self.class.name, item) AttributeError: 'Athena' object has no attribute 'list_table_metadata'

好的,正如旧版本的 boto3 所预期的那样。让我们尝试导入最新版本...

我执行以下步骤:

  1. 转到https://pypi.org/project/boto3/#files
  2. 下载 boto3-1.17.13-py2.py3-none-any.whl 文件
  3. 将其放在 S3 位置
  4. 返回 Glue 作业并在 安全配置、脚本库和作业参数(可选) 部分更新 Python库路径 与步骤 3 中的 S3 位置
  5. Re运行 上面的代码块

哪个returns

1.17.9

Traceback (most recent call last): File "/tmp/another_sample", line 20, in print(athena.list_table_metadata) File "/home/spark/.local/lib/python3.7/site-packages/botocore/client.py", line 566, in getattr self.class.name, item) AttributeError: 'Athena' object has no attribute 'list_table_metadata'

如果我 运行 在本地使用相同的脚本,即 运行ning 1.17.9 我可以找到方法:

1.17.9

<bound method ClientCreator._create_api_method.._api_call of <botocore.client.Athena object at 0x7efd8a4f4710>>

关于这里发生的事情以及如何访问我期望应该在升级版本中导入的方法有什么想法吗?

最终在 AWS documentation 中找到了解决方案。

安全配置、脚本库和作业参数(可选)下的 Glue 作业参数 中添加了以下 Key/Value 对 作业部分:

密钥:

--additional-python-modules

:

botocore>=1.20.12,boto3>=1.17.12