欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

hive内置方法一览

程序员文章站 2022-08-28 12:22:32
引用 https://www.cnblogs.com/qingyunzong/p/8744593.html#_label0 官方文档 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF 目录 数学函数 集合函数 类型 ......

 

正文

回到顶部

数学函数

return type

name (signature)

description

double

round(double a)

returns the rounded bigint value of a.

返回对a四舍五入的bigint值

double

round(double a, int d)

returns a rounded to d decimal places.

返回double型d的保留n位小数的doublw型的近似值

double bround(double a) returns the rounded bigint value of a using half_even rounding mode (as of hive 1.3.0, 2.0.0). also known as gaussian rounding or bankers' rounding. example: bround(2.5) = 2, bround(3.5) = 4.
银行家舍入法(1~4:舍,6~9:进,5->前位数是偶:舍,5->前位数是奇:进)
double bround(double a, int d) returns a rounded to d decimal places using half_even rounding mode (as of hive 1.3.0, 2.0.0). example: bround(8.25, 1) = 8.2, bround(8.35, 1) = 8.4.
银行家舍入法,保留d位小数

bigint

floor(double a)

returns the maximum bigint value that is equal to or less than a

向下取整,最数轴上最接近要求的值的左边的值  如:6.10->6   -3.4->-4

bigint

ceil(double a), ceiling(double a)

returns the minimum bigint value that is equal to or greater than a.

求其不小于小给定实数的最小整数如:ceil(6) = ceil(6.1)= ceil(6.9) = 6

double

rand(), rand(int seed)

returns a random number (that changes from row to row) that is distributed uniformly from 0 to 1. specifying the seed will make sure the generated random number sequence is deterministic.

每行返回一个double型随机数seed是随机因子

double

exp(double a), exp(decimal a)

returns ea where e is the base of the natural logarithm. decimal version added in hive 0.13.0.

返回e的a幂次方, a可为小数

double

ln(double a), ln(decimal a)

returns the natural logarithm of the argument a. decimal version added in hive 0.13.0.

以自然数为底d的对数,a可为小数

double

log10(double a), log10(decimal a)

returns the base-10 logarithm of the argument a. decimal version added in hive 0.13.0.

以10为底d的对数,a可为小数

double

log2(double a), log2(decimal a)

returns the base-2 logarithm of the argument a. decimal version added in hive 0.13.0.

以2为底数d的对数,a可为小数

double

log(double base, double a)

log(decimal base, decimal a)

returns the base-base logarithm of the argument a. decimal versions added in hive 0.13.0.

以base为底的对数,base 与 a都是double类型

double

pow(double a, double p), power(double a, double p)

returns ap.

计算a的p次幂

double

sqrt(double a), sqrt(decimal a)

returns the square root of a. decimal version added in hive 0.13.0.

计算a的平方根

string

bin(bigint a)

returns the number in binary format (see ).

计算二进制a的string类型,a为bigint类型

string

hex(bigint a) hex(string a) hex(binary a)

if the argument is an int or binaryhex returns the number as a string in hexadecimal format. otherwise if the number is a string, it converts each character into its hexadecimal representation and returns the resulting string. (see, binary version as of hive 0.12.0.)

计算十六进制a的string类型,如果a为string类型就转换成字符相对应的十六进制

binary

unhex(string a)

inverse of hex. interprets each pair of characters as a hexadecimal number and converts to the byte representation of the number. (binary version as of hive 0.12.0, used to return a string.)

hex的逆方法

string

conv(bigint num, int from_base, int to_base), conv(string num, int from_base, int to_base)

converts a number from a given base to another (see ).

将gigint/string类型的num从from_base进制转换成to_base进制

double

abs(double a)

returns the absolute value.

计算a的绝对值

int or double

pmod(int a, int b), pmod(double a, double b)

returns the positive value of a mod b.

a对b取模

double

sin(double a), sin(decimal a)

returns the sine of a (a is in radians). decimal version added in hive 0.13.0.

求a的正弦值

double

asin(double a), asin(decimal a)

returns the arc sin of a if -1<=a<=1 or null otherwise. decimal version added in hive 0.13.0.

求d的反正弦值

double

cos(double a), cos(decimal a)

returns the cosine of a (a is in radians). decimal version added in hive 0.13.0.

求余弦值

