时间维表的建立(完整)-并从Hive导出到Mysql

                                时间维表的建立完整版 

目录

                                时间维表的建立完整版 

思维方式 

步骤1:粘贴就能用

获取基础时间数据( 注意我的数据是导出到桌面上,你可以导出到HDFS上也可以 请细看代码注释 )

步骤2

    2.1 上传文件到Linux   (上传方法自己找)

    2.2  建表  转存到Hive中

步骤3.

    建时间维度表

步骤4:

    经过Hsql时间函数转换 得到各种各样的时间 导入数据到Hive的时间维表中

步骤5:

    检查数据

步骤6:

    Mysql建表

    使用sqoop导出数据

步骤7

    检查Mysql数据

没问题 , ok 


思维方式 

    步骤1.

先获取这样的   时间 数据

2026-03-03
2026-03-04
2026-03-05
2026-03-06
2026-03-07
2026-03-08
2026-03-09
2026-03-10

    步骤2.

建时间数据表

导入数据到Hive中

    步骤3.

建时间维表

   步骤4.

经过Hsql时间函数转换 得到各种各样的时间 导入数据到Hive的时间维表中

   步骤5.

检查数据是否准确

   步骤5.

sqoop导出到Mysql 

步骤1:粘贴就能用

获取基础时间数据( 注意我的数据是导出到桌面上,你可以导出到HDFS上也可以 请细看代码注释 )

package timeDimensionTableFunction;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName TimeTableFunction
 * @Description:
 * @Author 庄德帝
 * @Date 2020/9/18
 * @Version V1.0
 **/
public class TimeTableFunction {

    public static void main(String[] args) throws IOException {

        /**
         * 1.获取当前时间戳
         * 2.获取昨日的时间
         * 3.fou循环递减时间
         * 4.获取一个时间格式 2020-09-18
         * */
        //字符输出流; 文件输出流
        FileWriter writer;
        //写出桌面上
        writer = new FileWriter("C:\\Users\\10\\Desktop\\aaaa.txt");
        for (int i = -1000; i < 2000; i++) { // i的值自己定义 往前推几天  向后推几天

            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, i);  // 往前推昨天的时间应该是负数  或向后推时间应该是正数
            Date d = cal.getTime();

            SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd"); //确定需要转换的格式

            String ZUOTIAN = sp.format(d);

            System.out.println(ZUOTIAN); //2026-03-10

            writer.write(ZUOTIAN + "\n"); //写出

        }
        writer.flush();
        writer.close();

    }

}

步骤2

   2.1 上传文件到Linux   (上传方法自己找)

   2.2  建表  转存到Hive中

create table timeFun(

times string  comment '时间'

)
row format delimited   fields terminated by '\n' 
location '/a/timeFun/' ;
-- 导入数据
load data local inpath '/a/aaaa.txt' into table timeFun;

 步骤3.

 建时间维度表

create table timeFun_wei_biao(

int_year_month_day         string  comment '20171223'                         ,
string_year_month_day      string  comment '2017-12-23'                       ,
zw_year_month_day          string  comment '2017年12月23日'                   ,
year_month                 string  comment '2017-12'                          ,
zw_year_month              string  comment '2017年12月'                       ,
year                       string  comment '2017'                             ,
zw_year                    string  comment '2017年'                           ,
weekday_cn                 string  comment '周几(中文)'                       ,
weekday_eg                 string  comment '周几(英文)'                       ,
week_id                    string  comment '第几周 数字 '                     ,
week_long_desc             string  comment '2017年第52周'                     ,
daynumber_of_week          string  comment '数字周几'                         ,
daynumber_of_year          string  comment '是本年的第几天 '


)
row format delimited   fields terminated by '\001' 
location '/a/timeFun_wei_biao/' ;

步骤4:

经过Hsql时间函数转换 得到各种各样的时间 导入数据到Hive的时间维表中

 insert overwrite table timeFun_wei_biao
 
select
regexp_replace(times,'-','') as int_year_month_day  ,                                         -- 20171223 
date(times) as  string_year_month_day               ,                                         -- 2017-12-23 
from_unixtime(unix_timestamp(times,'yyyy-MM-dd'),'yyyy年MM月dd日') as zw_year_month_day ,     -- 2017年12月23日
-- 获取年月                                                                                 
from_unixtime(unix_timestamp(times,'yyyy-MM-dd'),'yyyy-MM') as  year_month ,                  -- 2017-12
from_unixtime(unix_timestamp(times,'yyyy-MM-dd'),'yyyy年MM月') zw_year_month,                 -- 2017年12月
 -- 获取年份                                                                                
