ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Hive【with as用法】

2021-12-15 14:30:20  阅读:213  来源: 互联网

标签:q1 src Hive 用法 CTE key where select


前言

公用表表达式(CTE)是从WITH子句中指定的简单查询派生的临时结果集(会把查询的表数据放到内存中,供其他查询随时使用),该子句紧跟在SELECT或INSERT关键字之前。CTE仅在单个语句的执行范围内定义。可以在Hive SELECT,INSERT, CREATE TABLE AS SELECT或CREATE 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;
with q1 as ( select key, value from src where key = '5')
from q1
insert overwrite table s1
select *;
 
-- insert 
create table tb_h_test_insert;
WITH TABLE1 AS
(
    SELECT 
    cod_index,
    CAST(test_1 AS VARCHAR(200)), 
    CAST(test_2 AS VARCHAR(200)), 
    CAST(test_3 AS VARCHAR(200))
    FROM db_h_gss.tb_h_test_orig
)
INSERT INTO TABLE tb_h_test_insert PARTITION (cod_index = 1)
SELECT
    test_1,
    test_2,
    test_3
FROM TABLE1 WHERE cod_index = 1;
 
 
-- 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)

详见官方:

标签:q1,src,Hive,用法,CTE,key,where,select
来源: https://blog.csdn.net/liuwei0376/article/details/121951651

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有