Hive常用函数大全

小编 2026-06-30 阅读:166 评论:0
文章目录 1 关系运算 1.1 1、等值比较: = 1.2 2、不等值比较: 1.3 3、小于比较: < 1.4 4、小于等于比较: <= 1.5 5、大于比较: >...

文章目录

关系运算

1、等值比较: =

语法:A=B
操作类型:所有基本类型
描述: 如果表达式A与表达式B相等,则为TRUE;否则为FALSE

hive> select 1 from iteblog where 1=1;

1

2、不等值比较: <>

语法: A <> B
操作类型: 所有基本类型
描述: 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A与表达式B不相等,则为TRUE;否则为FALSE

hive> select 1 from iteblog where 1 <> 2;

1

3、小于比较: <

语法: A < B
操作类型:所有基本类型
描述: 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A小于表达式B,则为TRUE;否则为FALSE

hive> select 1 from iteblog where 1 < 2;

1

4、小于等于比较: <=

语法: A <= B
操作类型: 所有基本类型
描述: 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A小于或者等于表达式B,则为TRUE;否则为FALSE

hive> select 1 from iteblog where 1 < = 1;

1

5、大于比较: >

语法: A > B
操作类型: 所有基本类型
描述: 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A大于表达式B,则为TRUE;否则为FALSE

hive> select 1 from iteblog where 2 > 1;

1

6、大于等于比较: >=

语法: A >= B
操作类型: 所有基本类型
描述: 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A大于或者等于表达式B,则为TRUE;否则为FALSE

hive> select 1 from iteblog where 1 >= 1;

1

注意:String的比较要注意(常用的时间比较可以先 to_date 之后再比较)

hive> select * from iteblog;

OK

2011111209 00:00:00     2011111209

 

hive> select a, b, a<b, a>b, a=b from iteblog;

2011111209 00:00:00     2011111209      false   true    false

7、空值判断: IS NULL

语法: A IS NULL
操作类型: 所有类型
描述: 如果表达式A的值为NULL,则为TRUE;否则为FALSE

hive> select 1 from iteblog where null is null;

1

8、非空判断: IS NOT NULL

语法: A IS NOT NULL
操作类型: 所有类型
描述: 如果表达式A的值为NULL,则为FALSE;否则为TRUE

hive> select 1 from iteblog where 1 is not null;

1

9、LIKE比较: LIKE

语法: A LIKE B
操作类型: strings
描述: 如果字符串A或者字符串B为NULL,则返回NULL;如果字符串A符合表达式B 的正则语法,则为TRUE;否则为FALSE。B中字符”_”表示任意单个字符,而字符”%”表示任意数量的字符。

hive> select 1 from iteblog where \'football\' like \'foot%\';

1

hive> select 1 from iteblog where \'football\' like \'foot____\';

1

<strong>注意:否定比较时候用NOT A LIKE B</strong>

hive> select 1 from iteblog where NOT \'football\' like \'fff%\';

1

10、JAVA的LIKE操作: RLIKE

语法: A RLIKE B
操作类型: strings
描述: 如果字符串A或者字符串B为NULL,则返回NULL;如果字符串A符合JAVA正则表达式B的正则语法,则为TRUE;否则为FALSE。

hive> select 1 from iteblog where \'footbar’ rlike \'^f.*r$’;

1

注意:判断一个字符串是否全为数字:

hive>select 1 from iteblog where \'123456\' rlike \'^\\\\d+$\';

1

hive> select 1 from iteblog where \'123456aa\' rlike \'^\\\\d+$\';

11、REGEXP操作: REGEXP

语法: A REGEXP B
操作类型: strings
描述: 功能与RLIKE相同

hive> select 1 from iteblog where \'footbar\' REGEXP \'^f.*r$\';

1

数学运算:

1、加法操作: +

语法: A + B
操作类型:所有数值类型
说明:返回A与B相加的结果。结果的数值类型等于A的类型和B的类型的最小父类型(详见数据类型的继承关系)。比如,int + int 一般结果为int类型,而 int + double 一般结果为double类型

hive> select 1 + 9 from iteblog;

10

hive> create table iteblog as select 1 + 1.2 from iteblog;

hive> describe iteblog;

_c0     double

2、减法操作: -

语法: A – B
操作类型:所有数值类型
说明:返回A与B相减的结果。结果的数值类型等于A的类型和B的类型的最小父类型(详见数据类型的继承关系)。比如,int – int 一般结果为int类型,而 int – double 一般结果为double类型

hive> select 10 – 5 from iteblog;

5

hive> create table iteblog as select 5.6 – 4 from iteblog;

hive> describe iteblog;

_c0     double

3、乘法操作: *

