Spark SQL,DF,RDD cache常用方式

RDD中的cache

调用cache方法

复制代码
val testRDD = sc.parallelize(Seq(elementA, elementB, elementC)).
    map(x => (x._1, x._2)).setName("testRDD")

testRDD.cache()

dataframe中的cache

利用catalog以表的形式对数据进行缓存

复制代码
import org.apache.spark.SparkConf
import org.apache.spark.sql.{SaveMode, SparkSession}
import org.apache.spark.sql.functions.udf



val conf = new SparkConf().setAppName(s"test_app")
val spark = SparkSession.builder().config(conf).getOrCreate()
spark.read.parquet(s"${BASEPATH}/dws_live_mid_stat_order_di/event_day=${event_day}").createOrReplaceTempView(s"dwd_flow_sessionid_di")

spark.catalog.cacheTable("dwd_flow_sessionid_di")
spark.catalog.uncacheTable("dwd_flow_sessionid_di")

SQL中的cache

复制代码
    spark.sql(
      s"""
         |cache table flow_basic_tmp as
         |select
         |    *
         |from
         |    test.tmp_live_mid_stat_order_di
         |""".stripMargin)

UNCACHE TABLE [ IF EXISTS ] table_identifier

SQL cache 相关文档,可以懒加载 CACHE TABLE - Spark 3.5.0 Documentation

需要注意的点 :

Spark.createDateFrame需要执行

基于DF 需要执行action,才能让RDD cache住

相关推荐
m0_3801138413 分钟前
SpringBoot创建动态定时任务的几种方式
java·spring boot·spring
Gofarlic_OMS28 分钟前
SolidEdge专业许可证管理工具选型关键评估标准
java·大数据·运维·服务器·人工智能
清华都得不到的好学生33 分钟前
数据结构->1.稀疏数组,2.数组队列(没有取模),3.环形队列
java·开发语言·数据结构
weyyhdke43 分钟前
基于SpringBoot和PostGIS的省域“地理难抵点(最纵深处)”检索及可视化实践
java·spring boot·spring
ILYT NCTR1 小时前
【springboot】Spring 官方抛弃了 Java 8!新idea如何创建java8项目
java·spring boot·spring
weixin_425023001 小时前
PG JSONB 对应 Java 字段 + MyBatis-Plus 完整实战
java·开发语言·mybatis
不早睡不改名@1 小时前
Netty源码分析---Reactor线程模型深度解析(二)
java·网络·笔记·学习·netty
子非鱼@Itfuture2 小时前
`<T> T execute(...)` 泛型方法 VS `TaskExecutor<T>` 泛型接口对比分析
java·开发语言
2601_949816162 小时前
spring.profiles.active和spring.profiles.include的使用及区别说明
java·后端·spring
淼淼爱喝水2 小时前
DVWA SQL 注入(Medium/High 级别)过滤绕过与防范实验(超详细图文版)
数据库·sql·网络安全