Solved

MySQL ETL scipts

Posted on 2016-09-20
11
24 Views
Last Modified: 2016-10-25
can anyone show me some sample ETL scripts to load data from staging tables in MySQL  with error handling and logging etc
0
Comment
Question by:Tara
  • 6
  • 3
  • 2
11 Comments
 
LVL 26

Accepted Solution

by:
Zberteoc earned 250 total points (awarded by participants)
ID: 41808688
The asiest way to import files to mysql is to use the LOAD command from within MySQL server:
LOAD DATA INFILE 'data.csv' INTO TABLE tbl_name
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;

Open in new window

Ignore 1 line only if the file has headers. More details on the command here:

http://dev.mysql.com/doc/refman/5.7/en/load-data.html

If you want to log the errors then you can use try catch:

https://www.experts-exchange.com/questions/28101010/Is-there-try-catch-block-capability-in-MySQL-and-If-so-how.html
0
 
LVL 50

Assisted Solution

by:Steve Bink
Steve Bink earned 250 total points (awarded by participants)
ID: 41808923
Be aware that handlers are only available in stored programs, and that LOAD DATA can not be used in stored programs.

LOAD DATA has no error checking or handling aside from MySQL's natural data validation.  For example, MySQL will generate an error or warning if a field value fails type checking, such as when inserting a string into an integer field.  

LOAD DATA does, however, cause INSERT triggers to fire, which is where you would need your validation/error-handling code.  If you create an INSERT trigger on your destination table, you can leverage the full capability of handlers, as well as other complicated logic to verify the validity of the imported row.  Keep in mind that allowing an error condition to propagate in one of the triggers will behave in the same way as if the error was generated by LOAD DATA.

Finally, the LOCAL keyword impacts how errors during LOAD DATA operations are handled.  When using LOCAL, errors become warnings, and the remainder of the import proceeds.  When not using LOCAL, an error will halt the operation.  LOAD DATA operations are considered atomic, which means a single error will rollback any valid rows found before the error.
0
 

Author Comment

by:Tara
ID: 41809064
Thanks for the responses I am thinking about stored procedures using insert/replace statements from staging tables , similar to the sample code Microsoft has provided with Adventure works
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 26

Expert Comment

by:Zberteoc
ID: 41809292
But how do you get data in the staging tables? You still need to import from the files and that you do with LOAD statements. Of course you could use some ETL third party tools that are even free. Try Talend Data Integration for instance:

https://www.talend.com/download/talend-open-studio#t4
0
 

Author Comment

by:Tara
ID: 41809341
I am using php scripts to upload into staging tables .
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41809352
So you are actually asking scripts in PHP? If yes you should add the PHP tag to your question.

If you load data using a programming language then yes, you load data into some staging tables and from there you can do whatever you need in MySQL code. I prefer and use Python, though. It is much simpler to use but PHP, or any other language for that matter, is fine too.
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 41809662
If you are using PHP, I recommend you handle your validation there.  MySQL does not have the flexibility that languages like PHP or Python have when it comes to reading files, implementing complicated logic, or reporting issues during operations.
0
 

Author Comment

by:Tara
ID: 41810926
Thank you for your comments! I will close this question
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41810931
You don't have to close it unless you don't award points to anyone, otherwise you just award them and that's it.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41832212
Tara, will you please finish this question? Thanks. No need to close it jut give the award points.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41858226
I split the points:

Zberteoc: 250
Steve Bink: 250
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikipedia.org/wiki/PHP  Very powerful.  But a…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question