Docsity

Regular Population Part 2-Data Warehouse-Lab Mannual, Lecture notes of Data Warehousing

Toggle sidebar
bg1
Chapter 8: Regular Population
This chapter covers regular population. Unlike initial population that you perform once only
before the start of your data warehouse operation, you schedule a regular population to load the
source data regularly.
In this chapter I show you how to prepare data before running a script that does regular
population in our dw database.
Identifying Data Sources and Loading Types
The first step to schedule a regular population is identify what source data is needed and
available for every fact and every dimension of the data warehouse. Afterwards, you decide the
extraction mode and the loading type suitable for the population. A sample document that
summarizes this information is shown in Table 8.1.
Table 8.1: Data sources and loading types of regular population
Open table as spreadsheet
Source Data
Data Warehouse Table
Extraction Mode
Loading Type
Customer
customer_dim
Whole, Pull
SCD2 on address
SCD1 on name
Product
product_dim
Whole, Pull
SCD2
Sales order Transaction
order_dim
CDC (daily), Pull
Unique order number
sales_order_fact
CDC (daily), Pull
Daily sales orders
n/a
date_dim
n/a
Pre-populate
Note
You learned about extraction mode in Chapter 5, โ€œSource Extractionโ€ and SCD in Chapter
2, โ€œDimension History.โ€
Another aspect of the source data that might have impact on your design is the window of time
when a particular data is available for the regular population. This is especially important for
transactional source data that is usually large, such as sales orders.
In addition, you need to know the detailed characteristics of every source data, such as its file
type and record structure, down to the individual field or column.
Regular Population Script
I use the script in Listing 8.1. to explain how regular population works. You can use it to
populate the data warehouse on a daily basis. The extraction mode and loading type used are as
follows:
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Regular Population Part 2-Data Warehouse-Lab Mannual and more Lecture notes Data Warehousing in PDF only on Docsity!

Chapter 8: Regular Population

This chapter covers regular population. Unlike initial population that you perform once only

before the start of your data warehouse operation, you schedule a regular population to load the

source data regularly.

In this chapter I show you how to prepare data before running a script that does regular

population in our dw database.

Identifying Data Sources and Loading Types

The first step to schedule a regular population is identify what source data is needed and

available for every fact and every dimension of the data warehouse. Afterwards, you decide the

extraction mode and the loading type suitable for the population. A sample document that

summarizes this information is shown in Table 8.1.

Table 8.1: Data sources and loading types of regular population

Open table as spreadsheet

Source Data Data Warehouse Table Extraction Mode Loading Type

Customer customer_dim Whole, Pull SCD2 on address

SCD1 on name

Product product_dim Whole, Pull SCD

Sales order Transaction order_dim CDC (daily), Pull Unique order number

sales_order_fact CDC (daily), Pull Daily sales orders

n/a date_dim n/a Pre-populate

Note You learned about extraction mode in Chapter 5, โ€œSource Extractionโ€ and SCD in Chapter

2 , โ€œDimension History.โ€

Another aspect of the source data that might have impact on your design is the window of time

when a particular data is available for the regular population. This is especially important for

transactional source data that is usually large, such as sales orders.

In addition, you need to know the detailed characteristics of every source data, such as its file

type and record structure, down to the individual field or column.

Regular Population Script

I use the script in Listing 8.1. to explain how regular population works. You can use it to

populate the data warehouse on a daily basis. The extraction mode and loading type used are as

follows:

๏‚ท The customer.csv and product.txt flat files are loaded into the customer_dim and

product_dim tables through the customer_stg and product_stg tables, respectively.

Loading is achieved through the use of MySQLโ€™s LOAD DATA INFILE utility.

๏‚ท SCD2 is applied to customer addresses, product names, and product groups. SCD1 is

applied to customer names.

๏‚ท Only sales orders entered on the current date are loaded to the order_dim and

sales_order_fact tables.

Listing 8.1: Daily dw regular population

/* dw_regular.sql / / / /*****************************************************************/ USE dw; / customer dimension loading / TRUNCATE customer_stg ; LOAD DATA INFILE 'customer.csv' INTO TABLE customer_stg FIELDS TERMINATED BY ', ' OPTIONALLY ENCLOSED BY ' " ' LINES TERMINATED BY '\r\n' IGNORE 1 LINES ( customer_number , customer_name , customer_street_address , customer_zip_code , customer_city , customer_state) ; / SCD2 on customer street addresses / / first, expire the existing customers / UPDATE customer_dim a , customer_stg b SET expiry_date = SUBDATE (CURRENT_DATE, 1) WHERE a.customer_number = b.customer_number AND a.customer_street_address <> b.customer_street_address AND expiry_date = '9999โ€“ 12 โ€“ 31' ; / then, add a new row for the customer */ INSERT INTO customer_dim SELECT