语法: A * B
操作类型:所有数值类型
说明:返回A与B相乘的结果。结果的数值类型等于A的类型和B的类型的最小父类型(详见数据类型的继承关系)。注意,如果A乘以B的结果超过默认结果类型的数值范围,则需要通过cast将结果转换成范围更大的数值类型

hive> select 40 * 5 from iteblog;

200

4、除法操作: /

语法: A / B
操作类型:所有数值类型
说明:返回A除以B的结果。结果的数值类型为double

hive> select 40 / 5 from iteblog;

8.0

注意:hive中最高精度的数据类型是double,只精确到小数点后16位,在做除法运算的时候要特别注意

hive>select ceil(28.0/6.999999999999999999999) from iteblog limit 1;   

结果为4

hive>select ceil(28.0/6.99999999999999) from iteblog limit 1;          

结果为5

5、取余操作: %

语法: A % B
操作类型:所有数值类型
说明:返回A除以B的余数。结果的数值类型等于A的类型和B的类型的最小父类型(详见数据类型的继承关系)。

hive> select 41 % 5 from iteblog;

1

hive> select 8.4 % 4 from iteblog;

0.40000000000000036

<strong>注意</strong>:精度在hive中是个很大的问题,类似这样的操作最好通过round指定精度

hive> select round(8.4 % 4 , 2) from iteblog;

0.4

6、位与操作: &

语法: A & B
操作类型:所有数值类型
说明:返回A和B按位进行与操作的结果。结果的数值类型等于A的类型和B的类型的最小父类型(详见数据类型的继承关系)。

hive> select 4 & 8 from iteblog;

0

hive> select 6 & 4 from iteblog;

4

7、位或操作: |

语法: A | B
操作类型:所有数值类型
说明:返回A和B按位进行或操作的结果。结果的数值类型等于A的类型和B的类型的最小父类型(详见数据类型的继承关系)。

hive> select 4 | 8 from iteblog;

12

hive> select 6 | 8 from iteblog;

14

8、位异或操作: ^

语法: A ^ B
操作类型:所有数值类型
说明:返回A和B按位进行异或操作的结果。结果的数值类型等于A的类型和B的类型的最小父类型(详见数据类型的继承关系)。

hive> select 4 ^ 8 from iteblog;

12

hive> select 6 ^ 4 from iteblog;

2

9.位取反操作: ~

语法: ~A
操作类型:所有数值类型
说明:返回A按位取反操作的结果。结果的数值类型等于A的类型。

hive> select ~6 from iteblog;

-7

hive> select ~4 from iteblog;

-5

逻辑运算:

1、逻辑与操作: AND

语法: A AND B
操作类型:boolean
说明:如果A和B均为TRUE,则为TRUE;否则为FALSE。如果A为NULL或B为NULL,则为NULL

hive> select 1 from iteblog where 1=1 and 2=2;

1

2、逻辑或操作: OR

语法: A OR B
操作类型:boolean
说明:如果A为TRUE,或者B为TRUE,或者A和B均为TRUE,则为TRUE;否则为FALSE

hive> select 1 from iteblog where 1=2 or 2=2;

1

3、逻辑非操作: NOT

语法: NOT A
操作类型:boolean
说明:如果A为FALSE,或者A为NULL,则为TRUE;否则为FALSE

hive> select 1 from iteblog where not 1=2;

1

数值计算

1、取整函数: round

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

hive> select round(3.1415926) from iteblog;

3

hive> select round(3.5) from iteblog;

4

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

hive> describe iteblog;

_c0     bigint

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

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

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

3.1416

3、向下取整函数: floor

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

hive> select floor(3.1415926) from iteblog;

3

hive> select floor(25) from iteblog;

25

4、向上取整函数: ceil

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

hive> select ceil(3.1415926) from iteblog;

4

hive> select ceil(46) from iteblog;

46

5、向上取整函数: ceiling

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

hive> select ceiling(3.1415926) from iteblog;

4

hive> select ceiling(46) from iteblog;

46

6、取随机数函数: rand

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

hive> select rand() from iteblog;

0.5577432776034763

hive> select rand() from iteblog;

0.6638336467363424

hive> select rand(100) from iteblog;

0.7220096548596434

hive> select rand(100) from iteblog;

0.7220096548596434

7、自然指数函数: exp

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

hive> select exp(2) from iteblog;

7.38905609893065

<strong>自然对数函数</strong>: ln

<strong>语法</strong>: ln(double a)

<strong>返回值</strong>: double

<strong>说明</strong>: 返回a的自然对数

1

hive> select ln(7.38905609893065) from iteblog;

2.0

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

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

hive> select log10(100) from iteblog;

2.0

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

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

hive> select log2(8) from iteblog;

3.0

10、对数函数: log

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

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

4.0

11、幂运算函数: pow

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

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

16.0

12、幂运算函数: power

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

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

