复制由双引号引起来的Redshift管道定界

时间:2020-01-26 16:35:07

标签: amazon-redshift

我正在尝试将文件从S3加载到Redshift。该文件由Pipe分隔,但是有些值包含Pipe和其他特殊字符,但是如果值包含Pipe,则将其用双引号引起来。

示例: Field1|Field2 "abc|dh"|123 efh@ih|233

我尝试使用以下命令,但出现错误消息,指出无效数字。这是由于复制命令正在将值中的管道视为定界符。

copy table from 's3' iam_role 'arn' region 'us-east-1' MAXERROR AS 10 NULL AS '(null)' 'ESCAPE "' IGNOREHEADER AS 1 DELIMITER '|' timeformat 'auto' GZIP;

1 个答案:

答案 0 :(得分:1)

您正在寻找REMOVEQUOTES参数。 https://docs.aws.amazon.com/redshift/latest/dg/copy-parameters-data-conversion.html#copy-removequotes

ESCAPE要求您的数据准备有嵌入的转义符。例如,如果您的转义字符为\,则需要准备数据,以使内容为"abc\|dh"

示例:

DROP TABLE IF EXISTS public.quote_test; 
CREATE TABLE IF NOT EXISTS public.quote_test (col_a VARCHAR(10), col_b VARCHAR(10)); 
SELECT * FROM quote_test;
echo '"a|b"|"c|d"' > ~/simple_quotes.txt
aws s3 cp ~/simple_quotes.txt s3://my-bucket/simple_quotes.txt
--Will fail
COPY quote_test FROM 's3://my-bucket/simple_quotes.txt' 
CREDENTIALS 'aws_iam_role=arn:aws:iam::012345678901:role/redshift-cluster'
DELIMITER '|' REGION 'us-west-2';

--Succeeds with REMOVEQUOTES
COPY quote_test FROM 's3://benchmark-files/simple_quotes.txt' 
CREDENTIALS 'aws_iam_role=arn:aws:iam::012345678901:role/redshift-cluster'
REMOVEQUOTES DELIMITER '|' REGION 'us-west-2';

SELECT * FROM quote_test;
--  col_a | col_b
-- -------+-------
--  a|b   | c|d