学习笔记系列----1、kettle之js实现日期维度

2017-05-16  本文已影响0人  夏无忧阳

最近在《Pentaho Kettle解决方案:使用PDI构建开源ETL解决方案》一书中看到了构建时间维度的kettle过程,进行一下学习记录。对kettle有兴趣的可以去看一下这本书,很不错的一本书。

整体


分解


  1. Generate 1 years


    生成一年的数据行

    生成一年的数据行(365条),并生成几个常量。
    initial_date设置初始日期是 2017-01-01
    language_code 语言代码设置成en
    country_code地区代码设置成gb

  2. Day Sequence


    生成自增序列

    生成增增的序列数字,用以后面和initial_date相加生成一系列连续的日期数据。
    预览该步骤的效果如下:


    Day Sequence预览
  3. Calculate Dimension Attributes

    生成时间维度
//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());            //这里是将date格式转化为calendar格式

   //set the calendar to the current date by adding DaySequence days
calendar.add(calendar.DAY_OF_MONTH,DaySequence.getInteger() - 1);   //Calendar的add()方法可以根据日历的规则,为给定的日历字段添加或减去指定的时间量。

   //get the calendar date
var date = new java.util.Date(calendar.getTimeInMillis());   //calendar.getTimeInMillis()方法返回此Calendar以毫秒为单位的时间。一般用做测试代码执行效率。
//en-us example: 9/3/07
var date_short  = java.text.DateFormat.getDateInstance(      //格式化日期   Calendar类的静态方法getInstance()可以初始化一个日历对象
                     java.text.DateFormat.SHORT
                 ,   locale
                 ).format(date);
//en-us example: Sep 3, 2007 
var date_medium = java.text.DateFormat.getDateInstance(
                     java.text.DateFormat.MEDIUM
                 ,   locale
                 ).format(date);
//en-us example: September 3, 2007
var date_long   = java.text.DateFormat.getDateInstance(
                     java.text.DateFormat.LONG
                 ,   locale
                 ).format(date);
//en-us example: Monday, September 3, 2007
var date_full   = java.text.DateFormat.getDateInstance(
                     java.text.DateFormat.FULL
                 ,   locale
                 ).format(date);

   //day in year: 1..366
var simpleDateFormat   = java.text.SimpleDateFormat("D",locale);    //“D”  Day   in   year
var day_in_year        = simpleDateFormat.format(date);
//day in month: 1..31
simpleDateFormat.applyPattern("d");       //“d”  Day   in   month 
var day_in_month       = simpleDateFormat.format(date);
//en-us example: "Monday"
simpleDateFormat.applyPattern("EEEE");    // E:一周中的第几天,对应星期几,第一天为星期日,于此类推
var day_name           = simpleDateFormat.format(date);
//en-us example: "Mon"
simpleDateFormat.applyPattern("E");    
var day_abbreviation   = simpleDateFormat.format(date);
//week in year, 1..53
simpleDateFormat.applyPattern("ww");   // w: 一年中的第几个星期
var week_in_year       = simpleDateFormat.format(date);
//week in month, 1..5
simpleDateFormat.applyPattern("W");     // W:一个月中的第几个星期
var week_in_month      = simpleDateFormat.format(date);
//month number in year, 1..12
simpleDateFormat.applyPattern("MM");  // M :月份
var month_number       = simpleDateFormat.format(date);
//en-us example: "September"
simpleDateFormat.applyPattern("MMMM");
var month_name         = simpleDateFormat.format(date);
//en-us example: "Sep"
simpleDateFormat.applyPattern("MMM");
var month_abbreviation = simpleDateFormat.format(date);
//2 digit representation of the year, example: "07" for 2007
simpleDateFormat.applyPattern("y");   // y 年份
var year2              = simpleDateFormat.format(date);
//4 digit representation of the year, example:  2007
simpleDateFormat.applyPattern("yyyy");  
var year4              = "" + simpleDateFormat.format(date);
//handling Quarters is a DIY
var quarter_name = "Q";
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;
}
quarter_name += quarter_number;

   //get the local yes/no values
var yes = local_yes.getString();
var no = local_no.getString();

   //initialize for week calculations  
var first_day_of_week = calendar.getFirstDayOfWeek();   // java.util.Calendar.getFirstDayOfWeek() 方法返回一周的第一天。
var day_of_week = java.util.Calendar.DAY_OF_WEEK; 

   //find out if this is the first day of the week
var is_first_day_in_week;
if(first_day_of_week==calendar.get(day_of_week)){
   is_first_day_in_week = yes;
} else {
   is_first_day_in_week = no;
}

   //calculate the next day
calendar.add(calendar.DAY_OF_MONTH,1);
//get the next calendar date
var next_day = new java.util.Date(calendar.getTimeInMillis());

   //find out if this is the first day of the week
var is_last_day_in_week;
if(first_day_of_week==calendar.get(day_of_week)){
   is_last_day_in_week = yes;
} else {
   is_last_day_in_week = no;
}

   //find out if this is the first day of the month
var is_first_day_of_month;
if(day_in_month == 1){
   is_first_day_in_month = yes;
} else {
   is_first_day_in_month = no;
}

   //find out if this is the last day in the month
var is_last_day_of_month;
if(java.text.SimpleDateFormat("d",locale).format(next_day)==1){
   is_last_day_in_month = yes;
} else {
   is_last_day_in_month = no;
}

   //date = year4 + "-" + month_number + "-" + day_in_month
var year_quarter            = year4 + "-" + quarter_name;
var year_month_number       = year4 + "-" + month_number;
var year_month_abbreviation = year4 + "-" + month_abbreviation;

   var date_key = year4 + month_number + (day_in_month<10?"0":"") + day_in_month;
  1. 排序记录


    记录按日期排序

    让记录按日期增长排序

  2. Load dim_date

输出到表

接受上一个步骤输出的数据,生成相应的sql命令,最终将数据插入维度表dim_data中。

dim_data生成的结果示例:

date_key    date_value  date_short  date_medium date_long   date_full   day_in_year day_in_month    is_first_day_in_month   is_last_day_in_month    day_abbreviation    day_name    week_in_year    week_in_month   is_first_day_in_week    is_last_day_in_week month_number    month_abbreviation  month_name  year2   year4   quarter_name    quarter_number  year_quarter    year_month_number   year_month_abbreviation

20170101   2017-01-01    01/01/17   01-Jan-2017  01 January 2017  Sunday, 1 January 2017    1   1   yes no  Sun Sunday  52  0   no  yes 1   Jan January 20  2017    Q1  1   2017-Q1 2017-01 2017-Jan

js代码中的相关类和方法


  1. java.util.Locale
    定义:
  1. ** java.util.GregorianCalendar**
    java.util.GregorianCalendar 类是Calendar的一个具体子类,提供用于世界上大多数国家的标准日历系统。以下是关于GregorianCalendar的要点:
  1. java.text.SimpleDateFormat:
    SimpleDateFormat 是 Java 中一个非常常用的类,该类用来对日期字符串进行解析和格式化输出。
    参考:java时间格式化----java.text.SimpleDateFormat
上一篇下一篇

猜你喜欢

热点阅读