double

acos(double a), acos(decimal a)

returns the arccosine of a if -1<=a<=1 or null otherwise. decimal version added in hive 0.13.0.

求反余弦值

double

tan(double a), tan(decimal a)

returns the tangent of a (a is in radians). decimal version added in hive 0.13.0.

求正切值

double

atan(double a), atan(decimal a)

returns the arctangent of a. decimal version added in hive 0.13.0.

求反正切值

double

degrees(double a), degrees(decimal a)

converts value of a from radians to degrees. decimal version added in hive 0.13.0.

奖弧度值转换角度值

double

radians(double a), radians(double a)

converts value of a from degrees to radians. decimal version added in hive 0.13.0.

将角度值转换成弧度值

int or double

positive(int a), positive(double a)

returns a.

返回a

int or double

negative(int a), negative(double a)

returns -a.

返回a的相反数

double or int

sign(double a), sign(decimal a)

returns the sign of a as '1.0' (if a is positive) or '-1.0' (if a is negative), '0.0' otherwise. the decimal version returns int instead of double. decimal version added in hive 0.13.0.

如果a是正数则返回1.0,是负数则返回-1.0,否则返回0.0

double

e()

returns the value of e.

数学常数e

double

pi()

returns the value of pi.

数学常数pi

bigint factorial(int a) returns the factorial of a (as of hive 1.2.0). valid a is [0..20].
求a的阶乘
double cbrt(double a) returns the cube root of a double value (as of hive 1.2.0).
求a的立方根

 

int bigint

shiftleft(tinyint|smallint|int a, int b)

shiftleft(bigint a, int b)

bitwise left shift (as of hive 1.2.0). shifts a b positions to the left.

returns int for tinyint, smallint and int a. returns bigint for bigint a.

按位左移

int

bigint

shiftright(tinyint|smallint|int a, intb)

shiftright(bigint a, int b)

bitwise right shift (as of hive 1.2.0). shifts a b positions to the right.

returns int for tinyint, smallint and int a. returns bigint for bigint a.

按拉右移

int

bigint

shiftrightunsigned(tinyint|smallint|inta, int b),

shiftrightunsigned(bigint a, int b)

bitwise unsigned right shift (as of hive 1.2.0). shifts a b positions to the right.

returns int for tinyint, smallint and int a. returns bigint for bigint a.

无符号按位右移(<<<)

t greatest(t v1, t v2, ...) returns the greatest value of the list of values (as of hive 1.1.0). fixed to return null when one or more arguments are null, and strict type restriction relaxed, consistent with ">" operator (as of hive 2.0.0).
求最大值
t least(t v1, t v2, ...) returns the least value of the list of values (as of hive 1.1.0). fixed to return null when one or more arguments are null, and strict type restriction relaxed, consistent with "<" operator (as of hive 2.0.0).
求最小值
 
回到顶部

集合函数

return type

name(signature)

description

int

size(map<k.v>)

returns the number of elements in the map type.

求map的长度

int

size(array<t>)

returns the number of elements in the array type.

求数组的长度

array<k>

map_keys(map<k.v>)

returns an unordered array containing the keys of the input map.

返回map中的所有key

array<v>

map_values(map<k.v>)

returns an unordered array containing the values of the input map.

返回map中的所有value

boolean

array_contains(array<t>, value)

returns true if the array contains value.

如该数组array<t>包含value返回true。,否则返回false

array

sort_array(array<t>)

sorts the input array in ascending order according to the natural ordering of the array elements and returns it (as of version 0.9.0).

按自然顺序对数组进行排序并返回

 
回到顶部

类型转换函数

return type

name(signature)

description

binary

binary(string|binary)

casts the parameter into a binary.

将输入的值转换成二进制

expected "=" to follow "type"

cast(expr as <type>)

converts the results of the expression expr to <type>. for example, cast('1' as bigint) will convert the string '1' to its integral representation. a null is returned if the conversion does not succeed. if cast(expr as boolean) hive returns true for a non-empty string.

将expr转换成type类型 如:cast("1" as bigint) 将字符串1转换成了bigint类型,如果转换失败将返回null

 
 
回到顶部

日期函数

return type

name(signature)

description

string

from_unixtime(bigint unixtime[, string format])

