Hive時間操作函式 獲取小時差 天數差

2021-10-08 23:28:59 字數 3154 閱讀 8464

1.首先hive獲取當前時間的函式與sql 不一樣

sql是:now();

(1) hive有乙個獲得當前時區的unix時間戳:unix_timestamp

語法: unix_timestamp()

返回值: bigint

說明: 獲得當前時區的unix時間戳

舉例:hive> select unix_timestamp() from test;

1453261615

語法: from_unixtime(bigint unixtime[, string format])

舉例:hive > select from_unixtime(unix_timestamp(),『yyyy-mm-dd hh:mm:ss』) from test;

2017-11-16 09:43:35

(3) 獲取資料建立時間與當前時間的時間差:datediff

desc function extended datediff;

okdatediff(date1, date2) - returns the number of days between date1 and date2

date1 and date2 are strings in the format 『yyyy-mm-dd hh:mm:ss』 or 『yyyy-mm-dd』. the time parts are ignored.if date1 is earlier than date2, the result is negative.

example:

select datediff(『2009-30-07』, 『2009-31-07』) from src limit 1;

datediff的用法:datediff(『日期1』,』日期2』),其中日期是有格式的,目前支援以下兩種格式:

『yyyy-mm-dd hh:mm:ss』

『yyyy-mm-dd』

舉例:獲取表建立時間與當前時間的之間差(day)

hive > select create_time ,datediff(from_unixtime(unix_timestamp(),』yyyy-mm-dd hh:mm:ss』), create_time) from test;

2017-11-10 09:43:35 6

結果就是:當前時間是2017-11-16 與create_time的11-10之間差了6天,輸出6;

(4)那我們要獲得小時差呢?

(4.1) 有一系列獲取日期轉年月日小時周的方法:

a. 日期時間轉日期函式: to_date語法: to_date(string timestamp) 返回:string

hive> select to_date(』2011-12-08 10:03:01′) from dual;

2011-12-08

b. 日期轉年函式: year語法: year(string date)

hive> select year(』2011-12-08 10:03:01′) from dual;

日期轉月函式: month語法: month (string date)

hive> select month(』2011-08-08′) from dual;

d.日期轉天函式: day語法: day (string date)

hive> select day(』2011-12-08 10:03:01′) from dual;

日期轉小時函式: hour語法: hour (string date)

hive> select hour(』2011-12-08 10:03:01′) from dual;

10f.日期轉分鐘函式: minute語法: minute (string date)

hive> select minute(』2011-12-08 10:03:01′) from dual;

日期轉秒函式: second語法: second (string date)

hive> select second(』2011-12-08 10:03:01′) from dual;

h.日期轉周函式: weekofyear語法: weekofyear (string date)

返回值: int

說明: 返回日期在當前的週數。

舉例:hive> select weekofyear(』2011-12-08 10:03:01′) from dual;

日期比較函式: datediff語法: datediff(string enddate, string startdate)

返回值: int

說明: 返回結束日期減去開始日期的天數。

舉例:hive> select datediff(』2012-12-08′,』2012-05-09′) from dual;

213j.日期增加函式: date_add語法: date_add(string startdate, int days)

返回值: string

說明: 返回開始日期startdate增加days天後的日期。

舉例:hive> select date_add(』2012-12-08′,10) from dual;

2012-12-18

日期減少函式: date_sub語法: date_sub (string startdate, int days)

返回值: string

說明: 返回開始日期startdate減少days天後的日期。

舉例:hive> select date_sub(』2012-12-08′,10) from dual;

2012-11-28

(4.2) 所以我們利用其中的hour和datediff來獲取create_time與當前時間的小時差:

hive> select create_time,(hour(from_unixtime(unix_timestamp(),『yyyy-mm-dd hh:mm:ss』))-hour(create_time)+(datediff(from_unixtime(unix_timestamp(),『yyyy-mm-dd hh:mm:ss』), create_time))*24) as hour_dvalue from test;

2017-11-10 09:43:35 145

hive 時間操作函式

日期函式unix時間戳轉日期函式 from unixtime語法 from unixtime bigint unixtime string format 返回值 string 說明 轉化unix時間戳 從1970 01 01 00 00 00 utc到指定時間的秒數 到當前時區的時間格式 舉例 hi...

hive時間操作函式

標籤 hive 函式linux 2016 12 21 16 07 49人閱讀收藏 舉報 hive 日期函式unix時間戳轉日期函式 from unixtime語法 from unixtime bigint unixtime string format 返回值 string 說明 轉化unix時間戳 ...

hive 時間操作函式

日期函式unix時間戳轉日期函式 from unixtime語法 from unixtime bigint unixtime string format 返回值 string 說明 轉化unix時間戳 從1970 01 01 00 00 00 utc到指定時間的秒數 到當前時區的時間格式 舉例 hi...