16.0

13、开平方函数: sqrt

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

hive> select sqrt(16) from iteblog;

4.0

14、二进制函数: bin

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

hive> select bin(7) from iteblog;

111

15、十六进制函数: hex

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

hive> select hex(17) from iteblog;

11

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

616263

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

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

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

abc

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

-

hive> select unhex(616263) from iteblog;

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

11

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

10001

18、绝对值函数: abs

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

hive> select abs(-3.9) from iteblog;

3.9

hive> select abs(10.9) from iteblog;

10.9

19、正取余函数: pmod

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

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

1

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

3

20、正弦函数: sin

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

hive> select sin(0.8) from iteblog;

0.7173560908995228

21、反正弦函数: asin

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

hive> select asin(0.7173560908995228) from iteblog;

0.8

22、余弦函数: cos

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

hive> select cos(0.9) from iteblog;

0.6216099682706644

23、反余弦函数: acos

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

hive> select acos(0.6216099682706644) from iteblog;

0.9

24、positive函数: positive

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

hive> select positive(-10) from iteblog;

-10

hive> select positive(12) from iteblog;

12

25、negative函数: negative

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

hive> select negative(-5) from iteblog;

5

hive> select negative(8) from iteblog;

-8

日期函数

1、UNIX时间戳转日期函数: from_unixtime

语法: from_unixtime(bigint unixtime[, string format])
返回值: string
说明: 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式

hive> select from_unixtime(1323308943,\'yyyyMMdd\') from iteblog;

20111208

2、获取当前UNIX时间戳函数: unix_timestamp

语法: unix_timestamp()
返回值: bigint
说明: 获得当前时区的UNIX时间戳

hive> select unix_timestamp() from iteblog;

1323309615

3、日期转UNIX时间戳函数: unix_timestamp

语法: unix_timestamp(string date)
返回值: bigint
说明: 转换格式为\"yyyy-MM-dd HH:mm:ss\"的日期到UNIX时间戳。如果转化失败,则返回0。

hive> select unix_timestamp(\'2011-12-07 13:01:03\') from iteblog;

1323234063

4、指定格式日期转UNIX时间戳函数: unix_timestamp

语法: unix_timestamp(string date, string pattern)
返回值: bigint
说明: 转换pattern格式的日期到UNIX时间戳。如果转化失败,则返回0。

hive> select unix_timestamp(\'20111207 13:01:03\',\'yyyyMMdd HH:mm:ss\') from iteblog;

1323234063

5、日期时间转日期函数: to_date

语法: to_date(string timestamp)
返回值: string
说明: 返回日期时间字段中的日期部分。

hive> select to_date(\'2011-12-08 10:03:01\') from iteblog;

2011-12-08

6、日期转年函数: year

语法: year(string date)
返回值: int
说明: 返回日期中的年。

hive> select year(\'2011-12-08 10:03:01\') from iteblog;

2011

hive> select year(\'2012-12-08\') from iteblog;

2012

7、日期转月函数: month

语法: month (string date)
返回值: int
说明: 返回日期中的月份。

hive> select month(\'2011-12-08 10:03:01\') from iteblog;

12

hive> select month(\'2011-08-08\') from iteblog;

8

8、日期转天函数: day

语法: day (string date)
返回值: int
说明: 返回日期中的天。

hive> select day(\'2011-12-08 10:03:01\') from iteblog;

8

hive> select day(\'2011-12-24\') from iteblog;

24

9、日期转小时函数: hour

语法: hour (string date)
返回值: int
说明: 返回日期中的小时。

hive> select hour(\'2011-12-08 10:03:01\') from iteblog;

10

10、日期转分钟函数: minute

语法: minute (string date)
返回值: int
说明: 返回日期中的分钟。

hive> select minute(\'2011-12-08 10:03:01\') from iteblog;

3

11、日期转秒函数: second

语法: second (string date)
返回值: int
说明: 返回日期中的秒。

hive> select second(\'2011-12-08 10:03:01\') from iteblog;

1

12、日期转周函数: weekofyear

语法: weekofyear (string date)
返回值: int
说明: 返回日期在当前的周数。

@font-face { font-family: "autolinktags"; src: url("https://www.seowoai.com/zb_users/plugin/AutoLinkTags/style/fonts/iconfont.woff2") format("woff2"), url("https://www.seowoai.com/zb_users/plugin/AutoLinkTags/style/fonts/iconfont.woff") format("woff"), url("https://www.seowoai.com/zb_users/plugin/AutoLinkTags/style/fonts/iconfont.ttf") format("truetype"); font-weight:normal; font-style:normal; }.tagslink::after { content:"\e613"; margin:2px 0 0 0px; font-size:12px; font-family:"autolinktags"; display:inline-block; vertical-align:top; }
版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表