Hive基础函数

一、hive函数

1、关系函数

2、日期函数

3、条件函数

4、字符串函数

5、统计函数

二、hiveQL

1、DDL

2、DML

三、其它

1、in()函数

2、lateral view 函数

3、row_number() 和rank()

4、grouping sets()函数

一、hive函数

1、关系函数

等值比较: =

语法:A=B。如果表达式A与表达式B相等,则为TRUE;否则为FALSE

不等值比较: <>

语法: A <> B。如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A与表达式B不相等,则为TRUE;否则为FALSE

小于比较: <

语法: A < B。 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A小于表达式B,则为TRUE;否则为FALSE

小于等于比较: <=

语法: A <= B。如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A小于或者等于表达式B,则为TRUE;否则为FALSE

大于等于比较: >=

语法: A >= B。如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A大于或者等于表达式B,则为TRUE;否则为FALSE

空值判断: IS NULL

语法: A IS NULL。如果表达式A的值为NULL,则为TRUE;否则为FALSE

非空判断: IS NOT NULL

语法: A IS NOT NULL。 如果表达式A的值为NULL,则为FALSE;否则为TRUE

LIKE比较: LIKE

语法: A[NOT] LIKE B。如果字符串A或者字符串B为NULL,则返回NULL;如果字符串A符合表达式B的正则语法,则为TRUE;否则为FALSE。B中字符”_”表示任意单个字符,而字符”%”表示任意数量的字符。

举例:

select * from dw.topic_order where partition_pay_date = ‘2016-04-22’ and client_type like ‘ip%’ ##能够匹配以ip开头的所有字符串

注意:对特殊字符进行转译时,注意要使用两个反斜杠\。

JAVA的LIKE/REGEXP操作: RLIKE/REGEXP

语法: A RLIKE/REGEXP B。如果字符串A或者字符串B为NULL,则返回NULL;如果字符串A符合JAVA正则表达式B的正则语法,则为TRUE;否则为FALSE

举例:select * from dw.topic_order where partition_pay_date = ‘2016-04-22’ and client_type rlike/regexp ‘^android.*’

注意:通配符‘%’在rlike/regexp函数中,只能匹配一个’%'字符,’‘也只能匹配一个’‘字符。例如:

select * from dw.topic_order where partition_pay_date = ‘2016-04-22’ and client_type regexp ‘ip%’ ##只能匹配ip%这个字符串

2、日期函数

UNIX时间戳转日期函数: from_unixtime

语法: from_unixtime(bigint unixtime[, string format])。转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式

举例:select from_unixtime(1323308943,‘yyyyMMdd’) from dual; ##返回值为20111208

获取当前UNIX时间戳函数: unix_timestamp

语法: unix_timestamp()。获得当前时区的UNIX时间戳

举例:select unix_timestamp() from dual; ##返回值为1323309615

日期转UNIX时间戳函数: unix_timestamp

语法: unix_timestamp(string date)。转换格式为"yyyy-MM-dd HH:mm:ss"的日期到UNIX时间戳。如果转化失败,则返回0。

举例:select unix_timestamp(‘2011-12-07 13:01:03’) from dual; ##返回值为1323234063

指定格式日期转UNIX时间戳函数: unix_timestamp

语法: unix_timestamp(string date, string pattern)。转换pattern格式的日期到UNIX时间戳。如果转化失败,则返回0。

举例:select unix_timestamp(‘20111207 13:01:03’,‘yyyyMMdd HH:mm:ss’) from dual; ##返回值为1323234063

日期时间转日期函数: to_date

语法: to_date(string timestamp)。返回日期时间字段中的日期部分。

举例:select to_date(‘2011-12-08 10:03:01’) from dual; ##返回值为2011-12-08

日期转年函数: year

语法: year(string date)。 返回日期中的年。

日期转月函数: month

语法: month (string date)。返回日期中的月份。

日期转天函数: day

语法: day (string date)。返回日期中的天。

日期转小时函数: hour

语法: hour (string date)。 返回日期中的小时。

日期转分钟函数: minute

语法: minute (string date)。返回日期中的分钟。

