Hive数值计算函数汇总

x33g5p2x  于2020-07-24 发布在 Hive  
字(3.4k)|赞(0)|评价(0)|浏览(1065)

1. 取整函数: round

语法: round(double a)
返回值: BIGINT
说明: 返回 double 类型的整数值部分 (遵循四舍五入)
举例:

hive> select round(3.1415926) from test;
3
hive> select round(3.5) from test;
4
hive> create table test as select round(9542.158) from test;
hive> describe test;
_c0 bigint

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

语法: round(double a, int d)
返回值: DOUBLE
说明: 返回指定精度 d 的 double 类型
举例:

hive> select round(3.1415926,4) from test;
3.1416

3. 向下取整函数: floor

语法: floor(double a)
返回值: BIGINT
说明: 返回等于或者小于该 double 变量的最大的整数
举例:

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

4. 向上取整函数: ceil

语法: ceil(double a)
返回值: BIGINT
说明: 返回等于或者大于该 double 变量的最小的整数
举例:

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

5. 向上取整函数: ceiling

语法: ceiling(double a)
返回值: BIGINT
说明: 与 ceil 功能相同
举例:

hive> select ceiling(3.1415926) from test;
4
hive> select ceiling(46) from test;
46

6. 取随机数函数: rand

语法: rand(),rand(int seed)
返回值: double
说明: 返回一个 0 到 1 范围内的随机数。如果指定种子 seed,则会等到一个稳定的随机数序列
举例:

hive> select rand() from test;
0.5577432776034763
hive> select rand() from test;
0.6638336467363424
hive> select rand(100) from test;
0.7220096548596434
hive> select rand(100) from test;
0.7220096548596434

7. 自然指数函数: exp

语法: exp(double a)
返回值: double
说明: 返回自然对数 e 的 a 次方
举例:

hive> select exp(2) from test;
7.38905609893065

8. 自然对数函数: ln

语法: ln(double a)
返回值: double
说明: 返回 a 的自然对数
举例:

hive> select ln(7.38905609893065) from test;
2.0

9. 以 10 为底对数函数: log10

语法: log10(double a)
返回值: double
说明: 返回以 10 为底的 a 的对数
举例:

hive> select log10(100) from test;
2.0
19

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

语法: log2(double a)
返回值: double
说明: 返回以 2 为底的 a 的对数
举例:

hive> select log2(8) from test;
3.0

11. 对数函数: log

语法: log(double base, double a)
返回值: double
说明: 返回以 base 为底的 a 的对数
举例:

hive> select log(4,256) from test;
4.0

12. 幂运算函数: pow

语法: pow(double a, double p)
返回值: double
说明: 返回 a 的 p 次幂
举例:

hive> select pow(2,4) from test;
16.0

13. 幂运算函数: power

语法: power(double a, double p)
返回值: double
说明: 返回 a 的 p 次幂,与 pow 功能相同
举例:

hive> select power(2,4) from test;
16.0

14. 开平方函数: sqrt

语法: sqrt(double a)
返回值: double
说明: 返回 a 的平方根
举例:

hive> select sqrt(16) from test;
4.0

15. 二进制函数: bin

语法: bin(BIGINT a)
返回值: string
说明: 返回 a 的二进制代码表示
举例:

hive> select bin(7) from test;
111

16. 十六进制函数: hex

语法: hex(BIGINT a)
返回值: string
说明: 如果变量是 int 类型,那么返回 a 的十六进制表示;如果变量是 string 类型,则返回该字符串的十六进制表示
举例:

hive> select hex(17) from test;
11
hive> select hex(‘abc’) from test;
616263

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

语法: unhex(string a)
返回值: string
说明: 返回该十六进制字符串所代码的字符串
举例:

hive> select unhex(‘616263’) from test;
abc
hive> select unhex(‘11’) from test;
-
hive> select unhex(616263) from test;
abc

18. 进制转换函数: conv

语法: conv(BIGINT num, int from_base, int to_base)
返回值: string
说明: 将数值 num 从 from_base 进制转化到 to_base 进制
举例:

hive> select conv(17,10,16) from test;
11
hive> select conv(17,10,2) from test;
10001

19. 绝对值函数: abs

语法: abs(double a) abs(int a)
返回值: double int
说明: 返回数值 a 的绝对值
举例:

hive> select abs(-3.9) from test;
3.9
hive> select abs(10.9) from test;
10.9

20. 正取余函数: pmod

语法: pmod(int a, int b),pmod(double a, double b)
返回值: int double
说明: 返回正的 a 除以 b 的余数
举例:

hive> select pmod(9,4) from test;
1
hive> select pmod(-9,4) from test;
3

21. 正弦函数: sin

语法: sin(double a)
返回值: double
说明: 返回 a 的正弦值
举例:

hive> select sin(0.8) from test;
0.7173560908995228

22. 反正弦函数: asin

语法: asin(double a)
返回值: double
说明: 返回 a 的反正弦值
举例:

hive> select asin(0.7173560908995228) from test;
0.8

23. 余弦函数: cos

语法: cos(double a)
返回值: double
说明: 返回 a 的余弦值
举例:

hive> select cos(0.9) from test;
0.6216099682706644

24. 反余弦函数: acos

语法: acos(double a)
返回值: double
说明: 返回 a 的反余弦值
举例:

hive> select acos(0.6216099682706644) from test;
0.9

25. positive 函数: positive

语法: positive(int a), positive(double a)
返回值: int double
说明: 返回 a
举例:

hive> select positive(-10) from test;
-10
hive> select positive(12) from test;
12

26. negative 函数: negative

语法: negative(int a), negative(double a)
返回值: int double
说明: 返回-a
举例:

hive> select negative(-5) from test;
5
hive> select negative(8) from test;
-8

相关文章