2024-06-01 14:08:56 发布
网友
在Redshift中,我运行以下命令将表中的数据卸载到S3中的文件中:
unload('select * from table') to 's3://bucket/unload/file_' iam_role 'arn:aws:iam:<aws-account-id>:role/<role_name>'
我想在Python中做同样的事情-有什么建议吗?我看到了使用accesskey和secret的例子,但这不是我的选择-需要在非公共bucket上使用基于角色的凭据。在
您需要两套凭证。通过IAM角色访问S3存储桶的IAM凭据和执行SQL命令的Redshift ODBC凭据。在
创建一个连接到Redshift的Python程序,其方式与其他数据库(如sqlserver)类似,并执行查询。此程序需要红移登录凭据,而不是IAM凭据(Redshift用户名、密码)。在
S3的IAM凭证被指定为Redshift的角色,以便Redshift可以将结果存储在S3上。这是问题中红移查询的iam_role 'arn:aws:iam:<aws-account-id>:role/<role_name>'部分。在
iam_role 'arn:aws:iam:<aws-account-id>:role/<role_name>'
您不需要boto3(或boto)来访问Redshift,除非您计划实际与Redshift API接口(它不访问Redshift中存储的数据库)。在
下面是一个访问Redshift的Python程序示例。此代码的链接是here。应收账款Varun Verma
互联网上还有其他的例子可以帮助你开始。在
############ REQUIREMENTS #################### # sudo apt-get install python-pip # sudo apt-get install libpq-dev # sudo pip install psycopg2 # sudo pip install sqlalchemy # sudo pip install sqlalchemy-redshift ############################################## import sqlalchemy as sa from sqlalchemy.orm import sessionmaker #>>>>>>>> MAKE CHANGES HERE <<<<<<<<<<<<< DATABASE = "dbname" USER = "username" PASSWORD = "password" HOST = "host" PORT = "" SCHEMA = "public" #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 = "unload('select * from table') to 's3://bucket/unload/file_' iam_role 'arn:aws:iam:<aws-account-id>:role/<role_name>';" 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()
您需要两套凭证。通过IAM角色访问S3存储桶的IAM凭据和执行SQL命令的Redshift ODBC凭据。在
创建一个连接到Redshift的Python程序,其方式与其他数据库(如sqlserver)类似,并执行查询。此程序需要红移登录凭据,而不是IAM凭据(Redshift用户名、密码)。在
S3的IAM凭证被指定为Redshift的角色,以便Redshift可以将结果存储在S3上。这是问题中红移查询的
iam_role 'arn:aws:iam:<aws-account-id>:role/<role_name>'
部分。在您不需要boto3(或boto)来访问Redshift,除非您计划实际与Redshift API接口(它不访问Redshift中存储的数据库)。在
下面是一个访问Redshift的Python程序示例。此代码的链接是here。应收账款Varun Verma
互联网上还有其他的例子可以帮助你开始。在
相关问题 更多 >
编程相关推荐