[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 to none. 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‘)

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.

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

Join Optimization

Outer Join Behavior

UNION ALL

TABLESAMPLE

Subqueries

Virtual Columns

Operators and UDFs

LATERAL VIEW

Windowing, OVER, and Analytics

Common Table Expressions

时间: 2024-10-13 19:45:02

[Hive - LanguageManual] Select base use的相关文章

[HIve - LanguageManual] Joins

Hive Joins Hive Joins Join Syntax Examples MapJoin Restrictions Join Optimization Predicate Pushdown in Outer Joins Enhancements in Hive Version 0.11 Join Syntax Hive supports the following syntax for joining tables: join_table:     table_reference J

[Hive - LanguageManual] Hive Data Manipulation Language

LanguageManual DML Hive Data Manipulation Language Hive Data Manipulation Language Loading files into tables Syntax Synopsis Notes Inserting data into Hive Tables from queries Syntax Synopsis Notes Dynamic Partition Inserts Example Additional Documen

[Hive - LanguageManual ] Windowing and Analytics Functions (待)

LanguageManual WindowingAndAnalytics Skip to end of metadata Added by Lefty Leverenz, last edited by Lefty Leverenz on Aug 01, 2014  (view change) show comment Go to start of metadata Windowing and Analytics Functions Windowing and Analytics Function

[HIve - LanguageManual] LateralView

Lateral View Syntax Description Example Multiple Lateral Views Outer Lateral Views Lateral View Syntax lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)* fromClause: FROM baseTable (lateralView)* Description Later

[Hive - LanguageManual] Create/Drop/Alter Database Create/Drop/Truncate Table

Hive Data Definition Language Hive Data Definition Language Overview Create/Drop/Alter Database Create/Drop/Truncate Table Alter Table/Partition/Column Create/Drop/Alter View Create/Drop/Alter Index Create/Drop Function Create/Drop/Grant/Revoke Roles

[Hive - LanguageManual] Alter Table/Partition/Column

Alter Table/Partition/Column Alter Table Rename Table Alter Table Properties Alter Table Comment Add SerDe Properties Alter Table Storage Properties Additional Alter Table Statements Alter Partition Add Partitions Dynamic Partitions Rename Partition

[Hive - LanguageManual] Create/Drop/Grant/Revoke Roles and Privileges / Show Use

Create/Drop/Grant/Revoke Roles and Privileges Hive Default Authorization - Legacy Mode has information about these DDL statements: CREATE ROLE GRANT ROLE REVOKE ROLE GRANT privilege_type REVOKE privilege_type DROP ROLE SHOW ROLE GRANT SHOW GRANT For 

[Hive - LanguageManual] Create/Drop/Alter View Create/Drop/Alter Index Create/Drop Function

Create/Drop/Alter View Create View Drop View Alter View Properties Alter View As Select Version information Icon View support is only available in Hive 0.6 and later. Create View CREATE VIEW [IF NOT EXISTS] view_name [(column_name [COMMENT column_com

[Hive - LanguageManual] Hive Default Authorization - Legacy Mode

Disclaimer Prerequisites Users, Groups, and Roles Names of Users and Roles Creating/Dropping/Using Roles Create/Drop Role Grant/Revoke Roles Viewing Granted Roles Privileges Grant/Revoke Privileges Viewing Granted Privileges Hive Operations and Requi