---
title: "hive常用函数"
author: "MNTMs"
date: "2015-05-28T10:00:28Z"
tags: []
categories: ["编程语言"]
slug: "hive"
draft: false
lastmod: "2015-05-28T10:00:28Z"
id: 2214981
url: "https://mntms.iteye.com/blog/2214981"
---

字符串函数

字符串长度函数：length

 

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 语法: length(string A)
2. 返回值: int
3. 说明：返回字符串A的长度
4. 举例：
5. hive&gt; select length(‘abcedfg’) from dual;
6. 7

 

 

 

字符串反转函数：reverse

 

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 语法: reverse(string A)
2. 返回值: string
3. 说明：返回字符串A的反转结果
4. 举例：
5. hive&gt; select reverse(‘abcedfg’) from dual;
6. gfdecba

 

 

 

字符串连接函数：concat

 

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 语法: concat(string A, string B…)
2. 返回值: string
3. 说明：返回输入字符串连接后的结果，支持任意个输入字符串
4. 举例：
5. hive&gt; select concat(‘abc’,'def’,'gh’) from dual;
6. abcdefgh

 

 

 

 

带分隔符字符串连接函数：concat\_ws

 

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 语法: concat\_ws(string SEP, string A, string B…)
2. 返回值: string
3. 说明：返回输入字符串连接后的结果，SEP表示各个字符串间的分隔符
4. 举例：
5. hive&gt; select concat\_ws(‘,’,'abc’,'def’,'gh’) from dual;
6. abc,def,gh

 

 

 

 

字符串截取函数：substr,substring

 

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

01. 语法: substr(string A, int start),substring(string A, int start)
02. 返回值: string
03. 说明：返回字符串A从start位置到结尾的字符串
04. 举例：
05. hive&gt; select substr(‘abcde’,3) from dual;
06. cde
07. hive&gt; select substring(‘abcde’,3) from dual;
08. cde
09. hive&gt;  select substr(‘abcde’,-1) from dual;  （和ORACLE相同）
10. e

 

 

 

字符串截取函数：substr,substring

 

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

01. 语法: substr(string A, int start, int len),substring(string A, int start, int len)
02. 返回值: string
03. 说明：返回字符串A从start位置开始，长度为len的字符串
04. 举例：
05. hive&gt; select substr(‘abcde’,3,2) from dual;
06. cd
07. hive&gt; select substring(‘abcde’,3,2) from dual;
08. cd
09. hive&gt;select substring(‘abcde’,-2,2) from dual;
10. de

 

 

 

 

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

 

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 语法: upper(string A) ucase(string A)
2. 返回值: string
3. 说明：返回字符串A的大写格式
4. 举例：
5. hive&gt; select upper(‘abSEd’) from dual;
6. ABSED
7. hive&gt; select ucase(‘abSEd’) from dual;
8. ABSED

 

 

 

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

 

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 语法: lower(string A) lcase(string A)
2. 返回值: string
3. 说明：返回字符串A的小写格式
4. 举例：
5. hive&gt; select lower(‘abSEd’) from dual;
6. absed
7. hive&gt; select lcase(‘abSEd’) from dual;
8. absed

 

 

 

 

去空格函数：trim

 

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 语法: trim(string A)
2. 返回值: string
3. 说明：去除字符串两边的空格
4. 举例：
5. hive&gt; select trim(‘ abc ‘) from dual;
6. abc

 

 

左边去空格函数：ltrim

 

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 语法: ltrim(string A)
2. 返回值: string
3. 说明：去除字符串左边的空格
4. 举例：
5. hive&gt; select ltrim(‘ abc ‘) from dual;
6. abc

 

 

右边去空格函数：rtrim

 

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 语法: rtrim(string A)
2. 返回值: string
3. 说明：去除字符串右边的空格
4. 举例：
5. hive&gt; select rtrim(‘ abc ‘) from dual;
6. abc

 

 

 

 

 

正则表达式解析函数：regexp\_extract

其中的index，是按照正则字符串（）的位置

 

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

01. 语法: regexp\_extract(string subject, string pattern, int index)
02. 返回值: string
03. 说明：将字符串subject按照pattern正则表达式的规则拆分，返回index指定的字符。注意，在有些情况下要使用转义字符
04. 举例：
05. hive&gt; select regexp\_extract(‘foothebar’, ‘foo(.\*?)(bar)’, 1) from dual;
06. the
07. hive&gt; select regexp\_extract(‘foothebar’, ‘foo(.\*?)(bar)’, 2) from dual;
08. bar
09. hive&gt; select regexp\_extract(‘foothebar’, ‘foo(.\*?)(bar)’, 0) from dual;
10. foothebar

 

 

 

 

 

函数parse\_url,解析URL字符串

 

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

01. parse\_url(url, partToExtract\[, key]) - extracts a part from a URL
02. 解析URL字符串，partToExtract的选项包含\[HOST,PATH,QUERY,REF,PROTOCOL,FILE,AUTHORITY,USERINFO]。
03. 举例：
04. * parse\_url('http://facebook.com/path/p1.php?query=1', 'HOST')返回'facebook.com'
05. * parse\_url('http://facebook.com/path/p1.php?query=1', 'PATH')返回'/path/p1.php'
06. * parse\_url('http://facebook.com/path/p1.php?query=1', 'QUERY')返回'query=1'，
07. 可以指定key来返回特定参数，例如
08. * parse\_url('http://facebook.com/path/p1.php?query=1', 'QUERY','query')返回'1'，
09. * parse\_url('http://facebook.com/path/p1.php?query=1#Ref', 'REF')返回'Ref'
10. * parse\_url('http://facebook.com/path/p1.php?query=1#Ref', 'PROTOCOL')返回'http'

 

json解析函数：get\_json\_object

语法: get\_json\_object(string json\_string, string path)

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

01. 返回值: string
02. 说明：解析json的字符串json\_string,返回path指定的内容。如果输入的json字符串无效，那么返回NULL。
03. 举例：
04. hive&gt; select  get\_json\_object(‘{“store”:
05. &gt;   {“fruit”:\\\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],
06. &gt;    “bicycle”:{“price”:19.95,”color”:”red”}
07. &gt;   },
08. &gt;  “email”:”amy@only\_for\_json\_udf\_test.net”,
09. &gt;  “owner”:”amy”
10. &gt; }
11. &gt; ‘,’$.owner’) from dual;
12. amy

 使用实例：

 

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. select get\_json\_object('{"store":{"fruit":\\\["aa","bb","cc"]},"owner":"amy"}','$.store.fruit\[0]') from test\_msg limit 1;

 

 

