简体   繁体   English

将数据从S3复制到python中的RedShift(sqlalchemy)

[英]COPY data from S3 to RedShift in python (sqlalchemy)

I'm trying to push (with COPY) a big file from s3 to Redshift. 我正在尝试(使用COPY)将大文件从s3推送到Redshift。 Im using sqlalchemy in python to execute the sql command but it looks that the copy works only if I preliminary TRUNCATE the table. 我在python中使用sqlalchemy执行sql命令,但它看起来只有在我初步截断该表时该副本才起作用。

the connection works ok: 连接正常:

from sqlalchemy import create_engine 
engine = create_engine('postgresql://XXXX:XXXX@XXXX:XXXX/XXXX') 

with this command string (if I truncate the table before the COPY command) 使用此命令字符串(如果我在COPY命令之前截断了表)

toRedshift = "TRUNCATE TABLE public.my_table; COPY public.my_table from 's3://XXXX/part-p.csv' CREDENTIALS 'aws_access_key_id=AAAAAAA;aws_secret_access_key=BBBBBBB' gzip removequotes IGNOREHEADER 0 delimiter '|';"
engine.execute(toRedshift)

If I remove the "TRUNCATE TABLE public.my_table;" 如果删除“ TRUNCATE TABLE public.my_table;” bit 一点

toRedshift = "COPY public.my_table from 's3://XXXX/part-p.csv' CREDENTIALS 'aws_access_key_id=AAAAAAA;aws_secret_access_key=BBBBBBB' gzip removequotes IGNOREHEADER 0 delimiter '|';"
engine.execute(toRedshift)

But the command works perfectly in with any other SQL client (like DBeaver for example) 但是该命令可以与任何其他SQL客户端完美配合(例如,DBeaver)

Thank you Ilja. 谢谢你Ilja With this command it works: 使用此命令,它可以工作:

engine.execute(text(toRedshift).execution_options(autocommit=True))

I don't know why I was able to push the data with the TRUNCATE bit at the front of the string. 我不知道为什么我能够使用字符串开头的TRUNCATE位来推送数据。

Ivan 伊万

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM