【问题标题】:AWS Glue Python Shell package importAWS Glue Python Shell 包导入
【发布时间】:2019-12-21 14:10:39
【问题描述】:

我们创建了一个 python shell 作业,它连接 Redshift 并获取数据,下面的程序在我的本地系统中运行良好。 以下是步骤和程序。

程序:-

import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
#>>>>>>>> MAKE CHANGES HERE <<<<<<<<<<<<< 
DATABASE = "#####"
USER = "#####"
PASSWORD = "#####"
HOST = "#####.redshift.amazonaws.com"
PORT = "5439"
SCHEMA = "test"      #default is "public" 

####### connection and session creation ############## 
connection_string = "redshift+psycopg2://%s:%s@%s:%s/%s" % (USER,PASSWORD,HOST,str(PORT),DATABASE)
engine = sa.create_engine(connection_string)
session = sessionmaker()
session.configure(bind=engine)
s = session()
SetPath = "SET search_path TO %s" % SCHEMA
s.execute(SetPath)
###### All Set Session created using provided schema  #######
################ write queries from here ###################### 
query = "SELECT * FROM test1 limit 2;"
rr = s.execute(query)
all_results =  rr.fetchall()
def pretty(all_results):
    for row in all_results :
        print("row start >>>>>>>>>>>>>>>>>>>>")
        for r in row :
            print(" ----" , r)
        print("row end >>>>>>>>>>>>>>>>>>>>>>")
pretty(all_results)
########## close session in the end ###############
s.close()

步骤:-

  • sudo pip install psycopg2
  • sudo pip install sqlalchemy
  • sudo pip install sqlalchemy-redshift

我已上传文件 psycopg2-2.8.4-cp27-cp27m-win32.whl、Flask_SQLAlchemy-2.4.1-py2.py3-none-any.whl 和 sqlalchemy_redshift-0.7.5-py2.py3-none-在 S3 (s3://####/lib/) 中的 any.whl,并在 AWS Glue Job 中映射 Python 库路径 中的文件夹。

当我运行程序时发生以下错误。

Traceback (most recent call last):
  File "/tmp/runscript.py", line 113, in <module>
    download_and_install(args.extra_py_files)
  File "/tmp/runscript.py", line 56, in download_and_install
    download_from_s3(s3_file_path, local_file_path)
  File "/tmp/runscript.py", line 81, in download_from_s3
    s3.download_file(bucket_name, s3_key, new_file_path)
  File "/usr/local/lib/python2.7/site-packages/boto3/s3/inject.py", line 172, in download_file
    extra_args=ExtraArgs, callback=Callback)
  File "/usr/local/lib/python2.7/site-packages/boto3/s3/transfer.py", line 307, in download_file
    future.result()
  File "/usr/local/lib/python2.7/site-packages/s3transfer/futures.py", line 106, in result
    return self._coordinator.result()
  File "/usr/local/lib/python2.7/site-packages/s3transfer/futures.py", line 265, in result
    raise self._exception
botocore.exceptions.ClientError: An error occurred (404) when calling the HeadObject operation: Not Found

PS:- Glue 作业角色拥有对 S3 的完全访问权限。

请建议如何将这些库与程序映射。

【问题讨论】:

  • 您可以尝试在 --extra-py-files 中传递这些文件,而不是作为库路径传递这些文件。同时传递以逗号分隔的每个文件的绝对路径。
  • 嗨,我已经这样做了(逗号(“,”),由 lib 文件分隔。现在遇到另一个问题。“警告:目录 '/.cache/pip/http' 或其父目录不属于当前用户并且缓存已被禁用。请检查该目录的权限和所有者。如果使用 sudo 执行 pip,您可能需要 sudo 的 -H 标志。"

标签: python aws-glue


【解决方案1】:

您可以在“--extra-py-files”标志下指定自己的 Python 库打包为 .egg 或 .whl 文件,如下例所示。

命令行示例:

aws glue create-job --name python-redshift-test-cli --role role --command '{"Name" :  "pythonshell", "ScriptLocation" : "s3://MyBucket/python/library/redshift_test.py"}' 
     --connections Connections=connection-name --default-arguments '{"--extra-py-files" : ["s3://MyBucket/python/library/redshift_module-0.1-py2.7.egg", "s3://MyBucket/python/library/redshift_module-0.1-py2.7-none-any.whl"]}'

参考:Create a glue job with extra python library

【讨论】:

  • 嗨,我已经这样做了,现在得到这个错误错误:- 警告:重试(重试(total=0,connect=None,read=None,redirect=None,status=None))之后连接被 'ConnectTimeoutError(, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/sqlalchemy/ 错误:找不到满足 SQLAlchemy>=0.8.0 要求的版本(来自 Flask-SQLAlchemy==2.4.1)(来自版本:无)错误:找不到 SQLAlchemy>=0.8.0 的匹配分布(来自 Flask-SQLAlchemy==2.4 .1)
  • 回溯(最近一次调用最后):文件“/tmp/runscript.py”,第 113 行,在 download_and_install(args.extra_py_files) 文件“/tmp/runscript.py”,行63、在download_and_install subprocess.check_call([sys.executable, "-m", "pip", "install", "--target= {} ".format(install_path), local_file_path]) 文件"/usr/local/ lib/python2.7/subprocess.py",第 190 行,在 check_call raise CalledProcessError(retcode, cmd) subprocess 中。 CalledProcessError : 命令 '['/usr/local/bin/python', '-m', 'pip', 'install', '--target=/glue/lib/installation',
  • @DeepeshUniyal 您能否添加您的 aws cli 命令的代码 sn-p。从上面的代码看起来它正在尝试从 Flask_SQLAlchemy whl 加载 sqlalchemy 并且找不到它。
  • @DeepeshUniyal :我还在您的本地环境中看到您正在使用“pip install sqlalchemy”,在这种情况下您正在传递flask_sqlalchemy。有什么具体原因吗?我在您的代码中没有看到任何烧瓶依赖项。
  • 嗨,按照给定的链接,现在依赖问题已经解决,但现在无法连接 redshift,似乎是安全问题,redshift 已经打开,我可以从任何地方手动访问它,但不能从胶水作业。日志:- 文件“/tmp/glue-python-scripts-gm07eo/redshift_test.py”,第 3 行,在 文件“/glue/lib/installation/redshift_module-0.1-py2.7.egg/redshift_module/pygresql_redshift_common .py”,第 8 行,在:无法连接到服务器:连接超时主机“testap-south-1.redshift.amazonaws.com”(14.236.218.0)并接受端口 5439 上的 TCP/IP 连接?
【解决方案2】:

有一种使用 whl 文件导入 python 依赖项的简单方法,可以在特定模块的 Python 站点上找到该文件。

您还可以使用逗号从 S3 添加多个车轮文件。

例如 "s3://xxxxxxxxx/common/glue/glue_whl/fastparquet-0.4.1-cp37-cp37m-macosx_10_9_x86_64.whl,s3://xxxxxx/common/glue/glue_whl/packaging-20.4-py2.py3-none-any .whl,s3://xxxxxx/common/glue/glue_whl/s3fs-0.5.0-py3-none-any.whl"

enter image description here

【讨论】:

    猜你喜欢
    • 2020-05-22
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2020-07-07
    相关资源
    最近更新 更多