SparkCore — CacheManager持久化原理

本文详细解析了Spark中CacheManager的持久化原理,阐述了在RDD计算过程中如何利用持久化数据提高效率。当计算RDD时,CacheManager会检查是否已持久化,若未持久化则从父RDD计算,若已持久化则直接读取,优先尝试本地读取,再考虑远程拉取,最后读取checkpoint数据,确保数据可用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CacheManager持久化原理

  CacheManager的主要功能就是在进行RDD计算的时候,会先从持久化的RDD中读取当前计算RDD需要的数据。
  下面是RDD在进行计算的时候,读取数据的流程:
CacheManager
  RDD在计算的时候,首先会判断是否被持久化过,如果没有持久化,那么会用父RDD的数据来执行我们定义的算子计算新的RDD的partition的数据;假设持久化了,那么使用CacheManager直接读取持久化的数据;在读取的时候,使用BlockManager尝试从本地读取数据,或者远程拉取数据;假如一直都读取不到,那么还会读取之前checkpoint的数据,假如checkpoint的数据也没有读取到,那么就需要重新计算。在重新计算的时候,会将之前丢失的数据接着进行缓存,依据持久化级别进行缓存,内存或者磁盘等。

Ehcache提供了多种持久化方式,包括磁盘持久化和数据库持久化。下面分别介绍这两种持久化方式的实现方法。 1. 磁盘持久化 (1)配置Ehcache的缓存管理器,使其支持磁盘持久化: ``` <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskPersistent="true" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/> ``` 其中,diskPersistent="true"表示启用磁盘持久化,diskExpiryThreadIntervalSeconds="120"表示缓存过期检查间隔时间为120秒。 (2)定义缓存数据对象和序列化方式: ``` public class User implements Serializable { private static final long serialVersionUID = 1L; private Long id; private String name; // 省略getter和setter } public class UserSerializer implements ElementValueSerializer<User> { @Override public User createValue(byte[] serializedValue) { try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedValue))) { return (User) ois.readObject(); } catch (IOException | ClassNotFoundException e) { throw new CacheException(e); } } @Override public byte[] serializeValue(User value) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(value); oos.flush(); return baos.toByteArray(); } catch (IOException e) { throw new CacheException(e); } } } ``` (3)使用缓存管理器创建缓存对象: ``` CacheManager cacheManager = new CacheManager(); Cache cache = new Cache( new CacheConfiguration("userCache", 10000) .eternal(false) .timeToIdleSeconds(120) .timeToLiveSeconds(120) .overflowToDisk(true) .diskPersistent(true) .diskExpiryThreadIntervalSeconds(120) .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU) .persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP)) .elementValueType(ElementValueType.SERIALIZATION) .elementValueSerializer(new UserSerializer()) ); cacheManager.addCache(cache); ``` 2. 数据库持久化 (1)配置Ehcache的缓存管理器,使其支持数据库持久化: ``` <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/> ``` 其中,overflowToDisk="false"表示禁用磁盘缓存,diskPersistent="false"表示禁用磁盘持久化。 (2)定义缓存数据对象和序列化方式: ``` public class User implements Serializable { private static final long serialVersionUID = 1L; private Long id; private String name; // 省略getter和setter } public class UserSerializer implements ElementValueSerializer<User> { @Override public User createValue(byte[] serializedValue) { try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedValue))) { return (User) ois.readObject(); } catch (IOException | ClassNotFoundException e) { throw new CacheException(e); } } @Override public byte[] serializeValue(User value) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(value); oos.flush(); return baos.toByteArray(); } catch (IOException e) { throw new CacheException(e); } } } ``` (3)使用缓存管理器创建缓存对象: ``` CacheManager cacheManager = new CacheManager(); Cache cache = new Cache( new CacheConfiguration("userCache", 10000) .eternal(false) .timeToIdleSeconds(120) .timeToLiveSeconds(120) .overflowToDisk(false) .diskPersistent(false) .diskExpiryThreadIntervalSeconds(120) .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU) .persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.LOCALRESTARTABLE)) .elementValueType(ElementValueType.SERIALIZATION) .elementValueSerializer(new UserSerializer()) ); cacheManager.addCache(cache); ``` 其中,persistence(new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.LOCALRESTARTABLE))表示启用数据库持久化,elementValueType(ElementValueType.SERIALIZATION)和elementValueSerializer(new UserSerializer())表示使用序列化方式将缓存数据存储到数据库中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值