Spark缓存策略

版权声明:转载请备注出处,https://blog.csdn.net/jiaxinhong https://blog.csdn.net/jiaxinhong/article/details/82145793

这里写图片描述

持久化的单位是partition,2是指partition的备份数,不是指持久化到几个节点上

package com.bjsxt.spark.persist

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.storage.StorageLevel

object CacheTest {
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
      .setAppName("CacheTest")
      .setMaster("local")

      val sc = new SparkContext(conf)
      sc.setCheckpointDir("G://checkpoint")

      var liensRDD  = sc.textFile("userLog")
      /**
       * 1、cache的返回值  必须赋值给一个新的变量  , 在其他的job中直接使用这个变量就可以
       * 2、cache是一个懒执行,必须有action类的算子触发
       * 3、cache算子的后面不能立即添加action类算子
       * 
       * 
       * cache默认会将rdd中的数据持久化到内存中
       * 
       * persist和cache有什么区别?
       * cache是persist的一个简化版   persist里面可以手动指定其他的持久化级别   cache = persists(StorageLevel.MEMORY_ONLY)
       * 
       */
//      liensRDD = liensRDD.cache()
      liensRDD.checkpoint()
      liensRDD = liensRDD.persist(StorageLevel.MEMORY_ONLY)


      val startTime = System.currentTimeMillis()
      val count1 = liensRDD.count
      val endTime = System.currentTimeMillis()
      println("总共耗时:" + (endTime - startTime) + "ms\t Count:" + count1)


      val startTime1 = System.currentTimeMillis()
      val count2 = liensRDD.count
      val endTime1 = System.currentTimeMillis()
      println("总共耗时:" + (endTime1 - startTime1) + "ms\t Count:" + count2)
  }
}

猜你喜欢

转载自blog.csdn.net/jiaxinhong/article/details/82145793