Built with Tutify.io

    AIStudyLabs

    Cache and Persistence

    Cache and Persistence

    Cache and Persistence

    Spark Cache and persist are optimization techniques for iterative and interactive Spark applications to improve the performance of subsequent operations. It is a mechanism to store RDD or DataFrame in memory so they can be reused effectively across multiple operations.

    As you know, Spark provides computation 100x faster than traditional MapReduce jobs; if you have not designed the jobs to reuse the repeating computations, you will see a drop in performance when you are dealing with billions or trillions of data. Hence, we need to look at the stages and use optimization techniques as one of the ways to improve performance.

    Caching can significantly improve the performance of Spark applications by reducing the need to recompute or reload data from disk. Caching helps avoid recomputation by storing intermediate or frequently used data in memory. cache() and persist() are two methods used for caching the RDD or DataFrame. The difference between cache() and persist() is that cache() saves the data to memory, whereas persist() saves it to the specified type of storage level.

    Spark Cache vs Persist

    Using cache() and persist() methods, Spark provides an optimization mechanism to store the intermediate computation of an RDD, DataFrame, and Dataset so they can be reused in subsequent actions (reusing the RDD, DataFrame, and Dataset computation results).

    Both caching and persisting are used to save the Spark RDD, DataFrame, and Datasets. But, the difference is, the RDD cache() method default saves it to memory (MEMORY_ONLY), whereas the persist() method is used to store to the user-defined storage level.

    cache() Method:

    persist() Method:

    Advantages of Caching and Persistence

    Below are the advantages of using Spark Cache and Persist methods.

    Spark Cache Syntax and Example

    Following is the syntax and usage of the cache() method in Java.

    Syntax

    // Syntax
    Dataset.cache()
    

    Example

    // Create SparkSession object
    import org.apache.spark.sql.SparkSession;
    import org.apache.spark.api.java.JavaSparkContext;
    import org.apache.spark.api.java.JavaRDD;
    
    SparkSession spark = SparkSession.builder().master("local[1]")
            .appName("AIStudyLabs.com").getOrCreate();
    JavaSparkContext sc = new JavaSparkContext(spark.sparkContext());
    
    JavaRDD<Integer> rdd = sc.parallelize(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
    rdd.cache();  // The RDD is cached in memory by default
    

    Spark Persist Syntax and Example

    Spark persist has two signatures: the first signature doesn’t take any argument, which by default saves it to MEMORY_AND_DISK storage level, and the second signature takes StorageLevel as an argument to store it at different storage levels.

    Syntax

    // Spark provides two overloaded methods
    RDD.persist()
    RDD.persist(StorageLevel newLevel)
    

    Example

    // Example
    JavaRDD<Integer> rdd = sc.parallelize(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
    rdd.persist(StorageLevel.MEMORY_AND_DISK());  // Specify storage level
    

    Unpersist Syntax and Example

    We can also unpersist or remove the persistent objects from the memory or storage by using the unpersist() method.

    Syntax

    // Spark provides two overloaded methods
    RDD.unpersist()
    RDD.unpersist(boolean blocking)
    

    Example

    // Example
    JavaRDD<Integer> rdd2 = rdd.unpersist();
    

    In summary, cache() is a convenient way to persist an RDD in memory with default settings, whereas persist() provides more flexibility by allowing you to specify custom storage levels and options. Depending on your specific use case and memory constraints, you can choose between these methods to optimize the performance and resource usage of your Spark application.

    Apache Spark Tutorial with Java Examples — Cache, Persistence and Shared Variables

    Introduction to Apache Spark

    Setup Apache Spark Environment

    SparkSession & SparkContext

    Resilient Distributed Datasets (RDDs)

    Cache, Persistence and Shared Variables

    DataFrame Introduction

    DataFrame Narrow Transformation

    DataFrame Wider Transformation

    DataFrame Actions

    Spark SQL Functions

    Advanced DataFrame Features

    Using SQL Queries

    Spark Read & Write CSV Files

    Working with JSON Files

    Working with Parquet Files

    Spark Dataset

    What Next?

    Tutorial Not Found