一、数值计算
对数函数: log
double log(double base, double a)
说明:返回以base为底的a的对数
例: select log(4,256) ;
4.0
幂运算函数: pow
double pow(double a, double p)
说明:返回a的p次幂
例: select pow(2.1,0.5) ;
1.449137674618944
进制转换函数: conv
string conv(bigint /string num, int from_base, int to_base)
说明:将数值num从from_base进制转化到to_base进制
例:select conv('abc',16,10);
2748
正取余函数: pmod
int pmod(int a, int b),
double pmod(double a, double b)
说明:返回正的a除以b的余数
例:select pmod(-9,4);
3
[反]正弦函数: [a]sin
double [a]sin(double/ decimal a)
说明:返回a的[反]正弦值
例:select sin(0.8);
0.7173560908995228
例:select asin(0.7173560908995228);
0.8
[反]余弦函数: [a]cos
double [a]cos (double/ decimal a)
说明:返回a的[反]余弦值
例:select cos(0.8);
0.6967067093471654
例:select acos(0.6967067093471654 );
0.8
[反]正切函数: [a]tan
double [a]tan (double/ decimal a)
说明:返回a的[反]正切值
例:select tan(0.8);
1.0296385570503641
例:select atan(1.0296385570503641 );
0.8
弧度转角度函数: degrees
double degrees (double/ decimal a)
说明:将弧度a转为角度
例:select degree( pi());
180.0
角度转弧度函数: radians
double radians (double/decimal a)
说明:将弧度a转为角度
例:select radians(90);
1.5707963267948966
positive函数: positive
int positive(int a), double positive(double a)
说明:返回a
例:select positive(-10.56) ;
-10.56
negative函数: negative
int negative(int a), double negative(double a)
说明:返回-a
例:select negative(-5) ;
5
sign函数: sign
int sign (double a),double sign (decimal a)
说明:返回-[+]1
例:select sign(-5) ;
-1
pi()函数: pi
double pi()
说明:返回3.14......
例:select pi();
3.141592653589793
e()函数: e
double e()
说明:返回2.718.....
例:select e();
2.718281828459045
greatest()函数:greatest
double greatest (T...vs)
说明:返回vs中的最大值
例:select greatest(1,2,5.4,-2);
5.4
least()函数:least
double least (T...vs)
说明:返回vs中的最小值
例:select least(1,2,5.4,-2);
-2
财务舍入法函数:bround
double bround (double a , int d)
说明::四舍六入五考虑,五后非空就进一,五后为空看奇偶,五前为偶应舍去,五前为奇要进一
例:select bround(2.355,2);
2.36
select bround(2.355,3);
2.35
select bround(2.3465,3);
factorial()函数
int factorial (int a)
说明:20以内阶乘
例:select factorial(4)
左位移函数:shiftleft()
bigint shiftleft(bigint a, int b)
说明:a左移b位
例:select shiftleft(2,3)
右位移函数:shiftright()
bigint shiftright(bigint a, int b)
说明:a右移b位
例:select shiftright(2,3)
二、集合函数
长度函数size()
int size(Map(K,V)/Array<T>)
说明:返回map/array类型的长度
例:select size(map('100','tom','101','mary'));
2
select size(array('100','101','102','103')) ;
4
map_keys()
Array<K> map_keys(Map<K,V>)
说明:以数组形式返回map所有键
例:select map_keys(map('100','tom','101','mary'));
["100","101"]
map_values()
Array<V> map_values(Map<K,V>)
说明:以数组形式返回map所有值
例:select map_values(map('100','tom','101','mary'));
["tom","mary"]
array_contains()
boolean array_contains(Array<T>, value)
说明:如该数组Array<T>包含value返回true。,否则返回false
例:select array_contains(array('103','101','100','102'),'104');
false
数组排序函数sort_array()
Array<T> sort_array(Array<T>)
说明:按自然顺序对数组进行排序并返回
例:select sort_array(array('103','101','100','102'));
["100","101","102","103"]
array类型构建array()
Array<T> array(T...t);
说明根据输入的参数构建数组array类型
例:select array('aa','ss','dd');
["aa","ss","dd"]
map类型构建map()
Map<K,V> map (k1, v1, k2, v2,…);
说明:根据输入的key和value对构建map类型
例:select map('name','hury','age','22');
{"name":"hury","age":"22"}
struct类型构建struct()
Struct<....> struct(v1, v2, v3,…)
说明:根据输入的参数构建结构体struct类型
例:select struct('name',22);
{"col1":"name","col2":22}
三、类型转换
类型转换函数cast()
type cast(expr as <type>)
说明:转换expr类型
例:select cast("1" as bigint) ;
1(bigint)
四、日期函数
UNIX时间戳转日期函数:from_unixtime
string from_unixtime(bigint time,string time_format)
说明:转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式
例:select unix_timestamp();
1605180154
select from_unixtime(1605180154,'yyyy-MM-dd');
2020-11-12
select from_unixtime(1605180154,'yyyy-MM-dd');
11
data_format()
string date_format(date/timestamp/string date,string format)
说明:从符合日期格式的字符串提取想要内容
例:select date_format('1998-10-17 22:22:22','MM');
10
current_date()
string current_date()
说明:提取当前系统时间年月日
例:select current_date();
2020-12-10
todate()
string todate(string timestamp)
说明:提取指定时间年月日
例:select to_date('2011-12-08 10:03:01')
2011-12-08
current_timestamp()
string current_timestamp()
说明:提取当前系统时间年月日时分秒毫秒
例:select current_timestamp();
2020-12-10 14:35:22.392
unix_timestamp()
bigint unix_timestamp()
说明:提取当前系统时间戳
例:select unix_timestamp( '2020-12-10 14:12:35.676');
| 1607580755
date_add()
string date_add(string date,int days)
说明:提取指定日期的几天后\前日期
例:select date_add(current_date(),2);
2020-12-12
select date_add(current_date(),-2);
2020-12-08
add_months()
string add_months(string date,int months)
说明:提取指定日期的几个月后\前日期
例:select add_months(current_date(),2);
2021-02-10
select add_months(current_date(),-2);
2020-10-10
next_day()
string next_day(string date,string dayOfWeek)
说明:下一个星期几日期
例:select next_day(current_date(),'TU');
2020-12-15
select next_day(current_date(),'FR');
2020-12-11
last_day()
string last_day(string date)
说明:指定日期的月份最后一天
例:select last_day(current_date());
2020-12-31
trunc()
string trunc(string date,string format)
说明:指定日期的最开始的日期
例:select trunc(current_date(),'YY');
2020-01-01
select trunc(current_date(),'MM');
2020-12-01
datediff()
int datediff(string dateto,string datefrom)
说明:指定两个日期之间天数
例:select datediff(current_date(),'2020-10-08');
63
months_between()
double months_between(string dateto,string datefrom)
说明:指定两个日期之间天数
例:select months_between(current_date(),'2020-10-08');
2.06451613
五、条件函数
if()
T if(boolean , T vtrue,T vfalse)
说明:第一个参数正确,返回vtrue,否则返回vfalse
例:select if(true,1,3);
1
nvl()
T nvl(T value,T default)
说明:参数1为null,返回参数2.否则返回参数1
例:select nvl(1,3);
1
select nvl(null,3);
3
coalesce()
T coalesce(T ...vs)
说明:返回第一个非null
例:select coalesce(null,null,null,3,2);
3
isnull()
boolean isnull(NULL)
说明:判断参数是否为空
例:select isnull(5);
false
isnotnull()
boolean isnotnull(NULL)
说明:判断参数是否为空
例:select isnotnull(5);
true
六、字符函数
ascii()
int ascii(string a)
说明:返回字符串首字母的ascll码值
例:select ascii('acdffjh');
97
concat()
string concat(string ...array)
说明:将动态字符串拼接成大字符串
例:select concat('aa','bb');
aabb
concat_ws()
string concat_ws(string sep,string ...array)
说明:用特殊符号将动态字符串拼接成大字符串
例:select concat_ws('|','aa','bb');
aa|bb
分词函数sentences()
array<array<string>> sentences(string sentence)
说明:返回输入 str 分词后的单词数组
例:select sentences('hello!how are you?');
[["hello"],["how","are","you"]]
ngrams()
array<struct<string,double>> ngrams(array<array<string>> arr,int n,int k)
说明:与 sentences()函数一起使用,分词后,统计分词结果,按n个单词出现频次,倒序取top k
例:select ngrams(sentences('hello word!hello hive,hi hive,hello hive'),2,2);
[{"ngram":["hello","hive"],"estfrequency":2.0},{"ngram":["hive","hello"],"estfrequency":1.0}]
该查询中,统计的是两个词在一起出现频次最高的 TOP-2
结果中,hello 与 hive 同时出现 2 次
select ngrams(sentences('hello word!hello hive,hi hive'),2,2);
[{"ngram":["hello","word"],"estfrequency":1.0},{"ngram":["hive","hi"],"estfrequency":1.0}]
该查询中,统计的是两个词在一起出现频次最高的 TOP-2
结果中,hello 与world 只出现 1次
context_ngrams()
array<struct<string,double>> context_ngrams(array<array<string>> arr, array<string>, int k)
说明: sentences()函数一起使用,分词后,统计分词结果,与数组中指定的单词一起出现频次,倒序取top k
例:select context_ngrams(sentences('hello!how are you?'),array('are',null),1);
[{"ngram":["you"],"estfrequency":1.0}]
select context_ngrams(sentences('hello word!hello hive,hi hive,hello hive'),array('hello',null),2);
[{"ngram":["hive"],"estfrequency":2.0},{"ngram":["word"],"estfrequency":1.0}]
encode()
binary encode(string source,string charset)
说明:以UTF-16BE加密
#"US-ASCII','ISO-8859-1','UTF-8','UTF-16BE','UTF-16LE','UTF-16'
例:select encode('我爱世界','UTF-16BE');
br1NuL
decode()
string decode(string source,string charset)
说明:以UTF-16BE解密
#"US-ASCII','ISO-8859-1','UTF-8','UTF-16BE','UTF-16LE','UTF-16'
例:select decode(encode('我爱世界','UTF-16BE'),'UTF-16BE');
我爱世界
format_number()
string format_number(decimal number ,int d)
说明:参数一小数点保留两位四舍五入
例:select format_number(12.2324,2);
12.23
get_json_object()
string get_json_object(string json,string path)
说明:通过path取出对应的数据
#HOST,PATH,QUERY,REF,PROTOCOL,AUTHORITY,FILE,and USERINFO
例:select get_json_object('{"name":"henay"}','$.name');
henay
select get_json_object('{"name":"henay","info":{"city":"nj"}}','$.info');
{"city":"nj"}
select get_json_object('{"name":"henay","info":[{"city":"nj"},{"city":"sh"}]}','$.info[1]');
{"city":"sh"}
in_file()
boolean in_file(string line,string path)
说明:path 指向虚拟机磁盘文件是否包含line内容
parse_url()
string parse_url(string urlstring,string part [,string key])
说明:提取url指定内容
#HOST,PATH,QUERY,REF,PROTOCOL,AUTHORITY,FILE and USERINFO
例:select parse_url( 'https://www.baidu.com/s?wd=hive%20in_file&rsv_spt=1&rsv_iqid=0xd471c7a00005ed27&issp=16f=3krsv_bp=1Ersv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=l&rsv_dl=ts06oq=hive%2520get_json_object&rsv_btype=t&inputT=2997&rsv_t=1825Ggpz0pjzYGW7hOC6XIYL8C1ZePd1Y8DrVi2zC73aCOBc%2FlhsrftC8D6YDAoTVTwCErsv_pq=b914a3c000057e58&rsv_sug3=1586rsv_sug1=102&rsv_sug7=1006rsv_sug2=0&prefixsug=hive%2520in_fi&rsp=0&rsv_sug4=3894','HOST');
www.baidu.com
printf()
string printf(string format,T...t)
说明:以参数1的格式输出后面内容
例:select printf('%s%d%.2f','huang',25,1245.3456);
huang251245.35
like()
boolean like(string str)
说明:判断字段是否符合参数1
# % _
例:select 'abcde' like 'abc';
false
select 'abcdef' like 'abc___';
true
select 'abcdef' like 'ab%';
true
rlike()
boolean rlike(string str)
说明:判断字段是否符合参数1
# [ ] { } ? + * \\d \w ...
例:select 'abcde' rlike 'abc';
true
select 'abcde' rlike '\\w+';
true
select '123456' rlike '\\d+';
true
regexp_replace()
string regexp_replace(string src,string regex,string replacement);
说明:把参数1里的参数2替换成参数3
# [ ] { } ? + * \\d \w ...
例:select regexp_replace('you he you me','you','You');
You he You me
select regexp_replace('you he you me','y\\w{2,3}','You');
You he You me
regexp_extract()
string regexp_extract(string src,string regex,int index )
说明:提取字符串里的子字符串
# [ ] { } ? + * \\d \w ...
例:select regexp_extract('namehuangokdalingduck','name(.*?)ok(.*?)duck',2);
daling
split()
array<string> split(string src,string regex)
说明:将字符串src以regex分割,以数组形式输出
例:select regexp_replace('["huang","polar","petry"]','\\[|\\]|"','');
huang,polar,petry
select split(regexp_replace('["huang","polar","petry"]','\\[|\\]|"',''),',');
["huang","polar","petry"]
str_to_map()
map<string,string> str_to_map(string src,string sep1,string sep2);
说明:将字符串转为map
例:select str_to_map('name:hury,age:22,gender:male');
{"name":"hury","gender":"male","age":"22"}
translate()
string translate(string src,string chars,string dchars)
说明:以参数3替换字符串指定内容
例:select translate('adafbvdhdvf','adfbvh','123456');
12134526253
initcap()
string initcap(string src)
说明:首字母大写
例:select initcap('adafbvdhdvf');
Adafbvdhdvf
substr()
string substr(string src,int begin[,int len]);
说明:从字符串中提取子字符串
例:select substr('henry',2,3);
enr
locate()
int locate(string sub,string src,int startPos);
说明:sub在src中从第n位开始第一次出现的起始位置
例:select locate('en','henreny',4);
5
instr()
int locate(string src,string sub)
说明:sub在src中的第一次出现的起始位置
例:select instr('henry','en');
2
md5()
string md5(string src)
说明:以md5给src加密
例:select md5('abc');
900150983cd24fb0d6963f7d28e17f72
base64()
string base64(binary b)
说明:以base64给b加密
例:select base64(cast('abc' as binary));
YWJj
unbase64()
string unbase64(string src);
说明:以unbase64给src解密
例:select unbase64(' YWJj');
abc
hex()
string .hex(string src);
说明:以hex给src加密
例:select hex('abc');
616263
unhex()
string unhex(int h);
说明:以unhex给h解密
例:select unhex( 616263);
abc
sha2()
string sha2(string src);
说明:以sha2给src加密
# 1/0
例:select sha2('abc',1);
a9993e364706816aba3e25717850c26c9cd0d89d
select sha2('abc',0);
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
七、聚合函数
count()
int count (expr)
说明:统计expr个数
sum()
double sum(expr)
说明:统计expr总和
var_pop()
double var_pop(col)
说明:方差(离散程度)
var_sample()
double var_sample(col)
说明:样本方差(变异程度)
stddev_pop()
double stddev_pop(col)
说明:标准偏差
studev_sample()
double studev_sample(col)
说明:样本标准偏差
covar_pop()
double covar_pop(col1,col2)
说明:协方差
covar_sample()
double covar_sample(col1,col2)
说明:样本协方差
corr()
double corr(col1,col2)
说明:两列数值的相关系数
percentile()
double percentile(bigint col,int p)
说明:返回col的p(0~1)%分位数
collect_list()
double collect_list(col)
说明:行转列
collect_set()
double collect_set(col)
说明:行转列(去重)
本文详细介绍了 Hive 中的各种函数,包括数值计算函数(如 log、pow、conv)、集合函数(如 size、map_keys、array_contains)、类型转换函数(如 cast)、日期函数(如 from_unixtime、date_format)、条件函数(如 if、nvl)、字符函数(如 ascii、concat、split)以及聚合函数(如 count、sum、percentile),是学习和使用 Hive 的重要参考资料。
&spm=1001.2101.3001.5002&articleId=109633247&d=1&t=3&u=f0bbc09f187e4f01ad3c74337a0c4a19)
958

被折叠的 条评论
为什么被折叠?



