[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_referenceindicates 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.identifierstonone. 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*FROMt1To 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;SELECTquery_specifications;USEdefault;
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 5hive> SELECT DISTINCT col1, col2 FROM t1 1 3 1 4 2 5hive> 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_viewsWHERE 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 =1SELECT * FROM sales SORT BY amount DESC LIMIT5
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 ...
随机推荐
- 格林治时间,也就是返回从 UTC 1970 年 1 月 1 日午夜开始经过的毫秒数。
格林治时间,也就是返回从 UTC 1970 年 1 月 1 日午夜开始经过的毫秒数. (* Delphi获取13位格林治时间实现方法, 与java中的java.lang.System.currentT ...
- 【原创】中文分词系统 ICTCLAS2015 的JAVA封装和多线程执行(附代码)
本文针对的问题是 ICTCLAS2015 的多线程分词,为了实现多线程做了简单的JAVA封装.如果有需要可以自行进一步封装其它接口. 首先ICTCLAS2015的传送门(http://ictclas. ...
- javascript 字符转义汇总
在开发中经常遇到需要字符转义的,我将一一把遇到的转义列举出来 1.今天中午做项目的时候遇到一个字符串链接的问题,需要链接的的是一个函数的参数 时间字符串:"2014-04-08 16:37: ...
- DirectX 3D 之C#开发
C#下进行directX的3D开发,一个旋转的4棱锥的例子. 建议看两个文档<Managed DirectX 9图形和游戏编程简略中文文档>和<Managed DirectX 9 S ...
- Android开发之XML的创建和解析
参考:http://blog.csdn.net/pi9nc/article/details/9320413 XML文件的解析,代码: public void click(View v) { Input ...
- [POJ3177]Redundant Paths(双连通图,割边,桥,重边)
题目链接:http://poj.org/problem?id=3177 和上一题一样,只是有重边. 如何解决重边的问题? 1. 构造图G时把重边也考虑进来,然后在划分边双连通分量时先把桥删去,再划分 ...
- apache启动报错(98)Address already in use: make_sock: could not bind to...
# /etc/init.d/httpd startStarting httpd: (98)Address already in use: make_sock: could not bind to ad ...
- span元素定义宽高度
由于span是行内元素,不可能有高度和宽度的,在span标签里添加内容,可以撑出来宽高,想要定义宽高必须转话成块级元素. span{ display:block; } 或者使用 span{ displ ...
- BestCoder Round #35
A 题意:给出n个黑球,m个白球,每次取1个球,取了n+m次以后,会生成一个随机的01串S, 如果第i次取出的是黑球,则s[i]=1,如果是白色的,那么s[i]=0, 问01串在S中出现的期望次数 大 ...
- acdream 1684 娜娜梦游仙境系列——莫名其妙的插曲 (gcd)
题意:一开始有一个集合,集合里有n个不同的数,然后Alice(娜娜)与Bob轮流进行操作,每人都可以任意选择两个数a,b,不妨设a>b,不过要求a-b不在集合中,把a-b放入集合(集合元素个数只 ...