hive常用函式三

2022-04-11 11:02:30 字數 3672 閱讀 8440

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

返回值: string

說明: 轉化unix時間戳(從1970-01-01 00:00:00 utc到指定時間的秒數)到當前時區的時間格式

舉例:hive> select from_unixtime(1323308943,'yyyymmdd') from lxw_dual;

語法: unix_timestamp()

返回值: bigint

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

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

語法: unix_timestamp(string date)

返回值: bigint

說明: 轉換格式為"yyyy-mm-dd hh:mm:ss"的日期到unix時間戳。如果轉化失敗,則返回0。

舉例:hive> select unix_timestamp('2011-12-07 13:01:03') from lxw_dual;

語法: unix_timestamp(string date, string pattern)

返回值: bigint

說明: 轉換pattern格式的日期到unix時間戳。如果轉化失敗,則返回0。

舉例:hive> select unix_timestamp('20111207 13:01:03','yyyymmdd hh:mm:ss') from lxw_dual;

語法: to_date(string timestamp)

返回值: string

說明: 返回日期時間欄位中的日期部分。

舉例:hive> select to_date('2011-12-08 10:03:01') from lxw_dual;

2011-12-08

語法: year(string date)

返回值: int

說明: 返回日期中的年。

舉例:hive> select year('2011-12-08 10:03:01') from lxw_dual;

hive> select year('2012-12-08') from lxw_dual;

語法: month (string date)

返回值: int

說明: 返回日期中的月份。

舉例:hive> select month('2011-12-08 10:03:01') from lxw_dual;

hive> select month('2011-08-08') from lxw_dual;

語法: day (string date)

返回值: int

說明: 返回日期中的天。

舉例:hive> select day('2011-12-08 10:03:01') from lxw_dual;

hive> select day('2011-12-24') from lxw_dual;

語法: hour (string date)

返回值: int

說明: 返回日期中的小時。

舉例:hive> select hour('2011-12-08 10:03:01') from lxw_dual;

語法: minute (string date)

返回值: int

說明: 返回日期中的分鐘。

舉例:hive> select minute('2011-12-08 10:03:01') from lxw_dual;

語法: second (string date)

返回值: int

說明: 返回日期中的秒。

舉例:hive> select second('2011-12-08 10:03:01') from lxw_dual;

語法: weekofyear (string date)

返回值: int

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

舉例:hive> select weekofyear('2011-12-08 10:03:01') from lxw_dual;

語法: datediff(string enddate, string startdate)

返回值: int

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

舉例:hive> select datediff('2012-12-08','2012-05-09') from lxw_dual;

語法: date_add(string startdate, int days)

返回值: string

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

舉例:hive> select date_add('2012-12-08',10) from lxw_dual;

2012-12-18

語法: date_sub (string startdate, int days)

返回值: string

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

舉例:hive> select date_sub('2012-12-08',10) from lxw_dual;

2012-11-28

語法: if(boolean testcondition, t valuetrue, t valuefalseornull)

返回值: t

說明:  當條件testcondition為true時,返回valuetrue;否則返回valuefalseornull

舉例:hive> select if(1=2,100,200) from lxw_dual;

hive> select if(1=1,100,200) from lxw_dual;

語法: coalesce(t v1, t v2, …)

返回值: t

說明:  返回引數中的第乙個非空值;如果所有值都為null,那麼返回null

舉例:hive> select coalesce(null,'100','50′) from lxw_dual;

語法: case a when b then c [when d then e]* [else f] end

返回值: t

說明:如果a等於b,那麼返回c;如果a等於d,那麼返回e;否則返回f

舉例:hive> select case 100 when 50 then 'tom' when 100 then 'mary' else 'tim' end from lxw_dual;

mary

hive> select case 200 when 50 then 'tom' when 100 then 'mary' else 'tim' end from lxw_dual;

tim語法: case when a then b [when c then d]* [else e] end

返回值: t

說明:如果a為true,則返回b;如果c為true,則返回d;否則返回e

舉例:hive> select case when 1=2 then 'tom' when 2=2 then 'mary' else 'tim' end from lxw_dual;

mary

hive> select case when 1=1 then 'tom' when 2=2 then 'mary' else 'tim' end from lxw_dual;

tom

Hive常用函式

if判斷 select if 1 1,yes no 返回yes 語法 case expression when condition1 then result1 when condition2 then result2 else result end 例子 case a when 1 then one...

hive常用函式

hive常用函式 1 檢視函式用法 desc function 函式名 desc function extended 函式名 2 獲取array陣列長度 size函式 select size collect list field from table select size split hello ...

Hive常用函式

常用日期函式 unix timestamp 返回當前或指定時間的時間戳 from unixtime 將時間戳轉為日期格式 current date 當前日期 current timestamp 當前的日期加時間 to date 抽取日期部分 year 獲取年 month 獲取月 day 獲取日 ho...