HiveQL Select Where

Hive查询语言(HiveQL)是 Hive 在 Metastore 中处理和分析结构化数据的查询语言。

本章介绍如何在 SELECT 语句中使用 WHERE 子句。

SELECT 语句用于从表中检索数据,WHERE 子句中包含条件。

SELECT 语句使用条件过滤数据并给出有限的结果。

内置的运算符和函数生成满足条件的表达式。

 

语法

下面给出的是SELECT查询的语法:

SELECT [ALL | DISTINCT] select_expr, select_expr, ...
FROM table_reference
[WHERE where_condition]
[GROUP BY col_list]
[HAVING having_condition]
[CLUSTER BY col_list | [DISTRIBUTE BY col_list] [SORT BY col_list]]
[LIMIT number];

 

范例

让我们以SELECT ... WHERE子句为例。假设我们有下面给出的员工表,其中包含名为Id,Name,Salary,Designation和Dept的字段。生成一个查询来检索获得超过30000卢比工资的员工详细信息。

+------+--------------+-------------+-------------------+--------+
| 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 salary>30000;

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

+------+--------------+-------------+-------------------+--------+
| 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     |
+------+--------------+-------------+-------------------+--------+

JDBC程序

用于给出示例的where子句的JDBC程序如下。

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

public class HiveQLWhere {
   private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";

   public static void main(String[] args) throws SQLException {

      // Register driver and create driver instance
      Class.forName(driverName);

      // get connection
      Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/userdb", "", "");

      // create statement
      Statement stmt = con.createStatement();

      // execute statement
      Resultset res = stmt.executeQuery("SELECT * FROM employee WHERE salary>30000;");

      System.out.println("Result:");
      System.out.println(" ID \t Name \t Salary \t Designation \t Dept ");

      while (res.next()) {
         System.out.println(res.getInt(1) + " " + res.getString(2) + " " + res.getDouble(3) + " " + res.getString(4) + " " + res.getString(5));
      }
      con.close();
   }
}

将该程序保存在名为HiveQLWhere.java的文件中。使用以下命令来编译和执行该程序。

$ javac HiveQLWhere.java
$ java HiveQLWhere

输出

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

本章介绍如何在 SELECT 语句中使用 ORDER BY 子句。ORDER BY子句用于基于一列检索信息,并按升序或降序对结果集进行排序。 语法下面给出的是ORDER BY子句的语法 ...