AWS copy命令在使用python psycopg2的EC2实例中不起作用

问题描述 投票:0回答:2

当我使用SQL工作台时,AWS copy命令运行良好。目的是将S3数据复制到Redshift表。这是我正在使用的复制命令:

copy oao_features_usbank_v2 from 's3://my_bucket/test_data.csv' iam_role 'arn:aws:iam::accountid:role/my_role' CSV IGNOREHEADER 1 MAXERROR 10 DELIMITER as '|';

然后当使用python psycopg2时。我尝试了多种连接方法,但都没有。如:

conn_string = 'postgresql://username:pwd@host:port/db'
copy_query = 'copy oao_features_usbank_v2 from 's3://my_bucket/test_data.csv' iam_role 'arn:aws:iam::accountid:role/my_role' CSV IGNOREHEADER 1 MAXERROR 10 DELIMITER as '|';'

with psycopg2.connect(conn_string) as conn:
    with conn.cursor() as curs:
        curs.execute(copy_query)
    conn.commit()
python-3.x amazon-s3 amazon-ec2 amazon-redshift psycopg2
2个回答
1
投票

尝试这样的事情:

import psycopg2
​
db = "your_database"
host = "redshift_end_point"
port = "5439"
user = "your_user_here"
pwd = "your_password_here"
connstr = "dbname=%s host=%s port=%s user=%s password=%s" % (db, host, port, user, pwd)
​
con = psycopg2.connect(connstr)

可以在AWS控制台中找到Redshift终点。它看起来像:

clustername.sofhsdl34534.us-east-1.redshift.amazonaws.com

您的EC2实例还应附加一个允许连接到Redshift的安全组。


0
投票

尝试将copy_query插入多个行字符串,如下所示:copy_query =“”“copy oao_features_usbank_v2 from's3://my_bucket/test_data.csv'iam_role'arnn:aws:iam :: accountid:role / my_role'CSV IGNOREHEADER 1 MAXERROR 10 DELIMITER为'|';' “””

因此,python代码将把三个引号中的所有字符串作为复制命令

© www.soinside.com 2019 - 2024. All rights reserved.