Hive時間整理

2021-10-06 13:28:23 字數 2369 閱讀 5286

from_unixtime & unix_timestam

from_unixtime:時間戳轉日期函式 返回值: string

unix_timestamp:日期轉時間戳函式 返回值:   bigint

時間戳一般是10位數的,13位數的為毫秒級的,毫秒級時間戳直接使用from_unixtime轉換需要/1000 

時間格式轉換:

select to_date(from_unixtime(unix_timestamp('20200527','yyyymmdd'), 'yyyy-mm-dd'));

select from_unixtime(unix_timestamp('20200527','yyyymmdd'),'yyyy-mm-dd');

select from_unixtime(unix_timestamp('2020-05-27','yyyy-mm-dd'),'yyyymmdd');

可使用最原始的字串拼接方式:

select concat(substr('20200527',1,4),'-',substr('20200527',5,2),'-',substr('20200527',7,2));
正則替換方式:

select regexp_replace(regexp_extract('2020-05-27','(\\d-\\d-\\d)',1), '-', '');
獲取當前日期

select current_date;

獲取當前時間

select current_timestamp;

select from_unixtime(unix_timestamp());

獲取當前時間戳(10位)

select unix_timestamp();

求兩個日期時間差

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

date1為結束日期,date2為開始日期,返回結束日期減去開始日期的天數

select datediff('2020-05-27','2020-03-31') as datediff;

日期加減:

date_add(start_date, num_days) - returns the date that is num_days after start_date.

date_sub(start_date, num_days) - returns the date that is num_days before start_date.

返回日期相加減後的日期

select date_add('2020-05-27',3) as dateadd;

select date_sub('2020-05-27',7) as datesub;

next_day(start_date, day_of_week) - returns the first date which is later than start_date and named as indicated.

返回指定日期的下乙個週幾

select next_day('2020-05-27', 'tu');

2020-06-02

時間簡寫彙總

monday

monmo

周一tuesday

tuetu

周二wednesday

wedwe

週三thursday

thuth

周四friday

frifr

周五saturday

satsa

週六sunday

sunsu週日

一 月 

january 

jan二 月 

february 

feb三 月 

march 

mar四 月 

april 

apr五 月 

may 

may六 月 

june 

jun七 月 

july 

jul八 月 

august 

aug九 月 

september 

sep十 月 

october 

oct十一月

november 

nov十二月

december 

dec

Hive優化整理

寫本文的目的是為了從本文開始,讓自己養成寫博文的習慣,也將知識一點點的沉澱下來,自己回頭看的時候方便,能為其他人提供一些幫助更好。同時也會整理一些面試題。1 兩表join,條件寫在的on後面和where後面什麼區別?1.left join,不管on後面跟什麼條件,左表的資料都會列出來,右表中關聯不上...

Hive 時間函式

to date 日期時間轉日期函式select to date 2015 04 02 13 34 12 輸出 2015 04 02from unixtime 轉化unix時間戳到當前時區的時間格式select from unixtime 1323308943,yyyymmdd 輸出 20111208...

hive時間函式

語法 from unixtime bigint unixtime stringformat 返回值 string 說明 轉化unix時間戳 從1970 01 0100 00 00 utc到指定時間的秒數 到當前時區的時間格式 舉例 select from unixtime 1323308943,yy...