hive 常用函数整理 4.数值计算


1. 取整函数: round

语法: round(double a)

返回值: BIGINT

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

举例:

hive> select round(3.222511658) from student;

3.0

hive> select round(3.722511658) from student;

4.0

hive> create table student4 as select round(321.456) from student;

hive> desc student4;

OK

_c0                         double

Time taken: 0.397 seconds, Fetched: 1 row(s)

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

语法: round(double a, int d)

返回值: DOUBLE

说明: 返回指定精度d的double类型,保留几位小数

举例:

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

3.1416

3. 向下取整函数: floor

语法: floor(double a)

返回值: BIGINT

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

举例:

hive> select floor(3.1415926) from student;

3

hive>  select floor(3.9415926) from student;

3

hive> select floor(25) from student;

25

4. 向上取整函数: ceil

语法: ceil(double a)

返回值: BIGINT

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

举例:

hive> select ceil(3.1415926) from student;

4

hive> select ceil(46) from student;

46

hive> select ceil(5.00001) from student;

6

hive> select ceil(5.9) from student;

6

5. 向上取整函数: ceiling

语法: ceiling(double a)

返回值: BIGINT

说明: 与ceil功能相同

举例:

hive> select ceiling(3.1415926) from student;

4

hive> select ceiling(46) from student;

46

hive> select ceiling(5.00001) from student;

6

hive> select ceiling(5.9) from student;

6

6. 取随机数函数: rand

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

返回值: double

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

举例:

hive> select rand() from student;

0.5802855899437019

hive> select rand() from student;

0.5577432776034763

hive> select rand() from student;

0.6638336467363424

hive> select rand(100) from student;

0.7220096548596434

hive> select rand(100) from student;

0.7220096548596434

hive> select rand(100) from student;

0.7220096548596434

7. 自然指数函数: exp

语法: exp(double a)

返回值: double

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

举例:

hive> select exp(1) from student;

2.718281828459045

hive> select exp(2) from student;

7.38905609893065

自然对数函数: ln

语法: ln(double a)

返回值: double

说明: 返回a的自然对数

举例:
​```sql
hive> select ln(7.38905609893065) from student;

2.0

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

语法: log10(double a)

返回值: double

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

举例:

hive> select log10(100) from student;

2.0

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

语法: log2(double a)

返回值: double

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

举例:

hive> select log2(8) from student;

3.0

10. 对数函数: log

语法: log(double base, double a)

返回值: double

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

举例:

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

4.0

11. 幂运算函数: pow

语法: pow(double a, double p)

返回值: double

说明: 返回a的p次幂

举例:

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

16.0

12. 幂运算函数: power

语法: power(double a, double p)

返回值: double

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

举例:

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

16.0

13. 开平方函数: sqrt

语法: sqrt(double a)

返回值: double

说明: 返回a的平方根

举例:

hive> select sqrt(16) from student;

4.0

14. 二进制函数: bin

语法: bin(BIGINT a)

返回值: string

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

举例:

hive> select bin(7) from student;

111

hive> select bin(51) from student;

110011

15. 十六进制函数: hex

语法: hex(BIGINT a)

返回值: string

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

举例:

hive> select hex(17) from student;

11

hive> select hex(abc) from student;

616263

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

语法: unhex(string a)

返回值: string

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

举例:

hive> select unhex(616263) from student;

abc

hive> select unhex(11) from student;

–

hive> select unhex(616263) from student;

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 student;

11

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

10001

18. 绝对值函数: abs

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

返回值: double int

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

举例:

hive> select abs(-3.9) from student;

3.9

hive> select abs(10.9) from student;

10.9

19. 正取余函数: pmod

语法: pmod(int a, int b),pmod(double a, double b)

返回值: int double

说明: 返回正的a除以b的余数

举例:

hive> select pmod(9,4) from student;

1

hive> select pmod(-9,4) from student;

3

20. 正弦函数: sin

语法: sin(double a)

返回值: double

说明: 返回a的正弦值

举例:

hive> select sin(0.8) from student;

0.7173560908995228

21. 反正弦函数: asin

语法: asin(double a)

返回值: double

说明: 返回a的反正弦值

举例:

hive> select asin(0.7173560908995228) from student;

0.8

22. 余弦函数: cos

语法: cos(double a)

返回值: double

说明: 返回a的余弦值

举例:

hive> select cos(0.9) from student;

0.6216099682706644

23. 反余弦函数: acos

语法: acos(double a)

返回值: double

说明: 返回a的反余弦值

举例:

hive> select acos(0.6216099682706644) from student;

0.9

24. positive函数: positive

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

返回值: int double

说明: 返回a

举例:

hive> select positive(-10) from student;

-10

hive> select positive(12) from student;

12

25. negative函数: negative

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

返回值: int double

说明: 返回-a

举例:

hive> select negative(-5) from student;

5

hive> select negative(8) from student;

-8

文章作者: hnbian
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 hnbian !
评论
  目录