[英]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.