/* product dimension loading / TRUNCATE product_stg ; LOAD DATA INFILE 'product.txt' INTO TABLE product_stg FIELDS TERMINATED BY ' ' OPTIONALLY ENCLOSED BY ' ' LINES TERMINATED BY '\r\n' IGNORE 1 LINES ( product_code , product_name , product_category) ; / SCD2 on product name and group / / first, expire the existing product / UPDATE product_dim a , product_stg b SET expiry_date = SUBDATE (CURRENT_DATE, 1) WHERE a.product_code = b.product_code AND ( a.product_name <> b.product_name OR a.product_category <> b.product_category) AND expiry_date = '9999โ€“ 12 โ€“ 31' ; / then, add a new row for the product */ INSERT INTO product_dim SELECT NULL , b.product_code , b.product_name , b.product_category , CURRENT_DATE ,'9999โ€“ 12 โ€“ 31' FROM product_dim a , product_stg b WHERE a.product_code = b.product_code AND ( a.product_name <> b.product_name OR a.product_category <> b.product_category) AND EXISTS( SELECT * FROM product_dim x WHERE b.product_code = x.product_code AND a.expiry_date = SUBDATE (CURRENT_DATE, 1)) AND NOT EXISTS ( SELECT * FROM product_dim y

WHERE b.product_code = y.product_code AND y.expiry_date = '9999โ€“ 12 โ€“ 31') ; /* add new product / INSERT INTO product_dim SELECT NULL , product_code , product_name , product_category , CURRENT_DATE , '9999โ€“ 12 โ€“ 31' FROM product_stg WHERE product_code NOT IN( SELECT y.product_code FROM product_dim x, product_stg y WHERE x.product_code = y.product_code) ; / end of product_dim loading */ INSERT INTO order_dim ( order_sk , order_number , effective_date , expiry_date) SELECT NULL , order_number , order_date , '9999โ€“ 12 โ€“ 31' FROM source.sales_order WHERE entry_date = CURRENT_DATE ; INSERT INTO sales_order_fact SELECT order_sk , customer_sk , product_sk , date_sk , order_amount FROM source.sales_order a , order_dim b , customer_dim c , product_dim d , date_dim e WHERE a.order_number = b.order_number AND a.customer_number = c.customer_number AND a.order_date >= c.effective_date AND a.order_date <= c.expiry_date AND a.product_code = d.product_code AND a.order_date >= d.effective_date AND a.order_date <= d.expiry_date

Sales Order Transactions

The last test data you need to prepare is the sales orders. Assuming you start the data warehouse

operation on March 1, 2007 (the date the first time you run the regular population). The script in

Listing 8.2 adds 16 sales orders with 2007โ€“ 03 โ€“ 01 order dates.

Listing 8.2: Adding sales orders

/* sales_order_regular.sql / / / /*****************************************************************/ USE source; INSERT INTO sales_order VALUES (22, 1, 1, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 1000) , (23, 2, 2, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 2000) , (24, 3, 3, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 3000) , (25, 4, 4, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 4000) , (26, 5, 2, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 1000) , (27, 6, 2, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 3000) , (28, 7, 3, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 5000) , (29, 8, 4, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 7000) , (30, 1, 1, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 1000) , (31, 2, 2, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 2000) , (32, 3, 3, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 4000) , (33, 4, 4, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 6000) , (34, 5, 1, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 2500) , (35, 6, 2, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 5000) , (36, 7, 3, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 7500) , (37, 8, 4, '2007โ€“ 03 โ€“ 01', '2007โ€“ 03 โ€“ 01', 1000) ; / end of script */

Run the sales_order_regular.sql the script using this command.

mysql> . c:\mysql\scripts\sales_order_regular.sql

The response on your console should be similar to this.

Database changed Query OK, 16 rows affected (0.08 sec) Records: 16 Duplicates: 0 Warnings: 0

The sales_order table now has a total of 37 rows.

Running the Regular Population Script

Before you run the dw_regular.sql script in Listing 8.1, you need to set your MySQL date to

March 1, 2007. Then, run the script by using this command.

mysql> . c:\mysql\scripts\dw_regular.sql

You will see this on your console.

Database changed Query OK, 7 rows affected (0.12 sec) Query OK, 8 rows affected (0.05 sec) Records: 8 Deleted: 0 Skipped: 0 Warnings: 0 Query OK, 1 row affected (0.06 sec) Rows matched: 1 Changed: 1 Warnings: 0 Query OK, 1 row affected (0.07 sec) Records: 1 Duplicates: 0 Warnings: 0 Query OK, 2 rows affected (0.06 sec) Rows matched: 2 Changed: 2 Warnings: 0 Query OK, 1 row affected (0.06 sec) Records: 1 Duplicates: 0 Warnings: 0 Query OK, 3 rows affected (0.05 sec) Query OK, 4 rows affected (0.08 sec) Records: 4 Deleted: 0 Skipped: 0 Warnings: 0 Query OK, 1 row affected (0.05 sec) Rows matched: 1 Changed: 1 Warnings: 0 Query OK, 1 row affected (0.06 sec) Records: 1 Duplicates: 0 Warnings: 0 Query OK, 1 row affected (0.06 sec) Records: 1 Duplicates: 0 Warnings: 0 Query OK, 16 rows affected (0.07 sec) Records: 16 Duplicates: 0 Warnings: 0 Query OK, 16 rows affected (0.11 sec) Records: 16 Duplicates: 0 Warnings: 0

