How to execute AWS S3 to Redshift Copy command from SQL script? - amazon-web-services

I am trying to copy some files from S3 to Redshift using copy command. I used following command through SQL workbench and it worked fine, it copied the data to Redshift Table.
copy <Redshift table name>
from 's3://my-bucket/path/to/directory/part'
iam_role 'arn:aws:iam::<IAM ROLE>'
delimiter '|' dateformat 'auto' IGNOREHEADER AS 1;
but when I copied the same command into .sql file and tried to execute this SQL file using AWS data pipeline, pipeline just fails without giving any explicit error.
Due to some issues with internally developed pipeline definition generation tool, I am not able to use CopyToRedshift type activity.
I would like to know how do I execute this copy command from an file?

Try this out !!
COPY Table_Name
FROM S3File_PATH
credentials "*AWS Credentails"
ignoreheader as 1
ACCEPTINVCHARS
delimiter '|'
This copy command should work from a sql file .If not try to check for any errors in stl_load_error_details

Related

remove backslash from a .csv file to load data to redshift from s3

I am getting an issue when I am loading my file , I have backslash in my csv file
how and what delimited can I use while using my copy command so that I don't get
error loading data from s3 to redshift.
Though I used the QUOTE command but gave me a syntax error so seems like new format
doesn't like the QUOTE key word.
Please if any one can provide a new and correct
command or dow I need to clean or preprocess my data before uploading to s3.
If the
Data size is too big it might not be a very feasible solution
If I have to process it , Do I use pyspark or python(PANDAS) to do it?
Below is the copy command I am using to copy data from s3 to redshift
I tried passing a quote command in the copy command but seems like it doesn't take
that anymore also there is no example in amazon docs on how to do or acheive it
If someone can suggest a command which can replace especial characters while loading
the data
COPY redshifttable from 'mys3filelocation'
CREDENTIALS 'aws_access_key_id=myaccess_key;aws_secret_access_key=mysecretID'
region 'us-west-2'
CSV
DATASET:
US063737,2019-11-07T10:23:25.000Z,richardkiganga,536737838,Terminated EOs,"",f,Uganda,Richard,Kiganga,Business owner,Round Planet DTV Uganda,richardkiganga,0.0,4,7.0,2021-06-1918:36:05,"","",panama-
Disc.s3.amazon.com/photos/…,\"\",Mbale,Wanabwa p/s,Eastern,"","",UACE Certificate,"",drive.google.com/file/d/148dhf89shh499hd9303-JHBn38bh/… phone,Mbale,energy_officer's_id_type,letty
mainzi,hakuna Cell,Agent,8,"","",4,"","","",+647739975493,Feature phone,"",0,Boda goda,"",1985-10-12,Male,"",johnatlhnaleviski,"",Wife

COPY command: AWS Aurora Postgresql, s3 extension

I'm trying to execute a COPY command to import csv file from S3 (result of UNLOAD command from Redshift) into an Amazon Aurora database Using the aws_s3.table_import_from_s3 Function to Import Amazon S3 Data, but I don't know to indicate the quotes character in the command.
SELECT aws_s3.table_import_from_s3(
'hr.person',
'',
'(FORMAT CSV,HEADER true,QUOTES ''"'')',
aws_commons.create_s3_uri('redshift-unload-tmp','resul_file.csv','us-east-2')
);
Thanks
I'll explain my use case more detailed:
Source
I used the UNLOAD command to "export" data from a table in Redshift, this is the command:
UNLOAD('SELECT * FROM schema.table')
TO 's3://bucket-name/prefix_'
HEADER
CSV
NULL AS '\000'
IAM_ROLE 'arn:aws:iam::accountNumber:role/aRoleWithRedshiftAndS3Permissions';
Target
I need to put the Redshift data (now file in s3) into RDS database (Aurora-postgresql), before import file, I did a rename of the files in s3 and add the extension .csv; I used pgAdmin 4 as a Postgresql client, and open a query editor to execute the following commands:
Add new s3 extension to the database:
CREATE EXTENSION aws_s3 CASCADE;
NOTICE: installing required extension "aws_commons"
Execute function to import file from s3
select aws_s3.table_import_from_s3(
'schame.table_name',
'',
'(FORMAT CSV, HEADER true)',
aws_commons.create_s3_uri('sample_s3_bucket_name','source_file_name.csv','aws-region')
);
Note: If you use CSV format by default quote character is ", then you don't need to indicate the quote as an option parameter, you need to do if you are using a different quote character.

Issue with copying data from s3 to Redshift

I am trying to sync a table from MySQL RDS to redshift trough data pipeline.
There was no issue in copying data frm RDS to S3. But while copying S3 to redhsift the follwoing isue is seen.
amazonaws.datapipeline.taskrunner.TaskExecutionException: java.lang.RuntimeException: Unable to load data: Invalid timestamp format or value [YYYY-MM-DD HH24:MI:SS]
While observing data it is seen that while copying data to S3 an extra "0" is being appended at the end of time stamp i.e 2015-04-28 10:25:58 from MySQL table is being copied as 2015-04-28 10:25:58.0 into CSV file which is giving issue.
I also tried copying with copy command using the following
copy XXX
from 's3://XXX/rds//2018-02-27-14-38-04/1d6d39b9-4aac-408d-8275-3131490d617d.csv'
iam_role 'arn:aws:iam::XXX:role/XXX' delimiter ',' timeformat 'auto';
but still the same issue.
Can anyone help me sort out this issue.
Thanks in advance

Unable to create AWS data pipeline for copying s3 to redshift

I am new to AWS, im trying to create a data pipeline to transfer s3 files into redshift.
I have already performed the same task manually. Now with pipelining, I am unable to proceed further here
Problem with Copy Options :
Sample data on s3 files is like :
15,NUL next, ,MFGR#47,MFGR#3438,indigo,"LARGE ANODIZED BRASS",45,LG CASE
22,floral beige,MFGR#4,MFGR#44,MFGR#4421,medium,"PROMO, POLISHED BRASS",19,LG DRUM
23,bisque slate,MFGR#4,MFGR#41,MFGR#4137,firebrick,"MEDIUM ""BURNISHED"" TIN",42,JUMBO JAR
24,dim white,MFGR#4,MFGR#45,MFGR#459,saddle,"MEDIUM , ""PLATED"" STEEL",20,MED CASE
So at manual work I gave this copy command:
copy table from 's3://<your-bucket-name>/load/key_prefix'
credentials 'aws_access_key_id=<Your-Access-Key-ID>;aws_secret_access_key=<Your-Secret-Access-Key>'
csv
null as '\000';
and it worked perfectly
I tried with basic options as :
1. csv
2. null as '\000'
But none works.

Move RedShift file to S3 as CSV

I'm trying to move a file from RedShift to S3. Is there an option to move this file as a .csv?
Currently I am writing a shell script to get the Redshift data, save it as a .csv, and then upload to S3. I'm assuming since this is all on AWS services, they would have an argument or something that let's me do this.
Use the UNLOAD command. It will create at least one file per slice, you will have to merge the files by yourself.
unload ('__SQL__')
to 's3://__BUCKET__/__PATH__'
credentials 'aws_access_key_id=__S3_KEY__;aws_secret_access_key=__S3_SECRET__'
delimiter as ','
addquotes
escape