举例:select minute(‘2011-12-08 10:03:01’) from dual; ##返回值为3

日期转秒函数: second

语法: second (string date)。 返回日期中的秒。

举例:select second(‘2011-12-08 10:03:01’) from dual; ##返回值为1

日期转周函数: weekofyear

语法: weekofyear (string date)。返回日期在当前的周数。

举例:select weekofyear(‘2011-12-08 10:03:01’) from dual; ##返回值为49

日期比较函数: datediff

语法: datediff(string enddate, string startdate)。返回结束日期减去开始日期的天数。

举例: select datediff(‘2012-12-08’,‘2012-05-09’) from dual; ##返回值为213

日期增加函数: date_add

语法: date_add(string startdate, int days)。 返回开始日期startdate增加days天后的日期。

举例:select date_add(‘2012-12-08’,10) from dual; ##返回值为2012-12-18

日期减少函数: date_sub

语法: date_sub (string startdate, int days)。 返回开始日期startdate减少days天后的日期。

举例:select date_sub(‘2012-12-08’,10) from dual; ##返回值为2012-11-28

注:几个日期函数在MySQL和Hive里的区别

Hive: SELECT FROM_UNIXTIME( 1249488000, ‘%Y%m%d’) 结果为 %2009%0%6
Hive: SELECT FROM_UNIXTIME( 1249488000, ‘yyyy-MM-dd’) 结果为 2009-08-06
MySQL:SELECT FROM_UNIXTIME( 1249488000, ‘%Y%m%d’) 结果为 20090806
MySQL:SELECT FROM_UNIXTIME( 1249488000, ‘%Y-%m-%d’) 结果为 2009-08-06

3、条件函数

If 函数: if

语法: if(boolean testCondition, T valueTrue, T valueFalseOrNull)

说明: 当条件testCondition为TRUE时,返回valueTrue;否则返回valueFalseOrNull

举例:select if(app_name = ‘group’,object_id,null) as deal_id from dw.topic_order where partition_pay_date = ‘2016-04-22’

非空查找函数: COALESCE

语法: COALESCE(T v1, T v2, …)

说明: 返回参数中的第一个非空值;如果所有值都为NULL,那么返回NULL

举例:select coalesce(uuid,’’) as uuid from dw.topic_order where partition_pay_date = ‘2016-04-22’

条件判断函数:CASE

语法 : CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END

说明:如果 a 等于 b ,那么返回 c ;如果 a 等于 d ,那么返回 e ;否则返回 f

举例:

select object_id,user_id,uuid, case when client_type like ‘ip%’ then ‘ios’ when client_type like ‘andr%’ then ‘android’ else ‘other’ end as utm_medium from dw.topic_order where partition_pay_date = ‘2016-04-22’

注意:相对而言,case when是最全的条件函数,可以用于判断多种条件;次之是if函数,属于二分判断;最后是coalesce函数,该函数只能对空值和非空进行判断。

4、字符串函数

字符串长度函数:length

语法: length(string A)。返回字符串A的长度

字符串反转函数:reverse

语法: reverse(string A)。返回字符串A的反转结果

举例:select reverse(abcedfg’) from dual; ##返回值为gfdecba

字符串连接函数:concat

语法: concat(string A, string B…)。返回输入字符串连接后的结果,支持任意个输入字符串

举例:select count(distinct if(partition_is_paid = 1,null,concat(coalesce(uuid,’’),coalesce(deal_id,’’)))) as order_cnt from dw.topic_order where partition_pay_date = ‘2016-04-22’

带分隔符字符串连接函数:concat_ws

语法: concat_ws(string SEP, string A, string B…)。返回输入字符串连接后的结果,SEP表示各个字符串间的分隔符

举例:select concat_ws(’,’,‘abc’,‘def’,‘gh’) from dual; ##返回值为abc,def,gh

字符串截取函数:substr,substring

语法: substr(string A, int start),substring(string A, int start)。返回字符串A从start位置到结尾的字符串

举例:select substr(‘abcde’,3) from dual; ##返回值cde

字符串截取函数:substr,substring

