Hive-built-in-operators

提供:Dev Guides
移動先:案内検索

Hive-組み込み演算子

この章では、Hiveの組み込み演算子について説明します。 Hiveには4種類の演算子があります。

  • 関係演算子
  • 算術演算子
  • 論理演算子
  • 複雑な演算子

関係演算子

これらの演算子は、2つのオペランドを比較するために使用されます。 次の表に、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.

*employee* テーブルは、以下に示すように、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 |
+-----+-----------+-----------+----------------------------------+

次のクエリを実行して、給与がRs 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

次のクエリは、20と30の2つの数値を追加します。

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

次のクエリは、部門がTPで給与がRs 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<K, V> 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.