第25章内置运算符
本章用来解释Hive的内置运算符,Hive有四种运算符:关系运算符、算术运算符、逻辑运算符、复杂运算符。
25.1 关系运算符
关系运算符被用于两个操作数的比较。下表描述了Hive中可以使用的关系运算符。

25.1.1关系运算符实例

如上图所示,创建emp表,根据创建的表,创建sample文件来存放要插入表格的数据。 sample数据文件如下:
1201,Gopal,45000 ,Technical manager,TP 1202,Manisha,45000 ,Proofreader,PR 1203,Masthanvali,40000,Technical writer,TP 1204,Krian,40000,Hr Admin,HR1205,Kranthi,30000,Op Admin,Admin 1206,Gopal 01,45000,Technical manager,TP 1207,Manisha 02,45000,Proofreader,PR 1208,Masthanvali 03,4000,Technical writer,TP1209,Krian 04,46000,Hr Admin,HR1210,Kranthi 05,60000,Op Admin,Admin
创建emp的表的语句:
CREATE TABLE IF NOT EXISTS emp ( eid int, name String, salary String, destination String) COMMENT 'Employee details'ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'STORED AS TEXTFILE;
执行以下查询来检索上表中员工详细信息:
SELECT * FROM emp WHERE eid=1205;
执行成功,会看到如下响应信息:

执行以下查询来检索 salary>=40000的员工详细信息:
SELECT * FROM emp WHERE salary >=40000;
执行成功,会看到如下响应信息:

25.2 算术运算符
运算符支持对操作数的各种公共算术运算。它们的所有运算返回数字类型,如下表格描述Hive中可用的算术运算符:
运算符
操作数
描述
A + B
all number types
Gives the result of adding A and B.
A – B
all number types
Gives the result of subtracting B from A.
A * B
all number types
Gives the result of multiplying A and B.
A / B
all number types
Gives the result of dividing B from A.
A % B
all number types
Gives the reminder resulting from dividing A by B.
A & B
all number types
Gives the result of bitwise AND of A and B.
A | B
all number types
Gives the result of bitwise OR of A and B.
A ^ B
all number types
Gives the result of bitwise XOR of A and B.
~A
all number types
Gives the result of bitwise NOT of A.
25.2.1算术运算符实例
执行查询语句使两个数字20、30相加:
SELECT 20+30 ADD FROM temp ;
执行成功后,会显出如下所示:

25.3 逻辑运算符
逻辑运算符如下表所示,所有操作的返回值要么是 TRUE要么是 FALSE:
操作符
操作符
描述
A AND B
boolean
TRUE if both A and B are TRUE, otherwise FALSE.
A && B
boolean
Same as A AND B.
A OR B
boolean
TRUE if either A or B or both are TRUE, otherwise FALSE.
A || B
boolean
Same as A OR B.
NOT A
boolean
TRUE if A is FALSE, otherwise FALSE.
!A
boolean
Same as NOT A.
25.3.1逻辑运算符实例
执行查询检索部门为TP且薪资大于40000的员工详细信息:
SELECT * FROM empWHERE salary>40000 AND detp=TP;
执行结果如下图所示:

25.4 复杂运算符(ComplexOperators)
这些运算符提供一个表达式接入Complex 类型元素。
操作符
操作数
描述
A[n]
A is an Array and n is an int
It returns the nth element in the array A. The first element has index 0.
M[key]
M is a Map
It returns the value corresponding to the key in the map.
S.x
S is a struct
It returns the x field of S.
Original: https://blog.51cto.com/u_15685799/5385790
Author: WEL测试
Title: 大数据学习笔记——————-(25)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/66371/
转载文章受原作者版权保护。转载请注明原作者出处!