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住

相关推荐
叫我阿柒啊8 分钟前
Java全栈开发面试实战:从基础到微服务的完整技术栈解析
java·spring boot·微服务·前端框架·vue·jwt·全栈开发
前行的小黑炭10 分钟前
Android:在项目当中可能会遇到的ANR,应该如何解决?
android·java·kotlin
索迪迈科技1 小时前
Flink Task线程处理模型:Mailbox
java·大数据·开发语言·数据结构·算法·flink
float_六七5 小时前
IntelliJ IDEA双击Ctrl的妙用
java·ide·intellij-idea
能摆一天是一天6 小时前
JAVA stream().flatMap()
java·windows
颜如玉7 小时前
🤲🏻🤲🏻🤲🏻临时重定向一定要能重定向🤲🏻🤲🏻🤲🏻
java·http·源码
程序员的世界你不懂8 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
星空寻流年8 小时前
设计模式第一章(建造者模式)
java·设计模式·建造者模式
gb42152879 小时前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python