How do I troubleshoot data loading errors while using the COPY command in Amazon Redshift?
"I tried to use the COPY command to load a flat file. However, I'm experiencing data loading issues or errors in Amazon Redshift. How do I troubleshoot this?"
"I tried to use the COPY command to load a flat file. However, I'm experiencing data loading issues or errors in Amazon Redshift. How do I troubleshoot this?Short descriptionUse the STL_LOAD_ERRORS table to identify any data loading errors that occur during a flat file load. The STL_LOAD_ERRORS table can help you track the progress of a data load, recording any failures or errors along the way. After you troubleshoot the identified issue, reload the data in the flat file while using the COPY command.Tip: If you're using the COPY command to load a flat file in Parquet format, you can also use the SVL_S3LOG table. The SVL_S3LOG table can be used to identify any data loading errors.ResolutionNote: The following steps use an example dataset of cities and venues.1.    Check the data in your sample flat file to confirm that the source data is valid.For example:7|BMO Field|Toronto|ON|016|TD Garden|Boston|MA|023|The Palace of Auburn Hills|Auburn Hills|MI|028|American Airlines Arena|Miami|FL|037|Staples Center|Los Angeles|CA|042|FedExForum|Memphis|TN|052|PNC Arena|Raleigh|NC  ,25   |059|Scotiabank Saddledome|Calgary|AB|066|SAP Center|San Jose|CA|073|Heinz Field|Pittsburgh|PA|65050In this example demo.txt file, five fields are used, separated by a pipe character. For more information, see Load LISTING from a pipe-delimited file (default delimiter).2.    Open the Amazon Redshift console.3.    Create a sample table using the following DDL:CREATE TABLE VENUE1(VENUEID SMALLINT,VENUENAME VARCHAR(100),VENUECITY VARCHAR(30),VENUESTATE CHAR(2),VENUESEATS INTEGER) DISTSTYLE EVEN;4.    Create a view to preview the relevant columns from the STL_LOAD_ERRORS table:create view loadview as(select distinct tbl, trim(name) as table_name, query, starttime,trim(filename) as input, line_number, colname, err_code,trim(err_reason) as reasonfrom stl_load_errors sl, stv_tbl_perm spwhere sl.tbl = sp.id);This view can help you identify the cause of the data loading error.5.    Use the COPY command to load the data:copy Demofrom 's3://your_S3_bucket/venue/'iam_role 'arn:aws:iam::123456789012:role/redshiftcopyfroms3'delimiter '|' ;Note: Replace your_S3_bucket with the name of your S3 bucket. Then, replace arn:aws:iam::123456789012:role/redshiftcopyfroms3 with the Amazon Resource Name (ARN) for your AWS Identity and Access Management (IAM) role. This IAM role must be able to access data from your S3 bucket. For more information, see Parameters.6.    Query the load view to display and review the error load details of the table:testdb=# select * from loadview where table_name='venue1';tbl | 265190table_name | venue1query | 5790starttime | 2017-07-03 11:54:22.864584input | s3://your_S3_bucket/venue/venue_pipe0000_part_00line_number | 7colname | venuestateerr_code | 1204reason | Char length exceeds DDL lengthIn this example, the exception is caused by the length value, which must be added to the venuestate column. The (NC ,25 |) value is longer than the length defined in the VENUESTATE CHAR(2) DDL.You can resolve this exception in two different ways:If the data is expected to exceed the defined length of the column, then review and update the table definition to modify the column length.-or-If the data isn't properly formatted or transformed, then modify the data in file to use the correct value.The output from this query includes the following important information:The file causing the error.The column causing the error.The line number in the input file.The reason for the exception.7.    Modify the data in your load file to use the correct values (the length must align with the defined column length):7|BMO Field|Toronto|ON|016|TD Garden|Boston|MA|023|The Palace of Auburn Hills|Auburn Hills|MI|028|American Airlines Arena|Miami|FL|037|Staples Center|Los Angeles|CA|042|FedExForum|Memphis|TN|052|PNC Arena|Raleigh|NC|059|Scotiabank Saddledome|Calgary|AB|066|SAP Center|San Jose|CA|073|Heinz Field|Pittsburgh|PA|650508.    Reload the data load:testdb=# copy Demofrom 's3://your_S3_bucket/sales/'iam_role 'arn:aws:iam::123456789012:role/redshiftcopyfroms3' delimiter '|' ;INFO:  Load into table 'venue1' completed, 808 record(s) loaded successfully.Note: The STL_LOAD_ERRORS table can hold only a limited amount of logs (typically for around 4 to 5 days). Also, standard users can view only their own data when querying the STL_LOAD_ERRORS table. To view all the table data, you must be a superuser.Related informationAmazon Redshift best practices for designing tablesAmazon Redshift best practices for loading dataSystem tables for troubleshooting data loadsWorking with recommendations from Amazon Redshift AdvisorFollow"