0
点赞
收藏
分享

微信扫一扫

hive函数大全:数值与数学相关函数


hive函数官网文档:hive函数官网文档

1.Hive所有内嵌数学函数


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 ​​​​​​​​​​http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_bin​​).

计算二进制a的STRING类型,a为BIGINT类型



STRING



hex(BIGINT a) hex(STRING a) hex(BINARY a)



If the argument is an ​​INT​​ or ​​binary​​, ​​hex​​ 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​​​​​​​​​​http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_hex​​, ​​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 ​​​​​​​​​​http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_conv​​).

将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​​).

求最小值

2.常用数值计算函数演示

1. 取整函数: round

语法: round(double a)

返回值: BIGINT

说明: ​返回double类型的整数值部分 (遵循四舍五入)

举例:

hive> select round(3.1415926) from t_fin_demo;

         3

hive> select round(3.5) from t_fin_demo;

         4

hive> create table t_fin_demo as select round(9542.158) from t_fin_demo;

hive> describe t_fin_demo;

         _c0     bigint


2. 指定精度取整函数: round

语法: round(double a, int d)

返回值: DOUBLE

说明: 返回指定精度d的double类型

举例:

hive> select round(3.1415926,4) from t_fin_demo;

         3.1416

3. 向下取整函数: floor

语法: floor(double a)

返回值: BIGINT

说明: 返回等于或者小于该double变量的最大的整数

hive> select floor(3.1415926) from t_fin_demo;
3
hive> select floor(25) from t_fin_demo;
25

hive> select floor(-25.1233) from t_fin_demo;

-26

4. 向上取整函数: ceil,与ceiling一样

语法: ceil(double a)

返回值: BIGINT

说明: ​返回等于或者大于该double变量的最小的整数

举例:

hive> select ceil(3.1415926) from t_fin_demo;
4
hive> select ceil(46) from t_fin_demo;
46

hive> select ceil(-25.1233) from t_fin_demo;

-25

5. 向上取整函数: ceiling

语法: ceiling(double a)

返回值: BIGINT

说明: 与ceil功能相同

举例:

hive> select ceiling(3.1415926) from t_fin_demo;

        4

hive> select ceiling(46) from t_fin_demo;

        46

6. 取随机数函数: rand

语法: rand(),rand(int seed)

返回值: double

说明: 返回一个0到1范围内的随机数。如果指定种子seed,则会等到一个稳定的随机数序列

举例:

hive> select rand() from t_fin_demo;

        0.5577432776034763

hive> select rand() from t_fin_demo;

        0.6638336467363424

hive> select rand(100) from t_fin_demo;

        0.7220096548596434

hive> select rand(100) from t_fin_demo;

        0.7220096548596434

7. 自然指数函数: exp

语法: exp(double a)

返回值: double

说明: 返回自然对数e的a次方

举例:

hive> select exp(2) from t_fin_demo;

        7.38905609893065

自然对数函数: ln

语法: ln(double a)

返回值: double

说明: 返回a的自然对数

举例:

hive> select ln(7.38905609893065) from t_fin_demo;

        2.0

9. 以2为底对数函数: log2

语法: log2(double a)

返回值: double

说明: 返回以2为底的a的对数

举例:

hive> select log2(8) from t_fin_demo;

        3.0

10. 对数函数: log

语法: log(double base, double a)

返回值: double

说明: 返回以base为底的a的对数

举例:

hive> select log(4,256) from t_fin_demo;

        4.0

11. 幂运算函数: pow

语法: pow(double a, double p)

返回值: double

说明: 返回a的p次幂

举例:

hive> select pow(2,4) from t_fin_demo;

        16.0

12. 幂运算函数: power

语法: power(double a, double p)

返回值: double

说明: 返回a的p次幂,与pow功能相同

举例:

hive> select power(2,4) from t_fin_demo;

        16.0

13. 开平方函数: sqrt

语法: sqrt(double a)

返回值: double

说明: 返回a的平方根

举例:

hive> select sqrt(16) from lxw_dual;

        4.0

14. 二进制函数: bin

语法: bin(BIGINT a)

返回值: string

说明: 返回a的二进制代码表示

举例:

hive> select bin(7) from t_fin_demo;

        111

15. 十六进制函数: hex

语法: hex(BIGINT a)

返回值: string

说明: 如果变量是int类型,那么返回a的十六进制表示;如果变量是string类型,则返回该字符串的十六进制表示

举例:

hive> select hex(17) from t_fin_demo;

        11

hive> select hex(‘abc’) from t_fin_demo;

        616263

16. 反转十六进制函数: unhex

语法: unhex(string a)

返回值: string

说明: 返回该十六进制字符串所代码的字符串

举例:

hive> select unhex(‘616263’) from t_fin_demo;

abc

hive> select unhex(‘11’) from t_fin_demo;

-

hive> select unhex(616263) from t_fin_demo;

abc


17. 进制转换函数: conv

语法: conv(BIGINT num, int from_base, int to_base)

返回值: string

说明: 将数值num从from_base进制转化到to_base进制

举例:

hive> select conv(17,10,16) from t_fin_demo;

        11

hive> select conv(17,10,2) from t_fin_demo;

        10001


18. 绝对值函数: abs

语法: abs(double a)   abs(int a)

返回值: double        int

说明: 返回数值a的绝对值

举例:

hive> select abs(-3.9) from lxw_dual;

3.9

hive> select abs(10.9) from t_fin_demo;

10.9


19. negative函数: negative

语法: negative(int a), negative(double a)

返回值: int double

说明: 返回-a,取负数

举例:

hive> select negative(-5) from t_fin_demo;

        5


举报

相关推荐

0 条评论