Confirming Successful Regular Population

To confirm your regular population was successful, you need to query the dimension and the fact

tables.

To query the customer_dim table, use this SQL statement.

customer_number: 6 customer_name: Extremely Loyal Clients customer_street_address: 7070 Ritter Rd. customer_zip_code: 17055 customer_city: Pittsburgh customer_state: PA effective_date: 2005 โ€“ 03 โ€“ 01 expiry_date: 2007 โ€“ 02 โ€“ 28 *************************** 7. row *************************** customer_sk: 7 customer_number: 7 customer_name: Distinguished Agencies customer_street_address: 9999 Scott St. customer_zip_code: 17050 customer_city: Mechanicsburg customer_state: PA effective_date: 2005 โ€“ 03 โ€“ 01 expiry_date: 9999 โ€“ 12 โ€“ 31 *************************** 8. row *************************** customer_sk: 8 customer_number: 6 customer_name: Extremely Loyal Clients customer_street_address: 7777 Ritter Rd. customer_zip_code: 17055 customer_city: Pittsburgh customer_state: PA effective_date: 2007 โ€“ 03 โ€“ 01 expiry_date: 9999 โ€“ 12 โ€“ 31 *************************** 9. row *************************** customer_sk: 9 customer_number: 8 customer_name: Subsidiaries customer_street_address: 10000 Wetline Blvd. customer_zip_code: 17055 customer_city: Pittsburgh customer_state: PA effective_date: 2007 โ€“ 03 โ€“ 01 expiry_date: 9999 โ€“ 12 โ€“ 31 9 rows in set (0.00 sec)

The query result shows you that:

๏‚ท SCD2 was applied on the street address of Customer 6

๏‚ท SCD1 was applied to the name of both rows of Customer 6

๏‚ท SCD1 was applied to the name of Customer 7

๏‚ท A new customer 8 was added

To query the product_dim table, use this SQL statement.

mysql> select * from product_dim \G

The result is as follows.

*************************** 1. row ***************************

product_sk: 1 product_code: 1 product_name: Hard Disk Drive product_category: Storage effective_date: 2005 โ€“ 03 โ€“ 01 expiry_date: 9999 โ€“ 12 โ€“ 31 *************************** 2. row *************************** product_sk: 2 product_code: 2 product_name: Floppy Drive product_category: Storage effective_date: 2005 โ€“ 03 โ€“ 01 expiry_date: 9999 โ€“ 12 โ€“ 31 *************************** 3. row *************************** product_sk: 3 product_code: 3 product_name: LCD Panel product_category: Monitor effective_date: 2005 โ€“ 03 โ€“ 01 expiry_date: 2007 โ€“ 02 โ€“ 28 *************************** 4. row *************************** product_sk: 4 product_code: 3 product_name: Flat Panel product_category: Monitor effective date: 2007 โ€“ 03 โ€“ 01 expiry_date: 9999 โ€“ 12 โ€“ 31 *************************** 5. row ************************** product_sk: 5 product_code: 4 product_name: Keyboard product_category: Peripheral effective_date: 2007 โ€“ 03 โ€“ 01 expiry_date: 9999 โ€“ 12 โ€“ 31 5 rows in set (0.01 sec)

The result from querying the product_dim table shows you that

๏‚ท SCD2 was applied to the name of Product 3

๏‚ท The new product 4 was added

Now, query the order_dim table.

mysql> select * from order_dim;

Here is the result. You should now have 35 orders, 19 of which were loaded in Chapter 7, โ€œInitial

Populationโ€ and 16 of which were loaded in this chapter.

| order_sk | order_number | effective_date | expiry_date | +----------+--------------+----------------+-------------+ | 1 | 3 | 2005 โ€“ 03 โ€“ 01 | 9999 โ€“ 12 โ€“ 31 | | 2 | 4 | 2005 โ€“ 04 โ€“ 15 | 9999 โ€“ 12 โ€“ 31 | | 3 | 5 | 2005 โ€“ 05 โ€“ 20 | 9999 โ€“ 12 โ€“ 31 |

35 rows ir n set (0.00 sec :)

The query result shows that:

๏‚ท The sixteen sales orders entered on March 1, 2007 were added

๏‚ท The valid product and customer were picked up correctly based on the order dates:

o Surrogate key 4 for Product 3, not the one with surrogate key 3

o Surrogate key 8 for Customer 6, not the one with surrogate key 6

Summary

In this chapter you learned and tested a daily regular population in which the pull mode, whole

source, and CDC extractions as well as SCD1 and 2 were applied. In the next chapter, you learn

how to schedule this regular population to run daily on Windows.