[Hive - LanguageManual] Select base use
Select Syntax
[WITH CommonTableExpression (, CommonTableExpression)*] (Note: Only available starting with Hive 0.13 . 0 ) SELECT [ALL | DISTINCT] select_expr, select_expr, ... FROM table_reference [WHERE where_condition] [GROUP BY col_list] [CLUSTER BY col_list | [DISTRIBUTE BY col_list] [SORT BY col_list] ] [LIMIT number] |
- A SELECT statement can be part of a union query or a subquery of another query.
table_reference
indicates the input to the query. It can be a regular table, a view, a join construct or a subquery.- Table names and column names are case insensitive.
- In Hive 0.12 and earlier, only alphanumeric and underscore characters are allowed in table and column names.
- In Hive 0.13 and later, column names can contain any Unicode character (see HIVE-6013). Any column name that is specified within backticks (
`
) is treated literally. Within a backtick string, use double backticks (``
) to represent a backtick character. - To revert to pre-0.13.0 behavior and restrict column names to alphanumeric and underscore characters, set the configuration property
hive.support.quoted.identifiers
tonone
. In this configuration, backticked names are interpreted as regular expressions. For details, see Supporting Quoted Identifiers in Column Names (attached to HIVE-6013). Also see REGEX Column Specification below.
Simple query. For example, the following query retrieves all columns and all rows from table t1.
SELECT
*
FROM
t1
To specify a database, either qualify the table names with database names ("
db_name.table_name
" starting in Hive 0.7) or issue the USE statement before the query statement (starting in Hive 0.6)."
db_name.table_name
" allows a query to access tables in different databases.USE sets the database for all subsequent HiveQL statements. Reissue it with the keyword "
default
" to reset to the default database.USE database_name;
SELECT
query_specifications;
USE
default
;
WHERE Clause
The WHERE condition is a boolean expression. For example, the following query returns only those sales records which have an amount greater than 10 from the US region. Hive supports a number ofoperators and UDFs in the WHERE clause:
SELECT * FROM sales WHERE amount > 10 AND region = "US" |
As of Hive 0.13 some types of subqueries 子查询 are supported in the WHERE clause.
ALL and DISTINCT Clauses
The ALL and DISTINCT options specify whether duplicate rows should be returned. If none of these options are given, the default is ALL (all matching rows are returned). DISTINCT specifies removal of duplicate rows from the result set. Note, Hive supports SELECT DISTINCT * since 0.15 (HIVE-9194).
hive> SELECT col1, col2 FROM t1 1 3 1 3 1 4 2 5 hive> SELECT DISTINCT col1, col2 FROM t1 1 3 1 4 2 5 hive> SELECT DISTINCT col1 FROM t1 1 2 |
Partition Based Queries 分区查询
In general, a SELECT query scans the entire(全部) table (other than for sampling (采样)). If a table created using the PARTITIONED BY clause, a query can do partition pruning and scan only a fraction of the table relevant to the partitions specified by the query. Hive currently does partition pruning if the partition predicates are specified in the WHERE clause or the ON clause in a JOIN. For example, if table page_views is partitioned on column date, the following query retrieves rows for just days between 2008-03-01 and 2008-03-31.
SELECT page_views.* FROM page_views WHERE page_views.date >= '2008-03-01' AND page_views.date <= '2008-03-31' |
If a table page_views is joined with another table dim_users, you can specify a range of partitions in the ON clause as follows:
SELECT page_views.* FROM page_views JOIN dim_users ON (page_views.user_id = dim_users.id AND page_views.date >= '2008-03-01' AND page_views.date <= '2008-03-31' ) |
- See also Group By
- See also Sort By / Cluster By / Distribute By / Order By
HAVING Clause
Hive added support for the HAVING clause in version 0.7.0. In older versions of Hive it is possible to achieve the same effect by using a subquery, e.g:
SELECT col1 FROM t1 GROUP BY col1 HAVING SUM(col2) > 10 |
can also be expressed as
SELECT col1 FROM (SELECT col1, SUM(col2) AS col2sum FROM t1 GROUP BY col1) t2 WHERE t2.col2sum > 10 |
LIMIT Clause
Limit indicates the number of rows to be returned. The rows returned are chosen at random. The following query returns 5 rows from t1 at random.
SELECT * FROM t1 LIMIT 5 |
Top k queries. The following query returns the top 5 sales records wrt amount.
SET mapred.reduce.tasks =
1
SELECT * FROM sales SORT BY amount DESC LIMIT
5
REGEX Column Specification 正则表达式列
A SELECT statement can take regex-based column specification in Hive releases prior to 0.13.0, or in 0.13.0 and later releases if the configuration property hive.support.quoted.identifiers
is set to none
.
- We use Java regex syntax. Try http://www.fileformat.info/tool/regex.htm for testing purposes.
- The following query selects all columns except ds and hr.
SELECT `(ds|hr)?+.+` FROM sales |
More Select Syntax
See the following documents for additional syntax and features of SELECT statements:
GROUP BY
SORT BY, ORDER BY, CLUSTER BY, DISTRIBUTE BY
JOIN
UNION ALL
TABLESAMPLE
Subqueries
Virtual Columns
Operators and UDFs
LATERAL VIEW
Windowing, OVER, and Analytics
Common Table Expressions
[Hive - LanguageManual] Select base use的更多相关文章
- Hive LanguageManual DDL
hive语法规则LanguageManual DDL SQL DML 和 DDL 数据操作语言 (DML) 和 数据定义语言 (DDL) 一.数据库 增删改都在文档里说得也很明白,不重复造车轮 二.表 ...
- hive中select中DISTINCT的技巧和使用
hive中select中DISTINCT的技巧和使用 单表的唯一查询用:distinct 多表的唯一查询用:group by 在使用MySQL时,有时需要查询出某个字段不重复的记录,虽然mysql提供 ...
- [HIve - LanguageManual] Hive Operators and User-Defined Functions (UDFs)
Hive Operators and User-Defined Functions (UDFs) Hive Operators and User-Defined Functions (UDFs) Bu ...
- [Hive - LanguageManual ] Windowing and Analytics Functions (待)
LanguageManual WindowingAndAnalytics Skip to end of metadata Added by Lefty Leverenz, last edi ...
- [HIve - LanguageManual] LateralView
Lateral View Syntax Description Example Multiple Lateral Views Outer Lateral Views Lateral View Synt ...
- [Hive - LanguageManual] DML: Load, Insert, Update, Delete
LanguageManual DML Hive Data Manipulation Language Hive Data Manipulation Language Loading files int ...
- [Hive - LanguageManual ] ]SQL Standard Based Hive Authorization
Status of Hive Authorization before Hive 0.13 SQL Standards Based Hive Authorization (New in Hive 0. ...
- [Hive - LanguageManual] Hive Concurrency Model (待)
Hive Concurrency Model Hive Concurrency Model Use Cases Turn Off Concurrency Debugging Configuration ...
- [Hive - LanguageManual ] Explain (待)
EXPLAIN Syntax EXPLAIN Syntax Hive provides an EXPLAIN command that shows the execution plan for a q ...
随机推荐
- java:抽象类和抽象函数
面向对象:先抽象后具体 抽象类也叫基类 抽象函数:只有函数的定义,没有函数体的函数, 语法:类必须定义为抽象类,才能调用抽象函数,抽象类里面可以没有抽象函数 abstract class Printe ...
- ssm框架整合小结
1.整合思路 一.Dao层:整合mybatis和spring 需要的jar包: 1.mybatis的jar包 2.Mysql数据库驱动 3.数据库连接池 4.Mybatis和spring的整合包. 5 ...
- !!流行的php面试题及答案
分类: 1.在PHP中,当前脚本的名称(不包括路径和查询字符串)记录在预定义变量(1)中:而链接到当前页面的URL记录在预定义变量(2)中. 答:echo $_SERVER['PHP_SELF']; ...
- Segmentation Fault错误原因总结
最近在项目上遇到了Segmentation Fault的错误,一直调试不出来是哪里出了问题,对于刚接触嵌入式的,也不知道该如何去调试一个项目,定位内存问题,纠结了好几天,好阿红整理下自己的思路.从头开 ...
- JVM学习笔记(三)------内存管理和垃圾回收
JVM内存组成结构 JVM栈由堆.栈.本地方法栈.方法区等部分组成,结构图如下所示: 1)堆 所有通过new创建的对象的内存都在堆中分配,其大小可以通过-Xmx和-Xms来控制.堆被划分为新生代和旧生 ...
- excel文档
1.快速统计行数(ctrl+Shift+(方向键向下)). bson数据类型 留个影响 public enum BsonType { Double = 0x01, String = 0x02, Doc ...
- 设置MySQL主从同步
1. 配置主服务器 1.1 编辑my.cnf文件,配置主服务器ID. [mysqld] log-bin=mysql-bin server-id=1relay-log = relay-bin relay ...
- JavaScript constructor 属性
定义和用法 constructor 属性返回对创建此对象的数组函数的引用. 语法 object.constructor 实例 例子 1 在本例中,我们将展示如何使用 constructor 属性: & ...
- JSOI2008最大数(线段树)
注意到数列只增不减,而题目中又明确说道m<=200000;这样的数据规模线段树完全可以承受得了.所以我们可以事先建好一棵200000个子节点的线段树,然后求极值就好了. type node=re ...
- ajax上传图片 jquery插件 jquery.form.js 的方法 ajaxSubmit; AjaxForm与AjaxSubmit的差异
先引入脚本 这里最好是把jquery的脚本升级到1.7 <script src="js/jquery-1.7.js" type="text/javascript& ...