year(times) as year ,                                                                         -- 2017
from_unixtime(unix_timestamp(times,'yyyy-MM-dd'),'yyyy年') as zw_year ,                       -- 2017年
                                                                                            
-- 获取周几                                                                                 
case when pmod(datediff(times, '2012-01-01'), 7) = 0 then '星期日'                          
    when pmod(datediff(times, '2012-01-01'), 7) = 1 then '星期一'                           
    when pmod(datediff(times, '2012-01-01'), 7) = 2 then '星期二'                           
    when pmod(datediff(times, '2012-01-01'), 7) = 3 then '星期三'                           
    when pmod(datediff(times, '2012-01-01'), 7) = 4 then '星期四'                           
    when pmod(datediff(times, '2012-01-01'), 7) = 5 then '星期五'                           
    when pmod(datediff(times, '2012-01-01'), 7) = 6 then '星期六'                           
    else pmod(datediff(times, '2012-01-01'), 7) end as weekday_cn,                            -- 周几(中文) 
from_unixtime(unix_timestamp(times,'yyyy-MM-dd'),'EEEE') as weekday_eg,                       -- 周几(英文)
                                                                                            
from_unixtime(unix_timestamp(times,'yyyy-MM-dd'),'w') week_id,                                --51 
from_unixtime(unix_timestamp(times,'yyyy-MM-dd'),'yyyy年第w周') week_long_desc,               --2017年第52周
from_unixtime(unix_timestamp(times,'yyyy-MM-dd'),'u') as daynumber_of_week,                  --数字周几
from_unixtime(unix_timestamp(times,'yyyy-MM-dd'),'D') as daynumber_of_year                   -- 是本年的第几天 
 
from 
timeFun
 ;

步骤5:检查数据


0: jdbc:hive2://linux01:10000> select * from timeFun_wei_biao  a limit 20 ; 
 时间                                                                                                                                                           英文周几        数字第几周                           数字周几            该时间是本年的第几天
+-----------------------+--------------------------+---------------------    +---------------+------------------  +---------+------------ +--------------   -+---------------+------------+-------------------   +----------------------+----------------------+
| a.int_year_month_day  | a.string_year_month_day  | a.zw_year_month_day     | a.year_month  | a.zw_year_month    | a.year  | a.zw_year   | a.weekday_cn     | a.weekday_eg  | a.week_id  | a.week_long_desc     | a.daynumber_of_week  | a.daynumber_of_year  |
+-----------------------+--------------------------+---------------------    +---------------+------------------  +---------+------------ +--------------   -+---------------+------------+-------------------   +----------------------+----------------------+
| 20171223              | 2017-12-23               | 2017年12月23日          | 2017-12       | 2017年12月         | 2017    | 2017年      | 星期六           | Saturday      | 51         | 2017年第51周         | 6                    | 357                  |
| 20171224              | 2017-12-24               | 2017年12月24日          | 2017-12       | 2017年12月         | 2017    | 2017年      | 星期日           | Sunday        | 52         | 2017年第52周         | 7                    | 358                  |
| 20171225              | 2017-12-25               | 2017年12月25日          | 2017-12       | 2017年12月         | 2017    | 2017年      | 星期一           | Monday        | 52         | 2017年第52周         | 1                    | 359                  |
| 20171226              | 2017-12-26               | 2017年12月26日          | 2017-12       | 2017年12月         | 2017    | 2017年      | 星期二           | Tuesday       | 52         | 2017年第52周         | 2                    | 360                  |
| 20171227              | 2017-12-27               | 2017年12月27日          | 2017-12       | 2017年12月         | 2017    | 2017年      | 星期三           | Wednesday     | 52         | 2017年第52周         | 3                    | 361                  |
| 20171228              | 2017-12-28               | 2017年12月28日          | 2017-12       | 2017年12月         | 2017    | 2017年      | 星期四           | Thursday      | 52         | 2017年第52周         | 4                    | 362                  |
| 20171229              | 2017-12-29               | 2017年12月29日          | 2017-12       | 2017年12月         | 2017    | 2017年      | 星期五           | Friday        | 52         | 2017年第52周         | 5                    | 363                  |
| 20171230              | 2017-12-30               | 2017年12月30日          | 2017-12       | 2017年12月         | 2017    | 2017年      | 星期六           | Saturday      | 52         | 2017年第52周         | 6                    | 364                  |
| 20171231              | 2017-12-31               | 2017年12月31日          | 2017-12       | 2017年12月         | 2017    | 2017年      | 星期日           | Sunday        | 1          | 2017年第1周          | 7                    | 365                  |
| 20180101              | 2018-01-01               | 2018年01月01日          | 2018-01       | 2018年01月         | 2018    | 2018年      | 星期一           | Monday        | 1          | 2018年第1周          | 1                    | 1                    |
| 20180102              | 2018-01-02               | 2018年01月02日          | 2018-01       | 2018年01月         | 2018    | 2018年      | 星期二           | Tuesday       | 1          | 2018年第1周          | 2                    | 2                    |
| 20180103              | 2018-01-03               | 2018年01月03日          | 2018-01       | 2018年01月         | 2018    | 2018年      | 星期三           | Wednesday     | 1          | 2018年第1周          | 3                    | 3                    |
| 20180104              | 2018-01-04               | 2018年01月04日          | 2018-01       | 2018年01月         | 2018    | 2018年      | 星期四           | Thursday      | 1          | 2018年第1周          | 4                    | 4                    |
| 20180105              | 2018-01-05               | 2018年01月05日          | 2018-01       | 2018年01月         | 2018    | 2018年      | 星期五           | Friday        | 1          | 2018年第1周          | 5                    | 5                    |
| 20180106              | 2018-01-06               | 2018年01月06日          | 2018-01       | 2018年01月         | 2018    | 2018年      | 星期六           | Saturday      | 1          | 2018年第1周          | 6                    | 6                    |
| 20180107              | 2018-01-07               | 2018年01月07日          | 2018-01       | 2018年01月         | 2018    | 2018年      | 星期日           | Sunday        | 2          | 2018年第2周          | 7                    | 7                    |
| 20180108              | 2018-01-08               | 2018年01月08日          | 2018-01       | 2018年01月         | 2018    | 2018年      | 星期一           | Monday        | 2          | 2018年第2周          | 1                    | 8                    |
| 20180109              | 2018-01-09               | 2018年01月09日          | 2018-01       | 2018年01月         | 2018    | 2018年      | 星期二           | Tuesday       | 2          | 2018年第2周          | 2                    | 9                    |
| 20180110              | 2018-01-10               | 2018年01月10日          | 2018-01       | 2018年01月         | 2018    | 2018年      | 星期三           | Wednesday     | 2          | 2018年第2周          | 3                    | 10                   |
| 20180111              | 2018-01-11               | 2018年01月11日          | 2018-01       | 2018年01月         | 2018    | 2018年      | 星期四           | Thursday      | 2          | 2018年第2周          | 4                    | 11                   |
+-----------------------+--------------------------+----------------------+---------------+------------------+---------+------------+---------------+---------------+------------+-------------------+----------------------+----------------------+

