-- 建立源数据数据库 DROP DATABASE IF EXISTS source; CREATE DATABASE source; -- 建立数据仓库数据库 DROP DATABASE IF EXISTS dw; CREATE DATABASE dw; -- 建立源库表 USE source; -- 建立客户表 CREATE TABLE customer ( customer_number INT NOT NULL AUTO_INCREMENT PRIMARY KEY comment '客户编号,主键', customer_name VARCHAR(50) comment '客户名称', customer_street_address VARCHAR(50) comment '客户住址', customer_zip_code INT(5) comment '邮编', customer_city VARCHAR(30) comment '所在城市', customer_state VARCHAR(2) comment '所在省份' ); -- 建立产品表 CREATE TABLE product ( product_code INT NOT NULL AUTO_INCREMENT PRIMARY KEY comment '产品编码,主键', product_name VARCHAR(30) comment '产品名称', product_category VARCHAR(30) comment '产品类型' ); -- 建立销售订单表 CREATE TABLE sales_order ( order_number INT NOT NULL AUTO_INCREMENT PRIMARY KEY comment '订单号,主键', customer_number INT comment '客户编号', product_code INT comment '产品编码', order_date DATE comment '订单日期', entry_date DATE comment '登记日期', order_amount DECIMAL(10 , 2 ) comment '销售金额', foreign key (customer_number) references customer (customer_number) on delete cascade on update cascade, foreign key (product_code) references product (product_code) on delete cascade on update cascade ); -- 建立数据仓库表 USE dw; -- 建立客户维度表 CREATE TABLE customer_dim ( customer_sk INT NOT NULL AUTO_INCREMENT PRIMARY KEY comment '主键,代理键', customer_number INT comment '客户编号,业务主键', customer_name VARCHAR(50) comment '客户名称', customer_street_address VARCHAR(50) comment '客户住址', customer_zip_code INT(5) comment '邮编', customer_city VARCHAR(30) comment '所在城市', customer_state VARCHAR(2) comment '所在省份', effective_date DATE comment '生效日期', expiry_date DATE comment '到期日期' ); -- 建立产品维度表 CREATE TABLE product_dim ( product_sk INT NOT NULL AUTO_INCREMENT PRIMARY KEY comment '主键,代理键', product_code INT comment '产品编码,业务主键', product_name VARCHAR(30) comment '产品名称', product_category VARCHAR(30) comment '产品类型', effective_date DATE comment '生效日期', expiry_date DATE comment '到期日期' ); -- 建立订单维度表 CREATE TABLE order_dim ( order_sk INT NOT NULL AUTO_INCREMENT PRIMARY KEY comment '主键,代理键', order_number INT comment '订单号,业务主键', effective_date DATE comment '生效日期', expiry_date DATE comment '到期日期' ); -- 建立日期维度表 CREATE TABLE date_dim ( date_sk INT NOT NULL AUTO_INCREMENT PRIMARY KEY comment '主键,代理键', date DATE comment '日期', month_name VARCHAR(9) comment '月份名称', month INT(1) comment '月份', quarter INT(1) comment '季度', year INT(4) comment '年', effective_date DATE comment '生效日期', expiry_date DATE comment '到期日期' ); -- 建立销售订单事实表 CREATE TABLE sales_order_fact ( order_sk INT comment '订单维度主键', customer_sk INT comment '客户维度主键', product_sk INT comment '产品维度主键', order_date_sk INT comment '日期维度主键', order_amount DECIMAL(10 , 2 ) comment '销售金额', foreign key (order_sk) references order_dim (order_sk) on delete cascade on update cascade, foreign key (customer_sk) references customer_dim (customer_sk) on delete cascade on update cascade, foreign key (product_sk) references product_dim (product_sk) on delete cascade on update cascade, foreign key (order_date_sk) references date_dim (date_sk) on delete cascade on update cascade ); -- 建立过渡表 USE dw; -- 建立产品过渡表 CREATE TABLE product_stg ( product_code INT, product_name VARCHAR(30), product_category VARCHAR(30) ); -- 建立客户过渡表 CREATE TABLE customer_stg ( customer_number INT, customer_name VARCHAR(30), customer_street_address VARCHAR(30), customer_zip_code INT(5), customer_city VARCHAR(30), customer_state VARCHAR(2) ); -- 生成源库测试数据 USE source; -- 生成客户表测试数据 INSERT INTO customer (customer_name, customer_street_address, customer_zip_code, customer_city, customer_state) VALUES ('Really Large Customers', '7500 Louise Dr.',17050, 'Mechanicsburg','PA'), ('Small Stores', '2500 Woodland St.',17055, 'Pittsburgh','PA'), ('Medium Retailers','1111 Ritter Rd.',17055,'Pittsburgh','PA'), ('Good Companies','9500 Scott St.',17050,'Mechanicsburg','PA'), ('Wonderful Shops','3333 Rossmoyne Rd.',17050,'Mechanicsburg','PA'), ('Loyal Clients','7070 Ritter Rd.',17055,'Pittsburgh','PA'), ('Distinguished Partners','9999 Scott St.',17050,'Mechanicsburg','PA'); -- 生成产品表测试数据 INSERT INTO product (product_name, product_category ) VALUES ('Hard Disk Drive', 'Storage'), ('Floppy Drive', 'Storage'), ('LCD Panel', 'Monitor'); INSERT INTO sales_order VALUES (1, 1, 1, '2013-02-01', '2013-02-01', 1000) , (2, 2, 2, '2013-02-10', '2013-02-10', 1000) , (3, 3, 3, '2013-03-01', '2013-03-01', 4000) , (4, 4, 1, '2013-04-15', '2013-04-15', 4000) , (5, 5, 2, '2013-05-20', '2013-05-20', 6000) , (6, 6, 3, '2013-07-30', '2013-07-30', 6000) , (7, 7, 1, '2013-09-01', '2013-09-01', 8000) , (8, 1, 2, '2013-11-10', '2013-11-10', 8000) , (9, 2, 3, '2014-01-05', '2014-01-05', 1000) , (10, 3, 1, '2014-02-10', '2014-02-10', 1000) , (11, 4, 2, '2014-03-15', '2014-03-15', 2000) , (12, 5, 3, '2014-04-20', '2014-04-20', 2500) , (13, 6, 1, '2014-05-30', '2014-05-30', 3000) , (14, 7, 2, '2014-06-01', '2014-06-01', 3500) , (15, 1, 3, '2014-07-15', '2014-07-15', 4000) , (16, 2, 1, '2014-08-30', '2014-08-30', 4500) , (17, 3, 2, '2014-09-05', '2014-09-05', 1000) , (18, 4, 3, '2014-10-05', '2014-10-05', 1000) , (19, 5, 1, '2015-01-10', '2015-01-10', 4000) , (20, 6, 2, '2015-02-20', '2015-02-20', 4000) , (21, 7, 3, '2015-02-28', '2015-02-28', 4000); COMMIT; -- 建立日期维度数据生成的存储过程 USE dw; DELIMITER // ; DROP PROCEDURE IF EXISTS pre_populate_date // CREATE PROCEDURE pre_populate_date (IN start_dt DATE, IN end_dt DATE) BEGIN WHILE start_dt <= end_dt DO INSERT INTO date_dim( date_sk , date , month_name , month , quarter , year , effective_date , expiry_date ) VALUES( NULL , start_dt , MONTHNAME(start_dt) , MONTH(start_dt) , QUARTER(start_dt) , YEAR (start_dt) , '0000-00-00' , '9999-12-31' ) ; SET start_dt = ADDDATE(start_dt, 1); END WHILE; END // DELIMITER ; // -- 生成日期维度数据 SET FOREIGN_KEY_CHECKS=0; truncate table date_dim; call pre_populate_date('2000-01-01', '2020-12-31'); commit; SET FOREIGN_KEY_CHECKS=1;
//Create a Locale according to the specified language code var locale = new java.util.Locale( language_code.getString() , country_code.getString() ); //Create a calendar, use the specified initial date var calendar = new java.util.GregorianCalendar(locale); calendar.setTime(initial_date.getDate()); //set the calendar to the current date by adding DaySequence days calendar.add(calendar.DAY_OF_MONTH,DaySequence.getInteger() - 1); var simpleDateFormat = java.text.SimpleDateFormat("D",locale); //get the calendar date var date = new java.util.Date(calendar.getTimeInMillis()); simpleDateFormat.applyPattern("MM"); var month_number = simpleDateFormat.format(date); simpleDateFormat.applyPattern("MMMM"); var month_name = simpleDateFormat.format(date); simpleDateFormat.applyPattern("yyyy"); var year4 = "" + simpleDateFormat.format(date); var quarter_number; switch(parseInt(month_number)){ case 1: case 2: case 3: quarter_number = "1"; break; case 4: case 5: case 6: quarter_number = "2"; break; case 7: case 8: case 9: quarter_number = "3"; break; case 10: case 11: case 12: quarter_number = "4"; break; } var date_key = DaySequence;