Building a Data Warehouse, Part 4: Extraction, Transformation, and Load
Join the DZone community and get the full member experience.Join For Free
part i: when to build your data warehouse
part ii: building a new schema
part iii: location of your data warehouse
in part i we looked at the advantages of building a data warehouse independent of cubes/a bi system and in part ii we looked at how to architect a data warehouse’s table schema. in part iii, we looked at where to put the data warehouse tables. today we are going to look at how to populate those tables and keep them in sync with your oltp system.
no matter where your data warehouse is located, the biggest challenge with a data warehouse, especially one where you are going to do real time reporting off of, is that the data is published from the transactional system (oltp). by definition, the data warehouse is out of date compared to the oltp system.
usually when you ask the boss, “how much latency can you accept between the oltp and data warehouse systems” the boss will reply: none. while that is impossible, the more time to develop and the more money you have will to develop said system, the closer to real time you can get. always bring up the american express example used in part i for proof that your system can accept at least some latency.
if you remember the code examples from part ii, most of the time you have to query oltp data, aggregate it, and then load it into your new schema. this is known as the process of extraction, transformation, and loading (etl) data from the oltp system into the data warehouse. the workflow usually goes like this: at a pre-set time (at every change, hourly, nightly, or weekly) query the oltp data (extraction) and then aggregate and flatten it out (transformation) and then copy the transformed data to the data warehouse (star or snowflake) tables (load).
you have several options ranging from near real time etl (very complex and expensive) to nightly data dumps (least complex and least expensive.) you can also perform full etl (wipe out the data warehouse tables completely and refill on each operation) and partial etl (only add incremental data as per a time series.) the actual load can be done via database triggers on an add/update/delete for near real time to simple scheduled sql batches that wipe data and run your load scripts. most likely you will want to take a hybrid approach, depending on the maturity of your system. you have three basic options, ranging from least complex to most complex:
- direct database dump
- etl tool
- database triggers
when you have a long time series to publish your data, say nightly, or weekly, you can do a direct database dump. the process would be pretty straightforward. at a regular interval (or manually) a process would start that would query the oltp database and perform all of the aggregations, etc and then load it into a staging data warehouse database, then wipe out the production data warehouse and load the data in.
another option is to use an etl tool. a good example is sql server integration services (ssis) if you are using microsoft sql server. (actually ssis will work with multiple database, you just need a sql server host.) a modern etl tool will give you the ability to segment the work into logical groups, have a control flow based on success and failure of a condition, and allows rollbacks.
a typical workflow with an etl tool is that the etl will run on a schedule (or based on a condition, such as a message arriving from a queue or a record written to a admin table) and have a parameter(s) passed to it. this parameter is usually a time series and the etl will perform all of the extraction on data from the oltp database filtered by that parameter. an etl tool is the most likely solution you will employ, especially if you have to make frequent updates from your oltp system to your data warehouse.
another option is to use database triggers. for those of you that don’t know a lot about triggers, well they can lead to evil. ;) that said, they are events that fire when data changes. you can then write sql code to run when the data changes, even etl code. triggers are hard to debug and difficult to maintain, so i would only suggest that you use a trigger when you need “real time” updates to your data warehouse and even then, the trigger should only write a record into an admin table that your etl process is polling to get started.
a common design pattern with a sophisticated etl is to do all of the etl to a staging data warehouse and allow a check mechanism to verify that the etl was successful. once this check is performed (either by computer or by human, depending on the system), you can then push the data from the staging tables to the production tables. there is a sql operator specifically for this process, the merge operator. merge looks at two tables and looks at their joins and compares the data and will do add, updates, inserts, and deletes to keep the two tables in sync. here is how we would do that with a merge statement and our fact table from part ii.
--merge the fact local to fact remote merge dbo.factorder as t--target using northwind.dwh.factorder as s--source on t.productid = s.productid and t.employeeid = s.employeeid and t.shipperid = s.shipperid and t.timekey = s.timekey --joining keys --record exists in target, but data different when matched and (s.orderdate != t.orderdate or s.postalcode != t.postalcode or s.[total sales] != t.[total sales] or s.discount != t.discount or s.[unit sales] != t.[unit sales]) then update set t.orderdate = s.orderdate, t.postalcode = s.postalcode, t.[total sales] = s.[total sales], t.discount = s.discount, t.[unit sales] = s.[unit sales] when not matched by source then --only in target, get rid of it delete when not matched by target then --record only exists in source insert values (s.orderdate, s.postalcode, s.productid,s.employeeid, s.shipperid, s.[total sales], s.discount, s.[unit sales], s.[timekey]);--required!!
next we will warp up with the application development options.
Published at DZone with permission of Stephen Forte, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.