【发布时间】: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 标志。"