Hive笔记2-函数

hive 函数

参考链接:http://lxw1234.com/archives/2015/06/251.htm

类型转换函数

语法: cast(expr as )
返回值:
说明: 将 expr 转换成
举例:

hive> select cast('1' as DOUBLE) from lxw1234;
OK
1.0

日期函数

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

返回值: string

说明: 转化 UNIX 时间戳(从 1970-01-01 00:00:00 UTC 到指定时间的秒数)到当前时区
的时间格式

举例:

hive> select from_unixtime(1323308943,'yyyyMMdd') from lxw1234;
20111208

语法: unix_timestamp(string date)

返回值: bigint

说明: 转换格式为"yyyy-MM-dd HH:mm:ss"的日期到 UNIX 时间戳。如果转化失败,则返
回 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
说明: 返回日期在当前的周数。
举例:

hive> select weekofyear('2011-12-08 10:03:01') from lxw1234;
49
hive> select datediff('2012-12-08','2012-05-09') from lxw1234; 
213 //返回相差天数
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

条件函数

语法: 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

字符串函数

hive> select concat(‘abc’,'def’,'gh’) from lxw1234;
abcdefgh
hive> select concat_ws(',','abc','def','gh') from lxw1234; 
abc,def,gh //返回输入字符串连接后的结果,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 //将数值 x 的小数位格式化成 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 中首次出现的位置

语法: str_to_map(text[, delimiter1, delimiter2])返回值: map说明:将字符串按照给定的分隔符转换成 map 结构.举例:

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 正则表达式的规则拆分,返回 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 指定的内容。如果输入的 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

汇总统计函数

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

表格生成函数

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

你可能感兴趣的:(Hive笔记2-函数)