hive中的時間函式總結

2021-10-01 23:23:07 字數 1360 閱讀 1500

–日期轉時間戳:從1970-01-01 00:00:00 utc到指定時間的秒數

select unix_timestamp(); --獲得當前時區的unix時間戳

select unix_timestamp(『2017-09-15 14:23:00』);

select unix_timestamp(『2017-09-15 14:23:00』,『yyyy-mm-dd hh:mm:ss』);

select unix_timestamp(『20170915 14:23:00』,『yyyymmdd hh:mm:ss』);

–時間戳轉日期

select from_unixtime(1505456567);

select from_unixtime(1505456567,『yyyymmdd』);

select from_unixtime(1505456567,『yyyy-mm-dd hh:mm:ss』);

select from_unixtime(unix_timestamp(),『yyyy-mm-dd hh:mm:ss』); --獲取系統當前時間

hive> select current_date from dual

2017-09-15

hive> select to_date(『2017-09-15 11:12:00』) from dual;

2017-09-15

with dtime as(select from_unixtime(unix_timestamp(),『yyyy-mm-dd hh:mm:ss』) as dt)

select year(dt),month(dt),day(dt),hour(dt),minute(dt),second(dt),weekofyear(dt)

from dtime

hive> select datediff(『2017-09-15』,『2017-09-01』) from dual;

14hive> select date_add(『2017-09-15』,1) from dual;

2017-09-16

hive> select date_sub(『2017-09-15』,1) from dual;

2017-09-14

查詢當前系統時間(包括毫秒數): current_timestamp;

查詢當月第幾天: dayofmonth(current_date);

月末: last_day(current_date)

當月第1天: date_sub(current_date,dayofmonth(current_date)-1)

下個月第1天: add_months(date_sub(current_date,dayofmonth(current_date)-1),1)

hive 時間函式 總結

做資料探勘的,離不開使用各種時間函式。為了避免遺忘,以及後續各種抓瞎到處亂找,特意總結了hive中大部分常用的時間函式,方便自己也方便他們。返回當前時區的unix時間戳 返回型別 bigint hive tmp select unix timestamp from hive sum limit 1 ...

hive 中時間函式

to date 日期時間轉日期函式 select to date 2015 04 02 13 34 12 輸出 2015 04 02 當前日期和時間 select current timestamp 2018 04 28 11 46 03.136 獲取當前日期,當前是 2018 04 28 sele...

hive中的時間處理函式

返回值 string 說明 轉化unix時間戳 從1970 01 01 00 00 00 utc到指定時間的秒數 到當前時區的時間格式 舉例 hive select from unixtime 1323308943,yyyymmdd from dual 20111208 獲取當前unix時間戳函式 ...