步骤6:

Mysql建表:使用sqoop导出数据

CREATE TABLE `time_wei_biao` (
  `int_year_month_day` int(30) NOT NULL COMMENT '20171223',
  `string_year_month_day` varchar(20) DEFAULT NULL COMMENT '2017-12-23 ',
  `zw_year_month_day` varchar(20) DEFAULT NULL COMMENT ' 2017年12月23日',
  `year_month` varchar(20) DEFAULT NULL COMMENT ' 2017-12',
  `zw_year_month` varchar(20) DEFAULT NULL COMMENT '2017年12月',
  `year` varchar(20) DEFAULT NULL COMMENT '2017 ',
  `zw_year` varchar(20) DEFAULT NULL COMMENT ' 2017年',
  `weekday_cn` varchar(20) DEFAULT NULL COMMENT '周几(中文)',
  `weekday_eg` varchar(20) DEFAULT NULL COMMENT '周几(英文)',
  `week_id` varchar(20) DEFAULT NULL COMMENT '第几周 数字',
  `week_long_desc` varchar(20) DEFAULT NULL COMMENT ' 2017年第51周 ',
  `daynumber_of_week` varchar(20) DEFAULT NULL COMMENT '数字周几',
  `daynumber_of_year` varchar(20) DEFAULT NULL COMMENT '是本年的第几天',
  PRIMARY KEY (`int_year_month_day`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='时间维表\r\n';

使用sqoop导出数据

sqoop export \
--connect jdbc:mysql://MYSQL连接:端口号/数据库名?characterEncoding=UTF-8 \
--username  账号--password 密码 \
--table time_wei_biao \
--export-dir /a/timeFun_wei_biao/ \
--input-fields-terminated-by '\001' \
--input-null-string '\\N' \
--input-null-non-string '\\N' \
--columns " int_year_month_day , string_year_month_day , zw_year_month_day , year_month , zw_year_month , year , zw_year , weekday_cn , weekday_eg , week_id , week_long_desc , daynumber_of_week , daynumber_of_year  " \
--update-mode allowinsert \
--update-key  "int_year_month_day  " \
-m 1

步骤7

检查Mysql数据

没问题 , ok 

猜你喜欢

转载自blog.csdn.net/weixin_45592182/article/details/108669145