首页天道酬勤Spark数据清洗【时间数据处理】

Spark数据清洗【时间数据处理】

admin 03-14 11:17 90次浏览

(1) 时间类型 TIMESTAMP

登录后复制 

-- timestamp

-- 年 月 日 时 分 秒 时区


-- 1. 获取当前时间 (年-月-日 时:分:秒.毫秒)

> SELECT current_timestamp();

  2022-04-22 16:45:23.526


-- 2. 指定时刻毫秒级时间戳 --[转]--> 毫秒级时间 (年-月-日 时:分:秒.毫秒)

> select timestamp_millis(1650621284511);

  2022-04-22 17:54:44.511

-- return timestamp

-- Since: 3.1.0


-- 3. 指定时间戳 --[转]--> 格式化时间 (年-月-日 时:分:秒)

> select from_unixtime(1650622091047/1000, 'yyyy-MM-dd HH:mm:ss');

  2022-04-22 18:08:11

-- return string


> select from_unixtime(1650622091047/1000, 'yyyyMMdd');

  20220422

(2) 日期类型 DTAE

登录后复制 

-- date

-- 年-月-日


-- 1. 获取当前日期 (年-月-日)

> select current_date();

  2022-04-22


-- 2. 日期格式化 

> SELECT date_format(current_date(), 'yyyyMMdd');

  20220422

-- return string


-- 3. 非标准化日期时间提取日期

> SELECT to_date('20161231 12:01:12', 'yyyyMMdd HH:mm:ss');

  2016-12-31

-- return date


> SELECT to_date('2009-07-30 04:17:52');

  2009-07-30

  

> SELECT to_date('2016-12-31', 'yyyy-MM-dd');

  2016-12-31

(3) 时间戳 BIGINT

登录后复制 

-- bigint

-- 秒级/毫秒级 时间戳 (13位/16位)


-- 1. 获取当前(年-月-日 时:分:秒)时间戳

> SELECT unix_timestamp(); 

  1476884637

Since: 1.5.0


-- 2. 获取当前(年-月-日 时:分:秒.毫秒)时间戳

> SELECT unix_timestamp()*1000;            -- 毫秒不精确

  1476884637000                                           


> select unix_millis(current_timestamp()); -- 毫秒精确

  1650622091047  2022-04-22 18:08:11.047

-- return bigint

-- Since: 3.1.0


-- 3. 获取指定时间秒级时间戳

> SELECT unix_timestamp('2016-04-08', 'yyyy-MM-dd');

  1460041200



-- 4.1 获取指定时间毫秒级时间戳

> select unix_millis(timestamp('2022-04-22 17:43:01.123'));

  1650620581123

  

-- 4.2 获取指定时间毫秒级时间戳

date_format(to_timestamp(concat(from_unixtime(1559461463324/1000,'yyyy-MM-dd HH:mm:ss.'), 1559461463324%1000)),'yyyy-MM-dd HH:mm:ss.SSS') as c3


koa初识 一分快3高手判断技巧_一分快三app推荐软件