Logo DWBI.org Login / Sign Up
Sign Up
Have Login?
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Login
New Account?
Recovery
Go to Login
By continuing you indicate that you agree to Terms of Service and Privacy Policy of the site.
Big Data

Hadoop DataLake Implementation Part 8

 
Updated on Oct 03, 2020

In this article we will load our first fact table into Hive warehouse which is sales transactions.

Load Sales Fact Table

Using Sqoop we will load the sales data, initial/base as well as incremental dataset from Source MySQL to HDFS.

ssh edw_user@192.168.136.139 -p 2222
sqoop job --meta-connect "jdbc:hsqldb:hsql://sandbox-hdp.hortonworks.com:16001/sqoop" \
--create jb_stg_sales \
-- import \
--bindir ./ \
--driver com.mysql.jdbc.Driver \
--connect jdbc:mysql://sandbox-hdp.hortonworks.com:3306/sales \
--username root \
--password-file /user/edw_user/sales/.password \
--table sales \
--fetch-size 1000 \
--as-textfile \
--fields-terminated-by '|' \
--target-dir /user/edw_user/sales/staging/sales \
--incremental append \
--check-column id \
--split-by id \
--num-mappers 2

Execute the sqoop job to perform initial extraction from source to hdfs.

sqoop job --meta-connect "jdbc:hsqldb:hsql://sandbox-hdp.hortonworks.com:16001/sqoop" --list
sqoop job --meta-connect "jdbc:hsqldb:hsql://sandbox-hdp.hortonworks.com:16001/sqoop" --show jb_stg_sales
sqoop job --meta-connect "jdbc:hsqldb:hsql://sandbox-hdp.hortonworks.com:16001/sqoop" --exec jb_stg_sales

Now we will define a hive external table for the Sales staging data as well as final Hive managed ORC dimension table. Connect to Beeline CLI using edw_user as username and password as hadoop. We will connect to hive schema ‘sales_analytics’.

One time setup

beeline
!connect jdbc:hive2://sandbox-hdp.hortonworks.com:10000/sales_analytics edw_user

