添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
HIVE常用内置函数

HIVE常用内置函数

1,json转换为结构化对象

select get_json_object('{"store":{"fruit":\["aa, "bb, "cc"]}
,"owner":"amy"}','$.store.fruit[0]');
select find_in_set('ab','ef,ab,de');
select repeat('abc',5);

2,解析URL

parse_url(url, partToExtract[, key]) 
-- extracts a part from a URL --
解析URL字符串,
partToExtract的选项包含[HOST,PATH,QUERY,REF,PROTOCOL,FILE,AUTHORITY,USERINFO]。

3,正则查找与替换

regexp_extract(string subject, string pattern, int index) 
substr(string A, int start, int len),substring(string A, int start, int len)
regexp_replace(string A, string B, string C)
select concat_ws('','abc','def','gh')
select reverse('abcedfg')


4,是否包含子字符串

//测试字符串是否包含子字符串
select if(split('父串','子串')[1] is not NULL,1,NULL)

5,数字IP转换成常规IP

//将数字IP转换成常规IP
字符串是如何转换而来的呢?其实并不复杂,以“218.22.123.26”这个IP地址为例,
IP地址转换成数字串方法如下:先将 “218.22.123.26”转换为十六进制“DA.16.7B.1A”,
然后去掉小数点后,变为“DA167B1A”,最后将这个十六进制数转换为十进制“3658906394”,
那么“218.22.123.26”就变为“3658906394”了。其他IP地址转换为数字串也是使用同样的方法。
SELECT concat_ws('.',CONV(substr(hex(3658906394),1,2),16,10),
CONV(substr(hex(3658906394),3,2),16,10),
CONV(substr(hex(3658906394),5,2),16,10),
CONV(substr(hex(3658906394),7,2),16,10));
select CONV(substr(hex(3658906394),3,4),16,10);
substr(string A, int start, int len)
substring(string A, intstart, int len)


6,集合查找函数

集合查找函数:find_in_set
find_in_set(string str, string strList)
返回str在strlist第一次出现的位置,strlist是用逗号分割的字符串。如果没有找该str字符,则返回0
cast(<expr> as <type>)
size(Array<T>)
from_unixtime(int unixtime)
to_date(string timestamp)


7,正则表达式的基础学习

. 一个单一字符
* 零次或多次
+ 一次或多次
?零次或一次
{n,m} 至少匹配n次,最多匹配m次
[a-z] //匹配所有的小写字母 
[A-Z] //匹配所有的大写字母 
[a-zA-Z] //匹配所有的字母 
[0-9] //匹配所有的数字 
[0-9\.\-] //匹配所有的数字,句号和减号 
[ \f\r\t\n] //匹配所有的白字符
[^a-z] //除了小写字母以外的所有字符 
[^\\\/\^] //除了(\)(/)(^)之外的所有字符 
[^\"\'] //除了双引号(")和单引号(')之外的所有字符

8,位运算

select 2&2------2
select 3&2------2
select 1|2------3
select 2|2------2
select ~2------ -3
bin(BIGINT a)
hex(BIGINT a) hex(string a)
unhex(string a)
conv(BIGINT num, int from_base, int to_base)---
select conv(150354d103e2a6cfb,16,10)
pmod(double a, double b) 返回余数绝对值
cast(expr as <type>) 类型转换


9,日期运算

date_add(string startdate, int days)
date_sub(string startdate, int days)
unix_timestamp(string date, string pattern) 将指定格式的时间 转换成秒
from_unixtime(bigint unixtime[, string format])
select from_unixtime(1463042015872,'yyyy-MM-dd HH:mm:ss')
select sentences('Hello there! How are you?')

10,语句分析

SELECT ngrams(sentences('Hello there! How are you?'), 2, 100 ,1000)
结果:[{"ngram":["Hello, "there"],"estfrequency":1.0},{"ngram":["How, "are"],"estfrequency":1.0},{"ngram":["are, "you"],"estfrequency":1.0}]
SELECT context_ngrams(sentences(lower(tweet)), array(null,null), 100, [, 1000]) FROM twitter;

11,集合

collect_set(col) 无重复记录
explode(array<TYPE> a) 拆分成列

12,分析函数(很重要)

分析函数:LAG--排序向上去第n,LEAD--排序向下去第n,
FIRST_VALUE--取分组排序当前行的第一个,
LAST_VALUE--取分组排序当前行的最后一个就是自己;
SELECT cookieid,
createtime,