[hive]生成日期维度表

create table if not exists dim_date (
date_pt string comment '日期(yyyymmdd)',
date_date string comment '日期(yyyy-mm-dd)',
date_name string comment '日期名称中文',
week_id int comment '周(0-6,周日~周六)',
week_cn_name string comment '周_名称_中文',
week_en_name string comment '周_名称_英文',
week_en_nm string comment '周_名称_英文缩写',
year_month_pt string comment '月份id(yyyymm)',
year_month_date string comment '月份(yyyy-mm)',
month_id int comment '月份id(1-12)',
date_month string comment '月份',
month_cn_name string comment '月份名称_中文',
month_en_name string comment '月份名称_英文',
month_en_nm string comment '月份名称_简写_英文',
quarter_id int comment '季度id(1-4)',
quarter_str string comment '季度名称',
quarter_cn_name string comment '季度名称_中文',
quarter_en_name string comment '季度名称_英文',
quarter_cn_nm string comment '季度名称_简写中文',
quarter_en_nm string comment '季度名称_简写英文',
date_year int comment '年份id',
year_cn_name string comment '年份名称_中文',
year_en_name string comment '年份名称_英文',
month_start_date string comment '当月1号(yyyy-mm-dd)',
month_end_date string comment '当月最后日期(yyyy-mm-dd)',
month_timespan int comment '月跨天数',
week_of_year int comment '当年第几周',
workday_flag string comment '是否工作日(周一至周五Y,否则:N)',
weekend_flag string comment '是否周末(周六和周日Y,否则:N)'
)
;


insert overwrite table dim_date
select 
date_id,
datestr,
concat(yearid,'年',monthid,'月',substr(datestr,9,2),'日') as date_name,
weekid,
case weekid when 0 then '星期日' when 1 then '星期一' when 2 then '星期二' when 3 then '星期三' when 4 then '星期四' when 5 then '星期五' when 6 then '星期六' end as week_cn_name,
case weekid when 0 then 'Sunday' when 1 then 'Monday' when 2 then 'Tuesday' when 3 then 'Wednesday' when 4 then 'Thurday' when 5 then 'Friday' when 6 then 'Saturday' end as week_en_name,
case weekid when 0 then 'Sun' when 1 then 'Mon' when 2 then 'Tues' when 3 then 'Wed' when 4 then 'Thur' when 5 then 'Fri' when 6 then 'Sat' end as week_en_nm,
substr(date_id,1,6) as yearmonthid,
substr(datestr,1,7) as yearmonthstr,
monthid,
concat(yearid,'年',monthid,'月') as monthstr,
concat(monthid,'月') as month_cn_name,
case monthid when 1 then 'January' when 2 then 'February' when 3 then 'March' when 4 then 'April' when 5 then 'May' when 6 then 'June' when 7 then 'July' when 8 then 'August' when 9 then 'September' when 10 then 'October' when 11 then 'November' when 12 then 'December' end as month_en_name,
case monthid when 1 then 'Jan' when 2 then 'Feb' when 3 then 'Mar' when 4 then 'Apr' when 5 then 'May' when 6 then 'Jun' when 7 then 'Jul' when 8 then 'Aug' when 9 then 'Sept' when 10 then 'Oct' when 11 then 'Nov' when 12 then 'Dec' end as month_en_nm,
quarterid,
concat(yearid,quarterid) as quarterstr,
concat(yearid,'年第',quarterid,'季度') as quarter_cn_name,
concat(yearid,'Q',quarterid) as quarter_en_name,
case quarterid when 1 then '第一季度' when 2 then '第二季度' when 3 then '第三季度' when 4 then '第四季度' end as quarter_cn_nm,
concat('Q',quarterid) as quarter_en_nm,
yearid,
concat(yearid,'年') as year_cn_name,
yearid as year_en_name,
month_start_date,
month_end_date,
datediff(month_end_date,month_start_date) + 1 as month_timespan,
week_of_year,
case when weekid in (1,2,3,4,5) then 'Y' else 'N' end as workday_flag,
case when weekid in (0,6) then 'Y' else 'N' end as weekend_flag
from
(
    select from_unixtime(unix_timestamp(datestr,'yyyy-MM-dd'),'yyyyMMdd') as date_id
    ,datestr as datestr
    ,pmod(datediff(datestr, '2012-01-01'), 7) as weekid
    ,concat(substr(datestr,1,4),substr(datestr,6,2)) as yearmonthid
    ,substr(datestr,1,7) as yearmonthstr
    ,cast(substr(datestr,6,2) as int) as monthid
    ,case when cast(substr(datestr,6,2) as int) <= 3 then 1
    when cast(substr(datestr,6,2) as int) <= 6 then 2
    when cast(substr(datestr,6,2) as int) <= 9 then 3
    when cast(substr(datestr,6,2) as int) <= 12 then 4
    end as quarterid
    ,substr(datestr,1,4) as yearid
    ,date_sub(datestr,dayofmonth(datestr)-1) as month_start_date --当月第一天
    ,last_day(date_sub(datestr,dayofmonth(datestr)-1)) month_end_date --当月最后一天
    ,weekofyear(datestr) as week_of_year
    from
    (
        select date_add('2000-01-01',t0.pos) as datestr
        from
        (
            select posexplode(split(repeat('o', datediff(from_unixtime(unix_timestamp('20991231','yyyymmdd'),'yyyy-mm-dd'), '2000-01-01')), 'o'))
        ) t0
    ) t1
) t2
;

Hive是一个开源的数据仓库和查询工具,用于将大数据处理和分析集成在Hadoop生态系统中。拉链表是一种在Hive中实现的数据处理技术,主要用于处理维度数据的历史变化。 拉链表的实现思路是将每个维度表根据指定的生效日期和失效日期进行拆分,生成多个对应不同时间段的维度数据,以保留维度表的历史变化记录。在Hive中,可以通过以下步骤来实现拉链表: 1. 创建维度表和事实表:首先,创建维度表和事实表的Hive表。维度表用于存储维度字段的详细信息,例如员工表、产品表等;事实表用于存储与维度表关联的度量数据,例如销售事实表。 2. 设计拉链表结构:在维度表中添加生效日期(start_date)和失效日期(end_date)字段,用于标识每条记录的有效时间段。通常,失效日期为空或未来日期表示当前有效数据。 3. 插入初始数据:将初始数据插入维度表,即没有历史记录的部分。在start_date字段中填写最早的日期,end_date字段中填写NULL或未来日期。 4. 插入新数据:当维度表中的记录有更新或新增时,需要按照拉链表的原则进行插入。具体操作是将原有的生效日期字段(start_date)的end_date字段更新为当前日期,并将新数据插入到维度表中。 5. 查询数据:在查询维度表和事实表时,可以通过使用日期条件和JOIN操作,将最近生效的维度数据关联到事实数据上,以获得正确的历史维度信息。 拉链表的实现使得Hive可以处理维度数据的历史变化情况,并提供了便捷的方式来查询和分析历史数据。它对于构建具有时间依赖性的报表和分析非常有用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值