CREATE EXTERNAL TABLE IF NOT EXISTS ext_sales (
id INT,
order_number VARCHAR(50),
customer_id INT,
showroom_id INT,
product_id INT,
quantity VARCHAR(50),
discount INT,
amount INT,
delivered VARCHAR(50),
card_type VARCHAR(50),
card_number VARCHAR(50),
txn_date DATE,
update_date TIMESTAMP,
create_date TIMESTAMP
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '|'
STORED AS TEXTFILE
LOCATION '/user/edw_user/sales/staging/sales';

SELECT * FROM ext_sales LIMIT 10;

Now we need to create an intermediate table to load the transformed data from staging in order to replace the product natural keys with product surrogate keys.

CREATE TABLE IF NOT EXISTS stg_sales (
id INT,
order_number VARCHAR(50),
customer_id INT,
showroom_id INT,
product_key INT,
quantity VARCHAR(50),
discount INT,
amount INT,
net_amount INT,
delivered VARCHAR(50),
card_type VARCHAR(50),
card_number VARCHAR(50),
update_date TIMESTAMP,
create_date TIMESTAMP,
txn_date DATE
)
STORED AS ORC 
TBLPROPERTIES ("orc.compress"="SNAPPY");

Next we will define the Hive managed ORC fact table.

CREATE TABLE IF NOT EXISTS fact_sales (
id INT,
order_number VARCHAR(50),
customer_id INT,
showroom_id INT,
product_key INT,
quantity VARCHAR(50),
discount INT,
amount INT,
net_amount INT,
delivered VARCHAR(50),
card_type VARCHAR(50),
card_number VARCHAR(50),
update_date TIMESTAMP,
create_date TIMESTAMP
)
PARTITIONED BY (txn_date DATE)
STORED AS ORC 
TBLPROPERTIES ("orc.compress"="SNAPPY");

!quit

Now we will define a Pig Script to replace the Product natural key with Surrogate key and loading sales data to final fact table. This script will be used later in oozie workflow manager to schedule the load.

Initial/Delta Setup

vi /home/edw_user/sampledata/transform_sales.pig

ext_sales = LOAD 'sales_analytics.ext_sales' USING org.apache.hive.hcatalog.pig.HCatLoader();
dim_product = LOAD 'sales_analytics.dim_product' USING org.apache.hive.hcatalog.pig.HCatLoader();
active_product = FILTER dim_product BY (active_flag == 'Y');
sales_product = JOIN ext_sales BY (product_id), active_product BY (id) PARALLEL 2;
stg_sales = FOREACH sales_product GENERATE ext_sales::id AS id, order_number AS order_number, customer_id AS customer_id, showroom_id AS showroom_id, prod_key AS product_key, quantity AS quantity, discount AS discount, amount AS amount, amount-discount AS net_amount, delivered AS delivered, card_type AS card_type, card_number AS card_number, ext_sales::update_date AS update_date, ext_sales::create_date AS create_date, txn_date AS txn_date;
STORE stg_sales INTO 'sales_analytics.stg_sales' USING org.apache.hive.hcatalog.pig.HCatStorer();
quit;
hdfs dfs -put /home/edw_user/sampledata/transform_sales.pig /user/edw_user/sales/scripts

Execute the Pig Script to trigger the initial data transformation & loading.

pig -x tez -useHCatalog -f "/home/edw_user/sampledata/transform_sales.pig"

Finally we load data from staging sales table to final fact table. This script will also be used later by oozie.

vi /home/edw_user/sampledata/load_sales.hql

set hive.execution.engine=tez;
set hive.optimize.sort.dynamic.partition=true;
set hive.exec.reducers.max=31;

SELECT * FROM stg_sales LIMIT 10;

INSERT INTO TABLE fact_sales PARTITION(txn_date) 
SELECT 
id, order_number, customer_id, showroom_id, product_key, quantity, discount, amount, 
amount-discount, delivered, card_type, card_number, create_date, update_date, txn_date
FROM stg_sales DISTRIBUTE BY txn_date;

ANALYZE TABLE fact_sales PARTITION(txn_date) COMPUTE STATISTICS FOR COLUMNS;
hdfs dfs -put /home/edw_user/sampledata/load_sales.hql /user/edw_user/sales/scripts

Execute the script to trigger the initial data load.

beeline -u jdbc:hive2://sandbox-hdp.hortonworks.com:10000/sales_analytics -n edw_user -p hadoop -d org.apache.hive.jdbc.HiveDriver -f "/home/edw_user/sampledata/load_sales.hql"

Let us quickly check the data loaded.

beeline
!connect jdbc:hive2://sandbox-hdp.hortonworks.com:10000/sales_analytics edw_user
SELECT * FROM stg_sales LIMIT 10;
SELECT * FROM fact_sales LIMIT 10;
SELECT fact_sales.* FROM dim_date, fact_sales WHERE day_date=txn_date AND day_of_week_number=7;

SELECT p.make, sum(s.net_amount) as sales
FROM fact_sales s, dim_product p
WHERE s.product_key = p.prod_key
GROUP BY p.make;

!quit

Finally copy & archive the sales datafiles.

vi /home/edw_user/sampledata/archive_sales.sh

hdfs dfs -mkdir /user/edw_user/sales/archive/sales/`date +%Y%m%d`
hdfs dfs -mv /user/edw_user/sales/staging/sales/* /user/edw_user/sales/archive/sales/`date +%Y%m%d`
hdfs dfs -put /home/edw_user/sampledata/archive_sales.sh /user/edw_user/sales/scripts
sh /home/edw_user/sampledata/archive_sales.sh

exit

In the next article we will load our stocks fact table followed by some analytics in excel using Hive sales mart tables.

PrimeChess

PrimeChess.org

PrimeChess.org makes elite chess training accessible and affordable for everyone. For the past 6 years, we have offered free chess camps for kids in Singapore and India, and during that time, we also observed many average-rated coaches charging far too much for their services.

To change that, we assembled a team of top-rated coaches including International Masters (IM) or coaches with multiple IM or GM norms, to provide online classes starting from $50 per month (8 classes each month + 4 tournaments)

This affordability is only possible if we get more students. This is why it will be very helpful if you could please pass-on this message to others.

Exclucively For Indian Residents: 
Basic - ₹1500
Intermediate- ₹2000
Advanced - ₹2500

Top 10 Articles