Spark缓存策略
当对同一个rdd多次执行action时,如果在磁盘上则每次执行action都会从磁盘将数据加载,如果将其缓存到内存中会提高再次action的读取速度,Spark缓存主要有cache()和persist()两种,当缓存一个rdd时,每一个节点上都会存放这个rdd的partition,当要使用rdd的时候可以直接从内存读出。
cache源码:
def cache(self):
"""
Persist this RDD with the default storage level (C{MEMORY_ONLY}).
"""
self.is_cached = True
self.persist(StorageLevel.MEMORY_ONLY)
return self
从源码可以看出,cache底层调用的是persist方法,传入的参数是:StorageLevel.MEMORY_ONLY,再看persist()方法:
def persist(self, storageLevel=StorageLevel.MEMORY_ONLY):
self.is_cached = True
javaStorageLevel = self.ctx._getJavaStorageLevel(storageLevel)
self._jrdd.persist(javaStorageLevel)
return self
persist方法,传入的参数是StorageLevel,从StorageLevel的源码可以看出它的值总共有6种,因此persist()相比cache()在缓存形式上更为丰富,不仅支持内存的方式,还支持内存和磁盘、内存副本等方式。
StorageLevel.DISK_ONLY = StorageLevel(True, False, False, False)
StorageLevel.DISK_ONLY_2 = StorageLevel(True, False, False, False, 2)
StorageLevel.MEMORY_ONLY = StorageLevel(False, True, False, False)
StorageLevel.MEMORY_ONLY_2 = StorageLevel(False, True, False, False, 2)
StorageLevel.MEMORY_AND_DISK = StorageLevel(True, True, False, False)
StorageLevel.MEMORY_AND_DISK_2 = StorageLevel(True, True, False, False, 2)
StorageLevel.OFF_HEAP = StorageLevel(True, True, True, False, 1)
持久化到内存和直接从磁盘读取时间对比:
import os
import time
from pyspark import SparkContext, SparkConf conf = SparkConf()
sc = SparkContext(conf=conf) current_dir = os.path.dirname(os.path.realpath(__file__))
file_path = "{}/name_age.txt".format(current_dir) def cached():
start_time = time.time()
text_rdd = sc.textFile("file://{}".format(file_path)).cache()
text_rdd.count()
text_rdd.count()
end_time = time.time()
print("{}:{}".format("first cache", end_time - start_time)) start1_time = time.time()
text1_rdd = sc.textFile("file://{}".format(file_path)).cache()
text1_rdd.count()
text1_rdd.count()
end1_time = time.time()
print("{}:{}".format("second cache", end1_time - start1_time)) def uncached():
start_time = time.time()
text_rdd = sc.textFile("file://{}".format(file_path))
text_rdd.count()
text_rdd.count()
end_time = time.time()
print("{}:{}".format("first uncache", end_time - start_time)) start1_time = time.time()
text1_rdd = sc.textFile("file://{}".format(file_path))
text1_rdd.count()
text1_rdd.count()
end1_time = time.time()
print("{}:{}".format("second uncache", end1_time - start1_time)) sc.stop() 执行cached()结果:
first cache:1.7104301452636719
second cache:0.2717571258544922 执行uncached()结果:
first uncache:1.4453039169311523
second uncache:0.49161386489868164
从执行结果可以看出,当第二次执行rdd.count()时,有cache情况下是0.2717571258544922;无cache情况下是0.49161386489868164,由于我的内存空间不足,所以不太明显,当数据量大且内存充足的时候,持久化到内存的效率会远远高于磁盘。
对pyspark有兴趣的小伙伴可以关注我的github,spark for python 持续更新
Spark缓存策略的更多相关文章
- RDD缓存策略
Spark支持将数据集放置在集群的缓存中,以便于数据重用. Spark缓存策略对应的类: class StorageLevel private( private var useDisk_ : Bool ...
- RDD概念、特性、缓存策略与容错
一.RDD概念与特性 1. RDD的概念 RDD(Resilient Distributed Dataset),是指弹性分布式数据集.数据集:Spark中的编程是基于RDD的,将原始数据加载到内存变成 ...
- Spark持久化策略
spark持久化策略_缓存优化persist.cache都是持久化到内存缓存策略 StorageLevel_useDisk:是否使用磁盘_useMemory:是否使用内存_useOffHeap:不用堆 ...
- Spark 缓存机制
Spark中的缓存机制:避免spark每次都重算RDD以及它的所有依赖,cache().persist(). checkpoint(). 1.cache():会被重复使用,但是不能太大的RDD,将其c ...
- 【腾讯Bugly干货分享】彻底弄懂 Http 缓存机制 - 基于缓存策略三要素分解法
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/qOMO0LIdA47j3RjhbCWUEQ 作者:李 ...
- 【转】理解Java Integer的缓存策略
本文将介绍 Java 中 Integer 缓存的相关知识.这是 Java 5 中引入的一个有助于节省内存.提高性能的特性.首先看一个使用 Integer 的示例代码,展示了 Integer 的缓存行为 ...
- Redis的缓存策略和主键失效机制
作为缓存系统都要定期清理无效数据,就需要一个主键失效和淘汰策略. >>EXPIRE主键失效机制 在Redis当中,有生存期的key被称为volatile,在创建缓存时,要为给定的key设置 ...
- Web开发基本准则-55实录-缓存策略
续上篇<Web开发基本准则-55实录-Web访问安全>. Web开发基本准则-55实录-缓存策略 郑昀 创建于2013年2月 郑昀 最后更新于2013年10月26日 提纲: Web访问安全 ...
- 腾讯QQ你的缓存策略应该改下了
缓存策略基本原则大家都怎么考虑的? 缓存好友数量这个也是醉了,这个数字好像变化频率有点低吧,ok,就算你企鹅用户量大,需要缓存,那肉肉的问一句你这更新策略也不能只管网上涨的,不管往下降的吧?难不成你是 ...
随机推荐
- [洛谷P1357] 花园
题目类型:状压\(DP\) -> 矩阵乘法 绝妙然而思维难度极其大的一道好题! 传送门:>Here< 题意:有一个环形花圃,可以种两种花:0或1. 要求任意相邻的\(M\)个花中1的 ...
- SD第九届省赛B题 Bullet
Bullet Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Description In G ...
- 帝国cms 不能正常显示最新文章
后台能正常刷新,但前台就是不能正常显示, 把网站从c盘换到d盘,好了,原来是权限的问题
- VirtualBox下安装linux虚拟机
下载VirtualBox 下载地址:https://www.virtualbox.org/wiki/Downloads 安装VirtualBox 安装虚拟机 如果选择不到64位系统, 开机按 F1 进 ...
- BSGS及扩展BSGS算法及例题
\(BSGS(baby-step-giant-step)\)算法是用来解高次同余方程的最小非负整数解的算法,即形如这个的方程: \(a^x\equiv b(mod\ p)\) 其中\(p\)为质数(其 ...
- Makefile 使用总结(转)
Makefile 使用总结 转自 https://www.cnblogs.com/wang_yb/p/3990952.html 1. Makefile 简介 Makefile 是和 make 命令一 ...
- Angular: 执行ng lint后如何快速修改错误
当我第一次被分配到“修正执行ng lint语句后的错误”这项任务前,我就被导师提前告知这是一个很无聊的任务,当我开始后,我发现其实有一些办法可以加快这个无聊单调的工作.接下来,我就分享一下我的经验. ...
- 基于ionic4、cordova搭建android开发环境
前颜(yan)最近公司有一个项目需求是利用h5进行跨平台开发,这里所说的跨平台开发指的是:将h5代码利用某种方式或工具环境进行打包,最后生成android的apk以及ios的ipa. 本文只讲解and ...
- Spark SQL相关总结
1.spark 数据透视图: pivot(pivot_col, values=None) Pivots a column of the current [[DataFrame]] and perfor ...
- 二维、三维 Laplace 算子的极坐标表示
(1) 设 $(r,\theta)$ 是 $\bbR^2$ 的极坐标, 即 $$\bex x=r\cos\theta,\quad y=r\sin \theta. \eex$$ 证明 Laplace 算 ...