📜  Hive-内置操作员

📅  最后修改于: 2020-11-30 04:53:49             🧑  作者: Mango


本章介绍了Hive的内置运算符。 Hive中有四种类型的运算符:

  • 关系运算符
  • 算术运算符
  • 逻辑运算符
  • 复杂运算符

关系运算符

这些运算符用于比较两个操作数。下表描述了Hive中可用的关系运算符:

Operator Operand Description
A = B all primitive types TRUE if expression A is equivalent to expression B otherwise FALSE.
A != B all primitive types TRUE if expression A is not equivalent to expression B otherwise FALSE.
A < B all primitive types TRUE if expression A is less than expression B otherwise FALSE.
A <= B all primitive types TRUE if expression A is less than or equal to expression B otherwise FALSE.
A > B all primitive types TRUE if expression A is greater than expression B otherwise FALSE.
A >= B all primitive types TRUE if expression A is greater than or equal to expression B otherwise FALSE.
A IS NULL all types TRUE if expression A evaluates to NULL otherwise FALSE.
A IS NOT NULL all types FALSE if expression A evaluates to NULL otherwise TRUE.
A LIKE B Strings TRUE if string pattern A matches to B otherwise FALSE.
A RLIKE B Strings NULL if A or B is NULL, TRUE if any substring of A matches the Java regular expression B , otherwise FALSE.
A REGEXP B Strings Same as RLIKE.

让我们假设员工表由名为Id,Name,Salary,Designation和Dept的字段组成,如下所示。生成查询以检索ID为1205的员工详细信息。

+-----+--------------+--------+---------------------------+------+
| Id  | Name         | Salary | Designation               | Dept |
+-----+--------------+------------------------------------+------+
|1201 | Gopal        | 45000  | Technical manager         | TP   |
|1202 | Manisha      | 45000  | Proofreader               | PR   |
|1203 | Masthanvali  | 40000  | Technical writer          | TP   |
|1204 | Krian        | 40000  | Hr Admin                  | HR   |
|1205 | Kranthi      | 30000  | Op Admin                  | Admin|
+-----+--------------+--------+---------------------------+------+

执行以下查询以使用上表检索员工详细信息:

hive> SELECT * FROM employee WHERE Id=1205;

成功执行查询后,您将看到以下响应:

+-----+-----------+-----------+----------------------------------+
| ID  | Name      | Salary    | Designation              | Dept  |
+-----+---------------+-------+----------------------------------+
|1205 | Kranthi   | 30000     | Op Admin                 | Admin |
+-----+-----------+-----------+----------------------------------+

执行以下查询以检索薪水大于或等于40000卢比的员工详细信息。

hive> SELECT * FROM employee WHERE Salary>=40000;

成功执行查询后,您将看到以下响应:

+-----+------------+--------+----------------------------+------+
| ID  | Name       | Salary | Designation                | Dept |
+-----+------------+--------+----------------------------+------+
|1201 | Gopal      | 45000  | Technical manager          | TP   |
|1202 | Manisha    | 45000  | Proofreader                | PR   |
|1203 | Masthanvali| 40000  | Technical writer           | TP   |
|1204 | Krian      | 40000  | Hr Admin                   | HR   |
+-----+------------+--------+----------------------------+------+

算术运算符

这些运算符支持对操作数的各种常见算术运算。它们都返回数字类型。下表描述了Hive中可用的算术运算运算符:

Operators Operand Description
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.

以下查询将两个数字相加,即20和30。

hive> SELECT 20+30 ADD FROM temp;

成功执行查询后,您将看到以下响应:

+--------+
|   ADD  |
+--------+
|   50   |
+--------+

逻辑运算符

运算符是逻辑表达式。它们全部返回TRUE或FALSE。

Operators Operands Description
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.

以下查询用于检索部门为TP并且薪水超过40000卢比的员工详细信息。

hive> SELECT * FROM employee WHERE Salary>40000 && Dept=TP;

成功执行查询后,您将看到以下响应:

+------+--------------+-------------+-------------------+--------+
| ID   | Name         | Salary      | Designation       | Dept   |
+------+--------------+-------------+-------------------+--------+
|1201  | Gopal        | 45000       | Technical manager | TP     |
+------+--------------+-------------+-------------------+--------+

复杂运算符

这些运算符提供一个表达式来访问复杂类型的元素。

Operator Operand Description
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 and key has type K It returns the value corresponding to the key in the map.
S.x S is a struct It returns the x field of S.