hive 函數(shù)
參考鏈接:http://lxw1234.com/archives/2015/06/251.htm
類型轉(zhuǎn)換函數(shù)
語法: cast(expr as <type>)
返回值: <type>
說明: 將 expr 轉(zhuǎn)換成<type>
舉例:
hive> select cast('1' as DOUBLE) from lxw1234;
OK
1.0
日期函數(shù)
語法: from_unixtime(bigint unixtime[, string format])
返回值: string
說明: 轉(zhuǎn)化 UNIX 時間戳(從 1970-01-01 00:00:00 UTC 到指定時間的秒數(shù))到當前時區(qū)
的時間格式
舉例:
hive> select from_unixtime(1323308943,'yyyyMMdd') from lxw1234;
20111208
語法: unix_timestamp(string date)
返回值: bigint
說明: 轉(zhuǎn)換格式為"yyyy-MM-dd HH:mm:ss"的日期到 UNIX 時間戳它匕。如果轉(zhuǎn)化失敗脓恕,則返
回 0慨削。
舉例:
hive> select unix_timestamp() from lxw1234;
1323309615 //當前時刻
hive> select unix_timestamp('2011-12-07 13:01:03') from lxw1234;
1323234063
hive> select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss') from lxw1234; 1323234063
語法: to_date(string timestamp)
返回值: string
說明: 返回日期時間字段中的日期部分旺上。
舉例
hive> select to_date('2011-12-08 10:03:01') from lxw1234;
2011-12-08
hive> select year('2011-12-08 10:03:01') from lxw1234;
2011
hive> select month('2011-08-08') from lxw1234;
8
hive> select day('2011-12-08 10:03:01') from lxw1234;
8
hive> select hour('2011-12-08 10:03:01') from lxw1234;
10
語法: weekofyear (string date)
返回值: int
說明: 返回日期在當前的周數(shù)。
舉例:
hive> select weekofyear('2011-12-08 10:03:01') from lxw1234;
49
hive> select datediff('2012-12-08','2012-05-09') from lxw1234;
213 //返回相差天數(shù)
hive> select date_add('2012-12-08',10) from lxw1234; 2012-12-18
hive> select date_sub('2012-12-08',10) from lxw1234; 2012-11-28
條件函數(shù)
語法: if(boolean testCondition, T valueTrue, T valueFalseOrNull)
返回值: T
說明: 當條件 testCondition 為 TRUE 時来颤,返回 valueTrue;否則返回 valueFalseOrNull
舉例:
hive> select if(1=2,100,200) from lxw1234;
200
語法:case
hive> Select case 100 when 50 then 'tom' when 100 then 'mary' else 'tim' end from lxw1234;
mary
hive> select case when 1=2 then 'tom' when 2=2 then 'mary' else 'tim' end from lxw1234;
mary
字符串函數(shù)
hive> select concat(‘a(chǎn)bc’,'def’,'gh’) from lxw1234;
abcdefgh
hive> select concat_ws(',','abc','def','gh') from lxw1234;
abc,def,gh //返回輸入字符串連接后的結(jié)果冠绢,SEP 表示各個字符串間的分隔符
hive> select concat_ws('|',array('a','b','c')) from lxw1234;
OK
a|b|c
hive> select format_number(5.23456,3) from lxw1234; OK
5.235 //將數(shù)值 x 的小數(shù)位格式化成 d 位,四舍五入
hive> select substr('abcde',3) from lxw1234; cde
hive> select substring('abcde',3) from lxw1234; cde
hive> select substr('abcde',-1) from lxw1234; (和 ORACLE 相同) e
hive> select substr('abcde',3,2) from lxw1234;
cd //返回字符串 A 從 start 位置開始德崭,長度為 len 的字符串
hive> select substring('abcde',3,2) from lxw1234; cd
hive>select substring('abcde',-2,2) from lxw1234; de
hive> select instr('abcdf','df') from lxw1234; OK
4 //返回字符串 substr 在 str 中首次出現(xiàn)的位置
語法: str_to_map(text[, delimiter1, delimiter2])返回值: map<string,string>說明:將字符串按照給定的分隔符轉(zhuǎn)換成 map 結(jié)構(gòu).舉例:
hive> select str_to_map('k1:v1,k2:v2') from lxw1234;
OK
{"k2":"v2","k1":"v1"}
hive> select str_to_map('k1=v1,k2=v2',',','=') from lxw1234;
OK
{"k2":"v2","k1":"v1"}
正則表達式
語法: regexp_extract(string subject, string pattern, int index)
返回值: string
說明:將字符串 subject 按照 pattern 正則表達式的規(guī)則拆分斥黑,返回 index 指定的字符。
舉例:
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 1) from lxw1234;
the
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 2) from lxw1234;
bar
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 0) from lxw1234;
foothebar
語法: get_json_object(string json_string, string path)
返回值: string
說明:解析 json 的字符串 json_string,返回 path 指定的內(nèi)容眉厨。如果輸入的 json 字符串無
效锌奴,那么返回 NULL。
舉例:
hive> select get_json_object('{"store":
> {"fruit":\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],
> "bicycle":{"price":19.95,"color":"red"}
> },
> "email":"amy@only_for_json_udf_test.net",
> "owner":"amy"
>}
> ','$.owner') from lxw1234; amy
匯總統(tǒng)計函數(shù)
hive> select count(*) from lxw1234;
20
hive> select sum(t) from lxw1234;
100
hive> select sum(distinct t) from lxw1234;
70
hive> select avg(t) from lxw1234;
50
hive> select avg (distinct t) from lxw1234;
30
hive> select min(t) from lxw1234;
20
hive> select max(t) from lxw1234;
120
表格生成函數(shù)
explode
hive> select explode(array(1,2,3)) from lxw1234; OK
1
2
3
hive> select explode(map('k1','v1','k2','v2')) from lxw1234; OK
k2 v2
k1 v1