当我使用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()
尝试这样的事情:
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的安全组。
尝试将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代码将把三个引号中的所有字符串作为复制命令