数学函数
下面表格中的函数为Hive的内置数学函数,大部分当参数为NULL时返回NULL。
返回类型 | 函数名 | 描述 |
| round(double a) | 返回a经过四舍五入的 |
double | round(double a, int d) | 返回a经过四舍五入,保留d位小数的DOUBLE 值 |
bigint | floor(double a) | 返回小于等于a的最大BIGINT 值 |
bigint | ceil(double a), ceiling(double a) | 返回大于等于a的最小BIGINT 值 |
double | rand(), rand(int seed) | 返回一个均匀分布在0到1之间的随机数,指定seed将会确保产生的随机数序列是确定的 |
double | exp(double a), exp(decimal a) | 返回ea |
double | ln(double a), ln(decimal a) | 返回a的自然对数值 |
double | log10(double a), log10(decimal a) | 返回a的以10为底的对数值 |
double | log2(double a), log2(decimal a) | 返回a的以2为底的对数值 |
double | log(double base, double a) | 返回a以base为底的对数值,除了DOUBLE 类型,还可以是DECIMAL 类型,并且两种类型可以混合使用 |
double | pow(double a, double p), power(double a, double p) | 返回ap |
double | sqrt(double a), sqrt(decimal a) | 返回a的平方根 |
string | bin(bigint a) | 返回a的二进制格式 |
string | hex(bigint a) hex(string a) hex(binary a) | 如果参数为整数或者二进制,返回十六进制的字符串,如果参数为字符串,该函数将每个字符转换为十六进制表示,并返回结果字符串 |
binary | unhex(string a) | 将每对字符解释为十六进制并转换为该十六进制的字节表示 |
string | conv(bigint num, int from_base, int to_base), conv(string num, int from_base, int to_base) | 将数值从一种进制表示转换为另一种进制,比如从二进制转换为十进制 |
double | abs(double a) | 返回a的绝对值 |
int or double | pmod(int a, int b), pmod(double a, double b) | 返回a mod b的正数值 |
double | sin(double a), sin(decimal a) | 返回a的正弦值,a为弧度值 |
double | asin(double a), asin(decimal a) | 如果1<=a<=-1,返回a的反正弦值,否则返回NULL |
double | cos(double a), cos(decimal a) | 返回a的余弦值,a为弧度值 |
double | acos(double a), acos(decimal a) | 如果-1<=a<=1,返回a的反余弦值,否则返回NULL |
double | tan(double a), tan(decimal a) | 返回a的正切值,a为弧度值 |
double | atan(double a), atan(decimal a) | 返回a的反正切值 |
double | degrees(double a), degrees(decimal a) | 将a从弧度值转换为对应的角度值 |
double | radians(double a), radians(double a) | 将a从角度值转换为弧度值 |
int or double | positive(int a), positive(double a) | 返回a |
int or double | negative(int a), negative(double a) | 返回-a |
double or int | sign(double a), sign(decimal a) | 如果a为正数返回1.0,若为负数返回-1.0,否则返回0.0。参数为DECIMAL 时返回值为INT类型 |
double | e() | 返回e的值 |
double | pi() | 返回π的值 |
集合函数
下表为Hive中的集合函数。
返回类型 | 函数名 | 描述 |
int | size(Map<K.V>) | 返回map中的元素数量 |
int | size(Array<T>) | 返回数组中的元素数量 |
array<K> | map_keys(Map<K.V>) | 返回未经排序的包含map中keys的数组 |
array<V> | map_values(Map<K.V>) | 返回未经排序的包含map中values的数组 |
boolean | array_contains(Array<T>, value) | 如果数组包含value,返回true |
array<t> | sort_array(Array<T>) | 将数组按照升序排序并返回 |
类型转换函数
下表为Hive中的类型转换函数。
返回类型 | 函数名 | 描述 |
binary | binary(string|binary) | 将输入参数转换为binary |
<type> | cast(expr as <type>) | 将表达式的结果expr转换为<type>,如果转换不成功则返回NULL,例如cast(‘2014-08-09’ as date) |
日期函数
下表中为Hive中的内置日期函数。
返回类型 | 函数名 | 描述 |
string | from_unixtime(bigint unixtime[, string format]) | 将从unix时间(1970-01-01 00:00:00 UTC)开始的秒数转换为表示当前系统时区的时间戳,格式为“1970-01-01 00:00:00” |
bigint | unix_timestamp() | 返回当前Unix时间戳表示的秒数 |
bigint | unix_timestamp(string date) | 将格式为yyyy-MM-dd HH:mm:ss的字符串转换为Unix时间戳(秒),使用默认的时区和区域,如果失败返回0 |
bigint | unix_timestamp(string date, string pattern) | 根据指定模式将时间字符串转换为Unix时间戳(秒),失败返回0 |
string | to_date(string timestamp) | 返回时间戳字符串的日期部分,如:to_date("1970-01-01 00:00:00") = "1970-01-01" |
int | year(string date) | 返回日期或时间戳字符串的年部分,如: year("1970-01-01 00:00:00") = 1970, year("1970-01-01") = 1970 |
int | month(string date) | 返回日期或时间戳字符串的月部分,如: month("1970-11-01 00:00:00") = 11, month("1970-11-01") = 11 |
int | day(string date) dayofmonth(date) | 返回日期或时间戳字符串的天部分,如:day("1970-11-01 00:00:00") = 1, day("1970-11-01") = 1 |
int | hour(string date) | 返回时间戳的小时部分: hour('2009-07-30 12:58:59') = 12, hour('12:58:59') = 12 |
int | minute(string date) | 返回时间戳的分钟 |
int | second(string date) | 返回时间戳的秒 |
int | weekofyear(string date) | 返回时间戳的星期数,如: weekofyear("1970-11-01 00:00:00") = 44, weekofyear("1970-11-01") = 44 |
int | datediff(string enddate, string startdate) | 返回从startdate到enddate的天数,如: datediff('2009-03-01', '2009-02-27') = 2 |
string | date_add(string startdate, int days) | 向startdate增加指定的天数,如: date_add('2008-12-31', 1) = '2009-01-01' |
string | date_sub(string startdate, int days) | 从startdate减去指定的天数,如: date_sub('2008-12-31', 1) = '2008-12-30' |
timestamp | from_utc_timestamp(timestamp, string timezone) | 假设给定时间戳为UTC时间,并将其转换为给定时区的时间戳 |
timestamp | to_utc_timestamp(timestamp, string timezone) | 假设给定时间戳为给定时区的时间,将其转换为UTC时间戳 |