converts the number of seconds from unix epoch (1970-01-01 00:00:00 utc) to a string representing the timestamp of that moment in the current system time zone in the format of "1970-01-01 00:00:00".

将时间的秒值转换成format格式(format可为“yyyy-mm-dd hh:mm:ss”,“yyyy-mm-dd hh”,“yyyy-mm-dd hh:mm”等等)如from_unixtime(1250111000,"yyyy-mm-dd") 得到2009-03-12

bigint

unix_timestamp()

gets current unix timestamp in seconds.

获取本地时区下的时间戳

bigint

unix_timestamp(string date)

converts time string in format yyyy-mm-dd hh:mm:ss to unix timestamp (in seconds), using the default timezone and the default locale, return 0 if fail: unix_timestamp('2009-03-20 11:30:01') = 1237573801

将格式为yyyy-mm-dd hh:mm:ss的时间字符串转换成时间戳  如unix_timestamp('2009-03-20 11:30:01') = 1237573801

bigint

unix_timestamp(string date, string pattern)

convert time string with given pattern (see [http://docs.oracle.com/javase/tutorial/i18n/format/simpledateformat.html]) to unix time stamp (in seconds), return 0 if fail: unix_timestamp('2009-03-20', 'yyyy-mm-dd') = 1237532400.

将指定时间字符串格式字符串转换成unix时间戳,如果格式不对返回0 如:unix_timestamp('2009-03-20', 'yyyy-mm-dd') = 1237532400

string

to_date(string timestamp)

returns the date part of a timestamp string: to_date("1970-01-01 00:00:00") = "1970-01-01".

返回时间字符串的日期部分

int

year(string date)

returns the year part of a date or a timestamp string: year("1970-01-01 00:00:00") = 1970, year("1970-01-01") = 1970.

返回时间字符串的年份部分

int quarter(date/timestamp/string) returns the quarter of the year for a date, timestamp, or string in the range 1 to 4 (as of hive 1.3.0). example: quarter('2015-04-08') = 2.

返回当前时间属性哪个季度 如quarter('2015-04-08') = 2

int

month(string date)

returns the month part of a date or a timestamp string: month("1970-11-01 00:00:00") = 11, month("1970-11-01") = 11.

返回时间字符串的月份部分

int

day(string date) dayofmonth(date)

returns the day part of a date or a timestamp string: day("1970-11-01 00:00:00") = 1, day("1970-11-01") = 1.

返回时间字符串的天

int

hour(string date)

returns the hour of the timestamp: hour('2009-07-30 12:58:59') = 12, hour('12:58:59') = 12.

返回时间字符串的小时

int

minute(string date)

returns the minute of the timestamp.

返回时间字符串的分钟

int

second(string date)

returns the second of the timestamp.

返回时间字符串的秒

int

weekofyear(string date)

returns the week number of a timestamp string: weekofyear("1970-11-01 00:00:00") = 44, weekofyear("1970-11-01") = 44.

返回时间字符串位于一年中的第几个周内  如weekofyear("1970-11-01 00:00:00") = 44, weekofyear("1970-11-01") = 44

int

datediff(string enddate, string startdate)

returns the number of days from startdate to enddate: datediff('2009-03-01', '2009-02-27') = 2.

计算开始时间startdate到结束时间enddate相差的天数

string

date_add(string startdate, int days)

adds a number of days to startdate: date_add('2008-12-31', 1) = '2009-01-01'.

从开始时间startdate加上days

string

date_sub(string startdate, int days)

subtracts a number of days to startdate: date_sub('2008-12-31', 1) = '2008-12-30'.

从开始时间startdate减去days

timestamp

from_utc_timestamp(timestamp, string timezone)

assumes given timestamp is utc and converts to given timezone (as of hive 0.8.0). for example, from_utc_timestamp('1970-01-01 08:00:00','pst') returns 1970-01-01 00:00:00.

如果给定的时间戳并非utc,则将其转化成指定的时区下时间戳

timestamp

to_utc_timestamp(timestamp, string timezone)

assumes given timestamp is in given timezone and converts to utc (as of hive 0.8.0). for example, to_utc_timestamp('1970-01-01 00:00:00','pst') returns 1970-01-01 08:00:00.

如果给定的时间戳指定的时区下时间戳,则将其转化成utc下的时间戳

date current_date

returns the current date at the start of query evaluation (as of hive 1.2.0). all calls of current_date within the same query return the same value.

返回当前时间日期

timestamp current_timestamp

returns the current timestamp at the start of query evaluation (as of hive 1.2.0). all calls of current_timestamp within the same query return the same value.

返回当前时间戳

string add_months(string start_date, int num_months)

returns the date that is num_months after start_date (as of hive 1.1.0). start_date is a string, date or timestamp. num_months is an integer. the time part of start_date is ignored. if start_date is the last day of the month or if the resulting month has fewer days than the day component of start_date, then the result is the last day of the resulting month. otherwise, the result has the same day component as start_date.

返回当前时间下再增加num_months个月的日期

string last_day(string date) returns the last day of the month which the date belongs to (as of hive 1.1.0). date is a string in the format 'yyyy-mm-dd hh:mm:ss' or 'yyyy-mm-dd'. the time part of date is ignored.

返回这个月的最后一天的日期,忽略时分秒部分(hh:mm:ss)

string next_day(string start_date, string day_of_week) returns the first date which is later than start_date and named as day_of_week (as of hive1.2.0). start_date is a string/date/timestamp. day_of_week is 2 letters, 3 letters or full name of the day of the week (e.g. mo, tue, friday). the time part of start_date is ignored. example: next_day('2015-01-14', 'tu') = 2015-01-20.

返回当前时间的下一个星期x所对应的日期 如:next_day('2015-01-14', 'tu') = 2015-01-20  以2015-01-14为开始时间,其下一个星期二所对应的日期为2015-01-20

string trunc(string date, string format) returns date truncated to the unit specified by the format (as of hive 1.2.0). supported formats: month/mon/mm, year/yyyy/yy. example: trunc('2015-03-17', 'mm') = 2015-03-01.

返回时间的最开始年份或月份  如trunc("2016-06-26",“mm”)=2016-06-01  trunc("2016-06-26",“yy”)=2016-01-01   注意所支持的格式为month/mon/mm, year/yyyy/yy

double months_between(date1, date2) returns number of months between dates date1 and date2 (as of hive 1.2.0). if date1 is later than date2, then the result is positive. if date1 is earlier than date2, then the result is negative. if date1 and date2 are either the same days of the month or both last days of months, then the result is always an integer. otherwise the udf calculates the fractional portion of the result based on a 31-day month and considers the difference in time components date1 and date2. date1 and date2 type can be date, timestamp or string in the format 'yyyy-mm-dd' or 'yyyy-mm-dd hh:mm:ss'. the result is rounded to 8 decimal places. example: months_between('1997-02-28 10:30:00', '1996-10-30') = 3.94959677

返回date1与date2之间相差的月份,如date1>date2,则返回正,如果date1<date2,则返回负,否则返回0.0  如:months_between('1997-02-28 10:30:00', '1996-10-30') = 3.94959677  1997-02-28 10:30:00与1996-10-30相差3.94959677个月

string date_format(date/timestamp/string ts, string fmt)

converts a date/timestamp/string to a value of string in the format specified by the date format fmt (as of hive 1.2.0). supported formats are java simpledateformat formats –https://docs.oracle.com/javase/7/docs/api/java/text/simpledateformat.html. the second argument fmt should be constant. example: date_format('2015-04-08', 'y') = '2015'.

date_format can be used to implement other udfs, e.g.:

  • dayname(date) is date_format(date, 'eeee')
  • dayofyear(date) is date_format(date, 'd')

    按指定格式返回时间date 如:date_format("2016-06-22","mm-dd")=06-22

 
 
回到顶部

条件函数

return type

name(signature)

description

t

if(boolean testcondition, t valuetrue, t valuefalseornull)

returns valuetrue when testcondition is true, returns valuefalseornull otherwise.

如果testcondition 为true就返回valuetrue,否则返回valuefalseornull ,(valuetrue,valuefalseornull为泛型) 

t nvl(t value, t default_value) returns default value if value is null else returns value (as of hive 0.11).

如果value值为null就返回default_value,否则返回value

t

coalesce(t v1, t v2, ...)

returns the first v that is not null, or null if all v's are null.

返回第一非null的值,如果全部都为null就返回null  如:coalesce (null,44,55)=44/strong>

t

case a when b then c [when d then e]* [else f] end

when a = b, returns c; when a = d, returns e; else returns f.

如果a=b就返回c,a=d就返回e,否则返回f  如case 4 when 5  then 5 when 4 then 4 else 3 end 将返回4

t

case when a then b [when c then d]* [else e] end

when a = true, returns b; when c = true, returns d; else returns e.

如果a=ture就返回b,c= ture就返回d,否则返回e  如:case when  5>0  then 5 when 4>0 then 4 else 0 end 将返回5;case when  5<0  then 5 when 4<0 then 4 else 0 end 将返回0

boolean isnull( a ) returns true if a is null and false otherwise.

如果a为null就返回true,否则返回false

boolean isnotnull ( a ) returns true if a is not null and false otherwise.

如果a为非null就返回true,否则返回false

 
 
回到顶部

字符函数

return type

name(signature)

description

int

ascii(string str)

returns the numeric value of the first  character of str.

返回str中首个ascii字符串的整数值

string

base64(binary bin)

converts the argument from binary to a base 64 string (as of hive 0.12.0)..

将二进制bin转换成64位的字符串

string

concat(string|binary a, string|binary b...)

returns the string or bytes resulting from concatenating the strings or bytes passed in as parameters in order. for example, concat('foo', 'bar') results in 'foobar'. note that this function can take any number of input strings..

对二进制字节码或字符串按次序进行拼接

array<struct<string,double>>

context_ngrams(array<array<string>>, array<string>, int k, int pf)

returns the top-k contextual n-grams from a set of tokenized sentences, given a string of "context". see statisticsanddatamining for more information..

与ngram类似,但context_ngram()允许你预算指定上下文(数组)来去查找子序列,具体看statisticsanddatamining(这里的解释更易懂)

string

concat_ws(string sep, string a, string b...)

like concat() above, but with custom separator sep..

与concat()类似,但使用指定的分隔符喜进行分隔

string

concat_ws(string sep, array<string>)

like concat_ws() above, but taking an array of strings. (as of hive 0.9.0).

拼接array中的元素并用指定分隔符进行分隔

string

decode(binary bin, string charset)

decodes the first argument into a string using the provided character set (one of 'us-ascii', 'iso-8859-1', 'utf-8', 'utf-16be', 'utf-16le', 'utf-16'). if either argument is null, the result will also be null. (as of hive 0.12.0.).

使用指定的字符集charset将二进制值bin解码成字符串,支持的字符集有:'us-ascii', 'iso-8859-1', 'utf-8', 'utf-16be', 'utf-16le', 'utf-16',如果任意输入参数为null都将返回null

binary

encode(string src, string charset)

encodes the first argument into a binary using the provided character set (one of 'us-ascii', 'iso-8859-1', 'utf-8', 'utf-16be', 'utf-16le', 'utf-16'). if either argument is null, the result will also be null. (as of hive 0.12.0.).

使用指定的字符集charset将字符串编码成二进制值,支持的字符集有:'us-ascii', 'iso-8859-1', 'utf-8', 'utf-16be', 'utf-16le', 'utf-16',如果任一输入参数为null都将返回null

int

find_in_set(string str, string strlist)

returns the first occurance of str in strlist where strlist is a comma-delimited string. returns null if either argument is null. returns 0 if the first argument contains any commas. for example, find_in_set('ab', 'abc,b,ab,c,def') returns 3..

返回以逗号分隔的字符串中str出现的位置,如果参数str为逗号或查找失败将返回0,如果任一参数为null将返回null回

string

format_number(number x, int d)

formats the number x to a format like '#,###,###.##', rounded to d decimal places, and returns the result as a string. if d is 0, the result has no decimal point or fractional part. (as of hive 0.10.0; bug with float types fixed in hive 0.14.0, decimal type support added in hive 0.14.0).

将数值x转换成"#,###,###.##"格式字符串,并保留d位小数,如果d为0,将进行四舍五入且不保留小数

string

get_json_object(string json_string, string path)

extracts json object from a json string based on json path specified, and returns json string of the extracted json object. it will return null if the input json string is invalid. note: the json path can only have the characters [0-9a-z_], i.e., no upper-case or special characters. also, the keys *cannot start with numbers.* this is due to restrictions on hive column names..

从指定路径上的json字符串抽取出json对象,并返回这个对象的json格式,如果输入的json是非法的将返回null,注意此路径上json字符串只能由数字 字母 下划线组成且不能有大写字母和特殊字符,且key不能由数字开头,这是由于hive对列名的限制

boolean

in_file(string str, string filename)

returns true if the string str appears as an entire line in filename..

如果文件名为filename的文件中有一行数据与字符串str匹配成功就返回true

int

instr(string str, string substr)

returns the position of the first occurrence of substr in str. returns null if either of the arguments are null and returns 0 if substr could not be found in str. be aware that this is not zero based. the first character in str has index 1..

查找字符串str中子字符串substr出现的位置,如果查找失败将返回0,如果任一参数为null将返回null,注意位置为从1开始的

int

length(string a)

returns the length of the string..

返回字符串的长度

int

locate(string substr, string str[, int pos])

returns the position of the first occurrence of substr in str after position pos..

查找字符串str中的pos位置后字符串substr第一次出现的位置

string

lower(string a) lcase(string a)

returns the string resulting from converting all characters of b to lower case. for example, lower('foobar') results in 'foobar'..

将字符串a的所有字母转换成小写字母

string

lpad(string str, int len, string pad)

returns str, left-padded with pad to a length of len..

从左边开始对字符串str使用字符串pad填充,最终len长度为止,如果字符串str本身长度比len大的话,将去掉多余的部分

string

ltrim(string a)

returns the string resulting from trimming spaces from the beginning(left hand side) of a. for example, ltrim(' foobar ') results in 'foobar '..

去掉字符串a前面的空格

array<struct<string,double>>

ngrams(array<array<string>>, int n, int k, int pf)

returns the top-k n-grams from a set of tokenized sentences, such as those returned by the sentences() udaf. see statisticsanddatamining for more information..

返回出现次数top k的的子序列,n表示子序列的长度,具体看statisticsanddatamining (这里的解释更易懂)

string

parse_url(string urlstring, string parttoextract [, string keytoextract])

returns the specified part from the url. valid values for parttoextract include host, path, query, ref, protocol, authority, file, and userinfo. for example, parse_url('http://facebook.com/path1/p.php?k1=v1&k2=v2#ref1', 'host') returns 'facebook.com'. also a value of a particular key in query can be extracted by providing the key as the third argument, for example, parse_url('http://facebook.com/path1/p.php?k1=v1&k2=v2#ref1', 'query', 'k1') returns 'v1'..

返回从url中抽取指定部分的内容,参数url是url字符串,而参数parttoextract是要抽取的部分,这个参数包含(host, path, query, ref, protocol, authority, file, and userinfo,例如:parse_url('http://facebook.com/path1/p.php?k1=v1&k2=v2#ref1', 'host') ='facebook.com',如果参数parttoextract值为query则必须指定第三个参数key  如:parse_url('http://facebook.com/path1/p.php?k1=v1&k2=v2#ref1', 'query', 'k1') =‘v1’

string

printf(string format, obj... args)

returns the input formatted according do printf-style format strings (as of hive0.9.0)..

按照printf风格格式输出字符串

string

regexp_extract(string subject, string pattern, int index)

returns the string extracted using the pattern. for example, regexp_extract('foothebar', 'foo(.*?)(bar)', 2) returns 'bar.' note that some care is necessary in using predefined character classes: using '\s' as the second argument will match the letter s; '\\s' is necessary to match whitespace, etc. the 'index' parameter is the java regex matcher group() method index. see docs/api/java/util/regex/matcher.html for more information on the 'index' or java regex group() method..

抽取字符串subject中符合正则表达式pattern的第index个部分的子字符串,注意些预定义字符的使用,如第二个参数如果使用'\s'将被匹配到s,'\\s'才是匹配空格

string

regexp_replace(string initial_string, string pattern, string replacement)

returns the string resulting from replacing all substrings in initial_string that match the java regular expression syntax defined in pattern with instances of replacement. for example, regexp_replace("foobar", "oo|ar", "") returns 'fb.' note that some care is necessary in using predefined character classes: using '\s' as the second argument will match the letter s; '\\s' is necessary to match whitespace, etc..

按照java正则表达式pattern将字符串intial_string中符合条件的部分成replacement所指定的字符串,如里replacement这空的话,抽符合正则的部分将被去掉  如:regexp_replace("foobar", "oo|ar", "") = 'fb.' 注意些预定义字符的使用,如第二个参数如果使用'\s'将被匹配到s,'\\s'才是匹配空格

string

repeat(string str, int n)

repeats str n times..

重复输出n次字符串str

string

reverse(string a)

returns the reversed string..

反转字符串

string

rpad(string str, int len, string pad)

returns str, right-padded with pad to a length of len..

从右边开始对字符串str使用字符串pad填充,最终len长度为止,如果字符串str本身长度比len大的话,将去掉多余的部分

string

rtrim(string a)

returns the string resulting from trimming spaces from the end(right hand side) of a. for example, rtrim(' foobar ') results in ' foobar'..

去掉字符串后面出现的空格

array<array<string>>

sentences(string str, string lang, string locale)

tokenizes a string of natural language text into words and sentences, where each sentence is broken at the appropriate sentence boundary and returned as an array of words. the 'lang' and 'locale' are optional arguments. for example, sentences('hello there! how are you?') returns ( ("hello", "there"), ("how", "are", "you") )..

字符串str将被转换成单词数组,如:sentences('hello there! how are you?') =( ("hello", "there"), ("how", "are", "you") )

string

space(int n)

returns a string of n spaces..

返回n个空格

array

split(string str, string pat)

splits str around pat (pat is a regular expression)..

按照正则表达式pat来分割字符串str,并将分割后的数组字符串的形式返回

map<string,string>

str_to_map(text[, delimiter1, delimiter2])

splits text into key-value pairs using two delimiters. delimiter1 separates text into k-v pairs, and delimiter2 splits each k-v pair. default delimiters are ',' for delimiter1 and '=' for delimiter2..

将字符串str按照指定分隔符转换成map,第一个参数是需要转换字符串,第二个参数是键值对之间的分隔符,默认为逗号;第三个参数是键值之间的分隔符,默认为"="

string

substr(string|binary a, int start) substring(string|binary a, int start)

returns the substring or slice of the byte array of a starting from start position till the end of string a. for example, substr('foobar', 4) results in 'bar' (see [])..

对于字符串a,从start位置开始截取字符串并返回

string

substr(string|binary a, int start, int len) substring(string|binary a, int start, int len)

returns the substring or slice of the byte array of a starting from start position with length len. for example, substr('foobar', 4, 1) results in 'b' (see [])..

对于二进制/字符串a,从start位置开始截取长度为length的字符串并返回

string substring_index(string a, string delim, int count) returns the substring from string a before count occurrences of the delimiter delim (as of hive 1.3.0). if count is positive, everything to the left of the final delimiter (counting from the left) is returned. if count is negative, everything to the right of the final delimiter (counting from the right) is returned. substring_index performs a case-sensitive match when searching for delim. example: substring_index('www.apache.org', '.', 2) = 'www.apache'..

截取第count分隔符之前的字符串,如count为正则从左边开始截取,如果为负则从右边开始截取

string

translate(string|char|varchar input, string|char|varchar from, string|char|varchar to)

translates the input string by replacing the characters present in the from string with the corresponding characters in the to string. this is similar to the translatefunction in postgresql. if any of the parameters to this udf are null, the result is null as well. (available as of hive 0.10.0, for string types)

char/varchar support added as of hive 0.14.0..

将input出现在from中的字符串替换成to中的字符串 如:translate("mobin","bin","m")="mom"

string

trim(string a)

returns the string resulting from trimming spaces from both ends of a. for example, trim(' foobar ') results in 'foobar'.

将字符串a前后出现的空格去掉

binary

unbase64(string str)

converts the argument from a base 64 string to binary. (as of hive 0.12.0.).

将64位的字符串转换二进制值

string

upper(string a) ucase(string a)

returns the string resulting from converting all characters of a to upper case. for example, upper('foobar') results in 'foobar'..

将字符串a中的字母转换成大写字母

string initcap(string a) returns string, with the first letter of each word in uppercase, all other letters in lowercase. words are delimited by whitespace. (as of hive 1.1.0.).

将字符串a转换第一个字母大写其余字母的字符串

int levenshtein(string a, string b) returns the levenshtein distance between two strings (as of hive 1.2.0). for example, levenshtein('kitten', 'sitting') results in 3..

计算两个字符串之间的差异大小  如:levenshtein('kitten', 'sitting') = 3

string soundex(string a) returns soundex code of the string (as of hive 1.2.0). for example, soundex('miller') results in m460..

将普通字符串转换成soundex字符串

 
 
回到顶部

聚合函数

return type

name(signature)

description

bigint

count(*), count(expr), count(distinct expr[, expr...])

count(*) - returns the total number of retrieved rows, including rows containing null values.

统计总行数,包括含有null值的行

count(expr) - returns the number of rows for which the supplied expression is non-null.

统计提供非null的expr表达式值的行数

count(distinct expr[, expr]) - returns the number of rows for which the supplied expression(s) are unique and non-null. execution of this can be optimized with hive.optimize.distinct.rewrite.

统计提供非null且去重后的expr表达式值的行数

double

sum(col), sum(distinct col)

returns the sum of the elements in the group or the sum of the distinct values of the column in the group.

sum(col),表示求指定列的和,sum(distinct col)表示求去重后的列的和

double

avg(col), avg(distinct col)

returns the average of the elements in the group or the average of the distinct values of the column in the group.

avg(col),表示求指定列的平均值,avg(distinct col)表示求去重后的列的平均值

double

min(col)

returns the minimum of the column in the group.

求指定列的最小值

double

max(col)

returns the maximum value of the column in the group.

求指定列的最大值

double

variance(col), var_pop(col)

returns the variance of a numeric column in the group.

求指定列数值的方差

double

var_samp(col)

returns the unbiased sample variance of a numeric column in the group.

求指定列数值的样本方差

double

stddev_pop(col)

returns the standard deviation of a numeric column in the group.

求指定列数值的标准偏差

double

stddev_samp(col)

returns the unbiased sample standard deviation of a numeric column in the group.

求指定列数值的样本标准偏差

double

covar_pop(col1, col2)

returns the population covariance of a pair of numeric columns in the group.

求指定列数值的协方差

double

covar_samp(col1, col2)

returns the sample covariance of a pair of a numeric columns in the group.

求指定列数值的样本协方差

double

corr(col1, col2)

returns the pearson coefficient of correlation of a pair of a numeric columns in the group.

返回两列数值的相关系数

double

 percentile(bigint col, p)

returns the exact pth percentile of a column in the group (does not work with floating point types). p must be between 0 and 1. note: a true percentile can only be computed for integer values. use percentile_approx if your input is non-integral.

返回col的p%分位数

 
 
回到顶部

表生成函数

return type

name(signature)

description

array type

explode(array<type> a)

for each element in a, generates a row containing that element.

对于a中的每个元素,将生成一行且包含该元素

n rows

explode(array)

returns one row for each element from the array..

每行对应数组中的一个元素

n rows

explode(map)

returns one row for each key-value pair from the input map with two columns in each row: one for the key and another for the value. (as of hive 0.8.0.).

每行对应每个map键-值,其中一个字段是map的键,另一个字段是map的值

n rows

posexplode(array)

behaves like explode for arrays, but includes the position of items in the original array by returning a tuple of (pos, value). (as of hive 0.13.0.).

与explode类似,不同的是还返回各元素在数组中的位置

n rows

stack(int n, v_1, v_2, ..., v_k)

breaks up v_1, ..., v_k into n rows. each row will have k/n columns. n must be constant..

把m列转换成n行,每行有m/n个字段,其中n必须是个常数

tuple

json_tuple(jsonstr, k1, k2, ...)

takes a set of names (keys) and a json string, and returns a tuple of values. this is a more efficient version of the get_json_object udf because it can get multiple keys with just one call..

从一个json字符串中获取多个键并作为一个元组返回,与get_json_object不同的是此函数能一次获取多个键值

tuple

parse_url_tuple(url, p1, p2, ...)

this is similar to the parse_url() udf but can extract multiple parts at once out of a url. valid part names are: host, path, query, ref, protocol, authority, file, userinfo, query:<key>..

返回从url中抽取指定n部分的内容,参数url是url字符串,而参数p1,p2,....是要抽取的部分,这个参数包含host, path, query, ref, protocol, authority, file, userinfo, query:<key>

 

inline(array<struct[,struct]>)

explodes an array of structs into a table. (as of hive 0.10.).

将结构体数组提取出来并插入到表中