简体   繁体   English

使用Psycopg2将数据从S3复制到AWS Redshift时出错

[英]Error Copying Data From S3 to AWS Redshift With Psycopg2

While executing a COPY command utilizing psycopg2, I receive the error: 使用psycopg2执行COPY命令时,出现以下错误:

psycopg2.ProgrammingError: unterminated quoted string at or near "'"
LINE 12:    NULL as '

Here is my origianl query: 这是我的原始查询:

"""copy dcm_floodlight_raw_abg_stg (COLUMN_NAMES)
from 'S3_BUCKET_PATH'
CREDENTIALS 'aws_access_key_id=KEY;aws_secret_access_key=SECRET'
gzip
DELIMITER '\t'
DATEFORMAT as 'yyyy-mm-dd'
BLANKSASNULL 
TRUNCATECOLUMNS 
FILLRECORD
MAXERROR 100
ACCEPTINVCHARS as '?'
NULL as '\0';"""

Not too sure why I am receiving this error as it doesn't throw this error for any other single quotations. 不太确定为什么收到此错误,因为对于其他任何单引号都不会引发此错误。

When having issues with a query you can use the psycopg2 mogrify function to see if there is an issue with your string. 当查询有问题时,可以使用psycopg2 mogrify函数查看字符串是否存在问题。 It will attempt to interpolate any parameters as well so can be a good check. 它将尝试插入任何参数,因此可以很好地进行检查。 Since I didn't have a cursor object to call it on I did this instead: 由于我没有游标对象可以调用它,因此我这样做:

print psycopg2.extensions.adapt(YOUR_QUERY).getquoted()

In your case this gives: 在您的情况下,这给出了:

>>> import psycopg2
>>> print psycopg2.extensions.adapt(YOUR_QUERY).getquoted()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: A string literal cannot contain NUL (0x00) characters.

>>> print psycopg2.extensions.adapt('\\0').getquoted()
'\\0'
>>>

So you can see it doesn't like the '\\0' . 因此,您可以看到它不喜欢'\\0' If you escape the backslash as John Rotenstein suggests psycopg2 will accept the query but it may not give you what you want for your null value. 如果您按照John Rotenstein的建议转义了反斜杠,则psycopg2将接受查询,但可能不会为您提供空值所需的内容。 The documentation suggests it should work if you can get the interpolation sorted out. 文档建议,如果您可以对插补进行整理,它应该可以工作。

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

相关问题 使用python和psycopg2将数据从S3复制到AWS Redshift - Copying data from S3 to AWS redshift using python and psycopg2 psycopg2 / python将数据从Postgresql复制到Amazon RedShift(postgresql) - psycopg2/python copy data from postgresql to Amazon RedShift(postgresql) 将数据从本地复制到S3到Redshift表时出现问题 - Issue while copying data from local to S3 to Redshift table 将数据从 s3 复制到 redshift 时出现“csv 的无效报价格式”错误 - Getting "invalid quote formatting for csv" error while copying data from s3 to redshift AWS Glue 数据从 S3 转移到 Redshift - AWS Glue Data moving from S3 to Redshift 使用 psycopg2 的 aws localstack redshift 连接问题 - aws localstack redshift connection issue using psycopg2 AWS Lambda Python / Boto3 / psycopg2 Redshift临时凭证 - AWS Lambda Python/Boto3/psycopg2 Redshift temporary credentials Python 使用 psycopg2 将 DateFrame 写入 AWS redshift - Python write DateFrame to AWS redshift using psycopg2 使用python psycopg2 execute_values从批处理插入redshift中获取身份ID - Get the identity id's from a batch insert into redshift using python psycopg2 execute_values 使用Amazon Redshift从Python的psycopg2中的光标获取大于MAX_INT的行数 - Getting number of rows larger than MAX_INT from cursor in Python's psycopg2 with Amazon Redshift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM