I am trying to use the COPY command to import data into Redshift. Unfortunately the data is not sanitized very well and there are CRLF characters in some of the data. This is causing an error because it thinks it is a new record.
I am already using the DELIMITER parameter, but that is setting the delimiter for the fields in each record. Is there a similar way to specify what character(s) are delimiting each record?
No. Redshift expects \n (0x0A) as the End of Record (EOF) and doesn't handle CRLF (0x0D 0x0A). I believe it just sees the CR as another piece of input data but this info cannot be inserted into anything other than a varchar column. If you lines just have CR (0x0D) Redshift won't see an EOF at all and combine rows.
You will need to cleanse your data to remove the CR characters. Each record needs to end with a newline NL (0x0A). (Yes, LF and NL are the same ascii code and just have different names in different applications.) Hopefully you can just remove the CR's but I've seen data with just CR as the EOF and you will need to change these to NL in this case not just remove them.
If your last column of data is a varchar then you can (I believe) just strip the CR character from these strings after it is loaded into Redshift. Otherwise you data needs to be fix before it enters Redshift.
For brevity sake I am just going to create a practical example:
Let's say I am ingesting a raw CSV into S3 and one of the columns is SSNO. Within the CSV format, SSNO is not wrapped in any quotes. As we all know, SSNOs can have leading 0's: 012345678. When I run my crawler, it creates a schema where SSNO is of Type bigint, and because of this it strips the leading zeroes: 12345678.
How do I either:
A) make it not strip zeroes
B) force it to read the columns as a string
I'm pulling data from Amazon S3 into a table in Amazon Redshift. The table contains various columns, where some column data might contain special characters.
The copy command has an option called Delimiter where we can specify the delimiter while pulling the data into the table.
The issue is 2 fold -
When I export (unload command) to S3 using a delimiter - say , - it works fine, but when I try to import into Redshift from S3, the issue creeps in because certain columns contain the ',' operator which the copy command misinterprets as delimiter and throws error.
I tried various delimiters, but the data in my table seems to contain some or other kind of special character which causes the above issue.
I even tried unloading using multiple delimiter - like #% or ~, but when loading from s3 using copy command - the dual delimiter is not supported.
Any solutions?
I think the delimiter can be escaped using \ but for some reason that isn't working either, or maybe I'm not using the right syntax for escaping in copy command.
The following example shows the contents of a text file with the field values separated by commas.
12,Shows,Musicals,Musical theatre
13,Shows,Plays,All "non-musical" theatre
14,Shows,Opera,All opera, light, and "rock" opera
15,Concerts,Classical,All symphony, concerto, and choir concerts
If you load the file using the DELIMITER parameter to specify comma-delimited input, the COPY command will fail because some input fields contain commas. You can avoid that problem by using the CSV parameter and enclosing the fields that contain commas in quote characters. If the quote character appears within a quoted string, you need to escape it by doubling the quote character. The default quote character is a double quotation mark, so you will need to escape each double quotation mark with an additional double quotation mark. Your new input file will look something like this.
12,Shows,Musicals,Musical theatre
13,Shows,Plays,"All ""non-musical"" theatre"
14,Shows,Opera,"All opera, light, and ""rock"" opera"
15,Concerts,Classical,"All symphony, concerto, and choir concerts"
Source :- Load Quote from a CSV File
What I use -
COPY tablename FROM 'S3-Path' CREDENTIALS '' MANIFEST CSV QUOTE '\"' DELIMITER ',' TRUNCATECOLUMNS ACCEPTINVCHARS MAXERROR 2
If I’ve made a bad assumption please comment and I’ll refocus my answer.
If the delimiter is appearing within fields, then use the ADDQUOTES parameter with the UNLOAD command:
Places quotation marks around each unloaded data field, so that Amazon Redshift can unload data values that contain the delimiter itself.
Then:
If you use ADDQUOTES, you must specify REMOVEQUOTES in the COPY if you reload the data.
A popular delimiter is the pipe character (|) that is rare in text files.
Adding CSV QUOTE as '\"' before the DELIMITER worked for me.
I have some text data stored in parquet format in HDFS in the Hive metastore. Each observation may or may not include \n as part of the text itself.
I need to export this data to a text (tab or comma delimited) file to analyze further in Python.
If I were to run a query against the data and save to text file I would get:
id,txt
1,I like this site \n tomorrow I'll write more
2,How cool \n is this website
At that point my rows get screwed due to the extra \n.
I tried to export the data but the regexp_replace function doesn't seem to produce the stripping I was expecting:
select id, regexp_replace(txt,'\\n',' ') as txt
from table
limit 1000
Any ideas on how to deal with this?
I'm loading a CSV file from S3 into Redshift. This CSV file is analytics data which contains the PageUrl (which may contain user search info inside a query string for example).
It chokes on rows where there is a single, double-quote character, for example if there is a page for a 14" toy then the PageUrl would contain:
http://www.mywebsite.com/a-14"-toy/1234.html
Redshift understandably can't handle this as it is expecting a closing double quote character.
The way I see it my options are:
Pre-process the input and remove these characters
Configure the COPY command in Redshift to ignore these characters but still load the row
Set MAXERRORS to a high value and sweep up the errors using a separate process
Option 2 would be ideal, but I can't find it!
Any other suggestions if I'm just not looking hard enough?
Thanks
Duncan
It's 2017 and I run into the same problem, happy to report there is now a way to get redshift to load csv files with the odd " in the data.
The trick is to use the ESCAPE keyword, and also to NOT use the CSV keyword.
I don't know why, but having the CSV and ESCAPE keywords together in a copy command resulted in failure with the error message "CSV is not compatible with ESCAPE;"
However with no change to the loaded data I was able to successfully load once I removed the CSV keyword from the COPY command.
You can also refer to this documentation for help:
http://docs.aws.amazon.com/redshift/latest/dg/copy-parameters-data-conversion.html#copy-escape
Unfortunately, there is no way to fix this. You will need to pre-process the file before loading it into Amazon Redshift.
The closest options you have are CSV [ QUOTE [AS] 'quote_character' ] to wrap fields in an alternative quote character, and ESCAPE if the quote character is preceded by a slash. Alas, both require the file to be in a particular format before loading.
See:
Redshift COPY Data Conversion Parameters
Redshift COPY Data Format Parameters
I have done this using ---> DELIMITER ',' IGNOREHEADER 1; at the replacement for 'CSV' at the end of COPY command. Its working really fine.