语法: substr(string A, int start, int len),substring(string A, int start, int len)。返回字符串A从start位置开始,长度为len的字符串

举例:select substr(‘abcde’,3,2) from dual; ##返回值为cd

字符串转大写函数:upper,ucase

语法: upper(string A) ucase(string A)。返回字符串A的大写格式

字符串转小写函数:lower,lcase

语法: lower(string A) lcase(string A)。返回字符串A的小写格式

去空格函数:trim

语法: trim(string A)。去除字符串两边的空格

举例:select trim(’ abc ') from dual; ##返回值为abc

左边去空格函数:ltrim

语法: ltrim(string A)。去除字符串左边的空格

右边去空格函数:rtrim

语法: rtrim(string A)。去除字符串右边的空格

正则表达式替换函数:regexp_replace

语法: regexp_replace(string A, string B, string C)。将字符串A中的符合java正则表达式B的部分替换为C。

举例:select regexp_replace(‘foobar’, ‘oo|ar’, ‘’) from dual; ##返回值为fb

正则表达式解析函数:regexp_extract

语法: regexp_extract(string subject, string pattern, int index)。将字符串subject按照pattern正则表达式的规则拆分,返回index指定的字符。

举例:select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 1) from dual; ##返回值为the

注意:(1)在有些情况下要使用转义字符,下面的等号要用双竖线转义,这是java正则表达式的规则。

(2)index的值不能大于pattern中()的个数。

URL解析函数:parse_url

语法: parse_url(string urlString, string partToExtract [, string keyToExtract])。返回URL中指定的部分。partToExtract的有效值为:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO.

举例:select parse_url(‘http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1’, ‘HOST’) from dual; ##返回值为facebook.com

json解析函数:get_json_object

语法: get_json_object(string json_string, string path)。解析json的字符串json_string,返回path指定的内容。如果输入的json字符串无效,那么返回NULL。

空格字符串函数:space

语法: space(int n)。返回长度为n的字符串

举例:select length(space(10)) from dual; ##返回值为10

重复字符串函数:repeat

语法: repeat(string str, int n)。返回重复n次后的str字符串

首字符ascii函数:ascii

语法: ascii(string str)。返回字符串str第一个字符的ascii码

举例:select ascii(‘abcde’) from dual; ##返回值为97

左补足函数:lpad

语法: lpad(string str, int len, string pad)。将str进行用pad进行左补足到len位

举例:select lpad(‘abc’,10,‘td’) from dual; ##返回值为tdtdtdtabc

右补足函数:rpad

语法: rpad(string str, int len, string pad)。将str进行用pad进行右补足到len位

分割字符串函数: split

语法: split(string str, string pat)。按照pat字符串分割str,会返回分割后的字符串数组

举例:select split(‘abtcdtef’,‘t’) from dual; ##返回值为[“ab”,“cd”,“ef”]

集合查找函数: find_in_set

语法: find_in_set(string str, string strList)。返回str在strlist第一次出现的位置,strlist是用逗号分割的字符串。如果没有找该str字符,则返回0

举例:select find_in_set(‘ab’,‘ef,ab,de’) from dual; ##返回值为2

5、统计函数

个数统计函数: count

语法: count(), count(expr), count(DISTINCT expr[, expr_.])。count()统计检索出的行的个数,包括NULL值的行;count(expr)返回指定字段的非空值的个数;count(DISTINCT expr[, expr_.])返回指定字段的不同的非空值的个数

总和统计函数: sum

语法: sum(col), sum(DISTINCT col)。sum(col)统计结果集中col的相加的结果;sum(DISTINCT col)统计结果中col不同值相加的结果

平均值统计函数: avg

语法: avg(col), avg(DISTINCT col)。avg(col)统计结果集中col的平均值;avg(DISTINCT col)统计结果中col不同值相加的平均值

最小值统计函数: min

语法: min(col)。 统计结果集中col字段的最小值

最大值统计函数: max

语法: max(col)。统计结果集中col字段的最大值

中位数函数:precentile语法: percentile(BIGINT col, p)。求准确的第pth个百分位数,p必须介于0和1之间,但是col字段目前只支持整数,不支持浮点数类型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值