空格字符串函数：space

语法: space(int n)

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 返回值: string
2. 说明：返回长度为n的字符串
3. 举例：
4. hive&gt; select space(10) from dual;
5. hive&gt; select length(space(10)) from dual;
6. 10

 

 

重复字符串函数：repeat

语法: repeat(string str, int n)

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 返回值: string
2. 说明：返回重复n次后的str字符串
3. 举例：
4. hive&gt; select repeat(‘abc’,5) from dual;
5. abcabcabcabcabc

 

 

首字符ascii函数：ascii

语法: ascii(string str)

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 返回值: int
2. 说明：返回字符串str第一个字符的ascii码
3. 举例：
4. hive&gt; select ascii(‘abcde’) from dual;
5. 97

 

 

左补足函数：lpad

语法: lpad(string str, int len, string pad)

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 返回值: string
2. 说明：将str进行用pad进行左补足到len位
3. 举例：
4. hive&gt; select lpad(‘abc’,10,’td’) from dual;
5. tdtdtdtabc

 

 

与GP，ORACLE不同，pad 不能默认

右补足函数：rpad

语法: rpad(string str, int len, string pad)

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 返回值: string
2. 说明：将str进行用pad进行右补足到len位
3. 举例：
4. hive&gt; select rpad(‘abc’,10,’td’) from dual;
5. abctdtdtdt

 

 

分割字符串函数: split

语法:  split(string str, string pat)

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 返回值:  array
2. 说明: 按照pat字符串分割str，会返回分割后的字符串数组
3. 举例：
4. hive&gt; select split(‘abtcdtef’,'t’) from dual;
5. \["ab","cd","ef"]

 

 

 

集合查找函数: find\_in\_set

语法: find\_in\_set(string str, string strList)

Java代码  [![收藏代码](http://blackproof.iteye.com/images/icon_star.png)]()

1. 返回值: int
2. 说明: 返回str在strlist第一次出现的位置，strlist是用逗号分割的字符串。如果没有找该str字符，则返回0
3. 举例：
4. hive&gt; select find\_in\_set(‘ab’,'ef,ab,de’) from dual;
5. 2
6. hive&gt; select find\_in\_set(‘at’,'ef,ab,de’) from dual;
7. 0