前言

sparksql任务中,有时需要建临时表,来实现一些代码逻辑,最后再进行不同临时表逻辑关联,以提高任务执行效率。下面介绍几种建临时表的方法。

1.create table + 临时表名

如下:

drop table  if exists default.tmp_test_table_di_00_${etl_date_key};
create table if not exists default.tmp_test_table_di_00_${etl_date_key} as 
select ...
from ...

将数据写入到临时表中,表的类型为tmp(临时表),适用于数据量大,任务执行完,记得删除临时表;

2. with as 语句

with as 语句是SQL中的一种常用语法,它可以为一个查询结果或子查询结果创建一个临时表,并且可以在后续的查询中使用这个临时表,在查询结束后该临时表就被清除了。这种语法的使用可以使得复杂的查询变得简单,同时也可以提高查询效率。

with tmp_test_table_di_00_${etl_date_key} as (
select ...
from ...
)

3.cache table 语法

cache table tmp_test_table_di_00_${etl_date_key} as (
    select ... 
    from ...
)

with语法只相当于一个视图,并不会将数据缓存;如果要将数据缓存,需要使用cache table语法;
最常见的,多个重复sql时,可使用with / cache语法。with是让代码看起来更简洁;cache是会将数据缓存在内存中,按需求考虑。cache一般用来缓存结果数据,小量数据;对于大量的中间数据做缓存时要仔细考虑,一般不建议;

4.create temporary table

create temporary table default.tmp_test_table_di_00_${etl_date_key}  as 
    select ... 
    from ...

Hive中使用临时表(create temporary table)

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