一、维度表模型
| 字段 | 说明 |
|---|---|
| dt | 日历日期 |
| lunar_dt | 农历日期 |
| calendar_year | 年份 |
| quarter_of_year | 季度 |
| month_of_year | 月份 |
| week_of_year | 一年第几周 |
| dt_of_week | 一周第几天 |
| dt_of_month | 一个月第几天 |
| dt_of_year | 一年第几天 |
| is_last_day_of_month | 是否一个月最后一天 |
| is_weekend | 是否周末日 |
| is_holiday | 是否节假日 |
| holiday_cnt | 节日天数 |
| holiday_type | 日历天类型:1:工作日;2:周末(和假期连着按假期,因节假日调休上班按工作日算:3:元旦 4:春节 5:清明节 6:劳动节:7:端午节 8:中秋节 9:国庆 |
二、维度表数据生成
基于python3构建维度表数据。之后可以导入到hdfs,并构建hive表来映射到这份数据。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import imp
import requests
import json
import datetime
import time
import calendar
import lunardate
import csv
imp.reload(sys) # reload 才能调用 setdefaultencoding 方法
holiday_dict = {
}
# 判断二月闰月
def isLeapYear(year):
return calendar.monthrange(year, 2)[1] == 29
# 季度信息
def quarter(month):
return (month - 1) / 3 + 1
# 周信息
def get_week_of_year(dt):
return dt.isocalendar()[1]
def get_day_of_year(dt):
return dt.timetuple().tm_yday
def get_day_of_week(day):
return day.weekday() + 1 % 7
# 返回是否最后一天
def last_day_of_month(year, month, day):
return day == calendar.monthrange(year, month)[1]
def get_is_weekend(day):
return day.weekday() == 5 or day.weekday(

本文介绍了一个使用Python构建日期维度表的方法,包括了日历日期、农历日期、节假日等信息的生成过程。通过此方法,可以方便地为数据分析提供丰富的日期维度数据。
最低0.47元/天 解锁文章
4530





