[Hive进阶]- Hive with as 语法

前言

           公用表表达式(CTE)是从WITH子句中指定的简单查询派生的临时结果集(会把查询的表数据放到内存中,供其他查询随时使用),该子句紧跟在SELECT或INSERT关键字之前。CTE仅在单个语句的执行范围内定义。可以在Hive SELECTINSERT,  CREATE TABLE AS SELECTCREATE VIEW AS SELECT语句中使用一个或多个CTE 。

使用HIVE-1180在Hive 0.13.0中添加了通用表格表达式。

语法

withClause: cteClause (, cteClause)*
cteClause: cte_name AS (select statment)
  • 注意
SubQuery Blocks中不支持WITH子句
Views,CTAS和INSERT语句支持CTE。
不支持递归查询。

举例

1、在 select 中使用 CTE

with q1 as ( select key from src where key = '5')
select *
from q1;
 
-- from style
with q1 as (select * from src where key= '5')
from q1
select *;
  
-- chaining CTEs
with q1 as ( select key from q2 where key = '5'),
q2 as ( select key from src where key = '5')
select * from (select key from q1) a;
  
-- union example
with q1 as (select * from src where key= '5'),
q2 as (select * from src s2 where key = '4')
select * from q1 union all select * from q2;

2、CTE in Views, CTAS, and Insert Statements

-- insert example
create table s1 like src;
with q1 as ( select key, value from src where key = '5')
from q1
insert overwrite table s1
select *;
 
-- ctas example
create table s2 as
with q1 as ( select key from src where key = '4')
select * from q1;
 
-- view example
create view v1 as
with q1 as ( select key from src where key = '5')
select * from q1;
select * from v1;
  
-- view example, name collision
create view v1 as
with q1 as ( select key from src where key = '5')
select * from q1;
with q1 as ( select key from src where key = '4')
select * from v1;

在第二个View示例中,查询的CTE与创建视图时使用的CTE不同。结果将包含key ='5'的行,因为在视图的查询语句中,视图定义中定义的CTE生效。

另见JIRA:

  • HIV-1180  支持Hive中的公用表表达式(CTE)

参考:

https://cwiki.apache.org/confluence/display/Hive/Common+Table+Expression

https://cwiki.apache.org/confluence/display/Hive/Home#Home-HiveDocumentation

https://cwiki.apache.org/confluence/display/Hive/LanguageManual

猜你喜欢

转载自blog.csdn.net/high2011/article/details/85335954