hive的常用函式

2021-08-18 06:45:40 字數 2975 閱讀 7633

型別轉換函式:

cast('1' as bigint)

日期函式:

返回值型別:

string

from_unixtime(bigint unixtime[, string format]) 例:from_unixtime(0)="1970-01-01 00:00:00"

bigint

unix_timestamp() 獲取當前的時間戳

bigint

unix_timestamp(string date) 獲得date表示的時間戳 

string

to_date(string timestamp) 返回時間中的年月日,例如to_date("1970-01-01 00:00:00") = "1970-01-01" 

intyear(string date) 返回指定時間的年份,範圍在1000到9999,或為」零」日期的0。

intmonth(string date) 返回指定時間的月份,範圍為1至12月,或0乙個月的一部分,如』0000-00-00′或』2008-00-00′的日期。

intdate_add(string startdate, int days) 給定時間,在此基礎上加上指定的時間段

intdate_sub(string startdate, int days)  

給定時間,在此基礎上減去指定的時間段

sting       from_unixtime(unix_timestamp(),'yyyy-mm-dd hh:mm:ss')

int         cast(from_unixtime(unix_timestamp(),'yyyy-mm-dd hh:mm:ss') as int)

from_unixtime(unix_timestamp('20160902000000','yyyymmddhhmmss'),'yyyy-mm-dd hh:mm:ss')

條件函式:

if(boolean condition,value1,value2)

判斷是否滿足條件,如果滿足返回第乙個值,如果不滿足則返回第二個值

例:if(vu.company is null,"",trim(vu.company)) as company 

case a when b then c     當a=b時,返回c;當a=d時,返回e,否則返回f

when d then e

else f end

case when a then b       當值為a時返回b,當值為c時返回d。否則返回e

c then d

else e end

字元函式:

返回值型別:

string   regexp_replace('2016-03-04','-','') ==> 20160304

int length(string a)   返回字串的長度

string   reverse(string a)  返回倒序字串

string   concat(string a,string b)  連線多個字串,合併為乙個字串,可以接受任意數量的輸入字串

string   concat_ws(string sep,string a,string b)  鏈結多個字串,字串之間以指定的分隔符分開

string   trim(string a) 刪除字串兩端的空格,字元之間的空格保留

string  

regexp_replace(string a, string b, string c)  字串a中的b字元被c字元替代

string    

parse_url(string urlstring, string parttoextract [, string keytoextract])   返回url指定的部分。parse_url(『 『host』) 返回:』facebook.com』

string     get_json_object(string json_string, string path)  解析json的字串json_string,返回path指定的內容。如果輸入的json字串無效,那麼返回null

說明:json_string:json資料(json資料的欄位名稱),path:要獲取的json資料的key。 得出要從json資料中胡獲得資料

---------------------------------------------

拼接列:concat , concat_ws

橫向拼接:例如兩張小表查出資料塞到一張寬表中

縱向拼接:union all

行轉列:concat_ws(',',collect_list(bank))

1.建立一張hive表

create table t_person(

id int,

name string,

likes  array,

address map

)partitioned by (*** int)

row format delimited

fields  terminated  by  ','

collection  items  terminated by '_'

map  keys  terminated by ':'

stored as textfile;

2.找到需要分析文字檔案資料person.txt

1,zs,sing_play,city:beijing_street:xisanqi

2,ls,play_sing,city:shanghai_street:xierqi3.shiyong 

3.使用命令,將本地文字檔案中的資料倒入hive表

load data local inpath '/root/person.txt' into table t_person  partition (***=0);

判斷語句:

if(not lower(lmlc.phone) like '%null%' ,lmlc.sys_id, ms.sys_id) as sys_id

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...