HIVE 常用函数介绍

1、 concat (str1,SEP,str2,SEP,str3) 和 concat_ws (SEP,str1,str2,str3)   

          —— 字符窜连接函数:(拼接的字符窜越多concat_ws 越有优势

select concat('山西省','-','太原市','-','迎泽区');
    结果:山西省-太原市-迎泽区

select concat_ws('-','山西省','太原市','迎泽区');
    结果:山西省-太原市-迎泽区

2、parse_url(url, partToExtract[, key]) 

         ——解析URL字符串,partToExtract的选项包含[HOST,PATH,QUERY,REF,PROTOCOL,FILE,AUTHORITY,USERINFO]

select parse_url('http://tdxy-bigdata-04:8889/notebook/editor?editor=957','HOST')

    结果:tdxy-bigdata-04

3、unix_timestamp()

        —— 当前系统时间,是将当前系统时间转换成数字型秒数

      from_unixtime()

       —— 当前系统时间,将数字型按照 格式进行时间转换

select unix_timestamp()
    
    结果:1537956544

select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')

    结果:2018-09-26 18:12:16

4、regexp_replace(string A, string B, string C)

      —— 字符串替换函数,将字符串A 中的B 用 C 替换。

select regexp_replace('www.tuniu.com','tuniu','jd');

    结果:www.jd.com

5、repeat(string str, int n)

       —— 重复N次字符串

select repeat('abcd',4);

    结果:abcdabcdabcdabcd

6、to_date(string timestamp)

      —— 将时间戳转换成日期型字符串

select to_date('2017-01-16 09:55:54');

    结果:2017-01-09

7、date_add(string startdate, int days)

    —— 日期加减

select date_add('2018-09-10', 7);

    结果:2018-09-17

select date_add('2018-09-10', -7);

    结果:2018-09-03

8、row_number()

      —— 添加编号

select row_number() over (distribute by project_code sort by update_time desc) as rd, amount from p2p_project

     结果:  1	2016112100000790	50000
            2	2016112100000790	50000
            1	2016112100000828	50000000
            1	2016112100000860	50000000
            2	2016112100000860	50000000
            3	2016112100000860	50000000

9、字符串长度函数:length


语法: length(string A)
返回值: int

说明:返回字符串A的长度

举例:

hive> select length(‘abcedfg’) from dual;

7

10、字符串反转函数:reverse

语法: reverse(string A)
返回值: string

说明:返回字符串A的反转结果

举例:

hive> select reverse(‘abcedfg’) from dual;

gfdecba

11、字符串截取函数:substr,substring

语法: substr(string A, int start, int len),substring(string A, int start, int len)
返回值: string
说明:返回字符串A从start位置开始,长度为len的字符串
举例:
hive> select substr(‘abcde’,3,2) from dual;
cd
hive> select substring(‘abcde’,3,2) from dual;
cd
hive>select substring(‘abcde’,-2,2) from dual;
de

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

语法: upper(string A) ucase(string A)
返回值: string
说明:返回字符串A的大写格式
举例:
hive> select upper(‘abSEd’) from dual;
ABSED
hive> select ucase(‘abSEd’) from dual;
ABSED

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

语法: lower(string A) lcase(string A)
返回值: string
说明:返回字符串A的小写格式
举例:
hive> select lower(‘abSEd’) from dual;
absed
hive> select lcase(‘abSEd’) from dual;
absed

14、去空格函数:trim

语法: trim(string A)
返回值: string

说明:去除字符串两边的空格

举例:

hive> select trim(‘ abc ‘) from dual;

abc

15、左边去空格函数:ltrim


语法: ltrim(string A)
返回值: string

说明:去除字符串左边的空格

举例:

hive> select ltrim(‘ abc ‘) from dual;

abc

16、右边去空格函数:rtrim

语法: rtrim(string A)
返回值: string

说明:去除字符串右边的空格

举例:

hive> select rtrim(‘ abc ‘) from dual;

abc

17、正则表达式替换函数:regexp_replace

语法: regexp_replace(string A, string B, string C)
返回值: string
说明:将字符串A中的符合java正则表达式B的部分替换为C。注意,在有些情况下要使用转义字符
举例:
hive> select regexp_replace(‘foobar’, ‘oo|ar’, ”) from dual;
fb

18、正则表达式解析函数:regexp_extract

语法: regexp_extract(string subject, string pattern, int index)
返回值: string
说明:将字符串subject按照pattern正则表达式的规则拆分,返回index指定的字符。注意,在有些情况下要使用转义字符
举例:
hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 1) from dual;
the
hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 2) from dual;
bar
hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 0) from dual;
foothebar

19、json解析函数:get_json_object

语法: 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 dual;
amy

20、空格字符串函数:space

语法: space(int n)
返回值: string

说明:返回长度为n的字符串

举例:

hive> select space(10) from dual;

hive> select length(space(10)) from dual;

10

21、首字符ascii函数:ascii

语法: ascii(string str)
返回值: int

说明:返回字符串str第一个字符的ascii码

举例:

hive> select ascii(‘abcde’) from dual;

97

22、分割字符串函数: split

语法:  split(string str, string pat)
返回值:  array
说明: 按照pat字符串分割str,会返回分割后的字符串数组
举例:
hive> select split(‘abtcdtef’,'t’) from dual;
["ab","cd","ef"]

23、集合查找函数: find_in_set

语法: find_in_set(string str, string strList)
返回值: int
说明: 返回str在strlist第一次出现的位置,strlist是用逗号分割的字符串。如果没有找该str字符,则返回0
举例:
hive> select find_in_set('ab','ef,ab,de') from dual;
2
hive> select find_in_set('at','ef,ab,de') from dual;
0

猜你喜欢

转载自blog.csdn.net/weixin_42181917/article/details/82856863