group by
按照某些字段的值进行分组,有相同值放到一起。
样例
select col1 [,col2] ,count(1),sel_expr(聚合操作)from table where condition -->Map端执行 group by col1 [,col2] -->Reduce端执行 [having] -->Reduce端执行
注意
select后面非聚合列,必须出现在group by中
select后面除了普通列就是一些聚合操作
group by后面也可以跟表达式,比如substr(col)
特性
使用了reduce操作, 受限于reduce数量 ,设置reduce参数 mapred.reduce.tasks
输出文件个数与reduce数相同,文件大小与reduce处理的数据量有关。
示例
让我们以SELECT... GROUP BY子句为例。假设员工表有如下Id, Name, Salary, Designation, 和 Dept字段。产生一个查询以检索每个部门的员工数量。
+------+--------------+-------------+-------------------+--------+ | ID | Name | Salary | Designation | Dept | +------+--------------+-------------+-------------------+--------+ |1201 | Gopal | 45000 | Technical manager | TP | |1202 | Manisha | 45000 | Proofreader | PR | |1203 | Masthanvali | 40000 | Technical writer | TP | |1204 | Krian | 45000 | Proofreader | PR | |1205 | Kranthi | 30000 | Op Admin | Admin | +------+--------------+-------------+-------------------+--------+
下面使用上述业务情景查询检索员工的详细信息。
hive> SELECT Dept,count(*) FROM employee GROUP BY DEPT;
成功执行查询后,能看到以下回应:
+------+--------------+ | Dept | Count(*) | +------+--------------+ |Admin | 1 | |PR | 2 | |TP | 3 | +------+--------------+
JDBC 程序
下面给出的是JDBC程序应用对给定的GROUP BY子句例子。
import java.sql.SQLException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.sql.DriverManager; public class HiveQLGroupBy { 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 Dept,count(*) ” + “FROM employee GROUP BY DEPT; ”); System.out.println(" Dept \t count(*)"); while (res.next()) { System.out.println(res.getString(1) + " " + res.getInt(2)); } con.close(); } }
输出:
Dept Count(*) Admin 1 PR 2 TP 3
感觉本站内容不错,读后有收获?小额赞助,鼓励网站分享出更好的教程