Built with Tutify.io

    AIStudyLabs

    Create a Spark RDD using Parallelize

    Create a Spark RDD using Parallelize

    Let's see how to create Spark RDD using sparkContext.parallelize() method and using Spark shell and Scala example.

    Before we start let me explain what is RDD, Resilient Distributed Datasets (RDD) is a fundamental data structure of Spark, It is an immutable distributed collection of objects. Each dataset in RDD is divided into logical partitions, which may be computed on different nodes of the cluster.

    Note: Spark Parallelizes an existing collection in your driver program.

    Below is an example of how to create an RDD using a parallelize method from Sparkcontext. For example, sparkContext.parallelize(Array(1,2,3,4,5,6,7,8,9,10)) creates an RDD with an Array of Integers.

    <a id="h-using-sc-parallelize-on-spark-shell-or-repl"></a>

    Using sc.parallelize on Spark Shell or REPL

    Spark shell provides SparkContext variable "sc", use sc.parallelize() to create an RDD.

    
    scala> val rdd = sc.parallelize(Array(1,2,3,4,5,6,7,8,9,10))
    rdd: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[1] at parallelize at
    :24
    

    <a id="h-using-spark-sparkcontext-parallelize-in-scala"></a>

    Using Spark sparkContext.parallelize in Scala

    If you are using scala, get SparkContext object from SparkSession and use sparkContext.parallelize() to create rdd. This function also has another signature which additionally takes an integer argument to specify the number of partitions. Partitions are basic units of parallelism in Apache Spark. RDDs in Apache Spark are a collection of partitions that are executed by processors to achieve parallelism. The parallelize method takes a collection of objects, such as a list, tuple, or set, and creates an RDD from them. The RDD is then distributed across the nodes in the Spark cluster for parallel processing.

    
    // Imports
    import org.apache.spark.rdd.RDD
    import org.apache.spark.sql.SparkSession
    
    object RDDParallelize {
    
      def main(args: Array[String]): Unit = {
          val spark:SparkSession = SparkSession.builder().master("local[1]")
              .appName("AIStudyLabs.com")
              .getOrCreate()
          val rdd:RDD[Int] = spark.sparkContext.parallelize(List(1,2,3,4,5))
          val rddCollect:Array[Int] = rdd.collect()
          println("Number of Partitions: "+rdd.getNumPartitions)
          println("Action: First element: "+rdd.first())
          println("Action: RDD converted to Array[Int] : ")
          rddCollect.foreach(println)
      }
    }
    

    By executing the above program you should see the below output.

    
    Number of Partitions: 1
    Action: First element: 1
    Action: RDD converted to Array[Int] : 
    1
    2
    3
    4
    5
    

    <a id="h-create-empty-rdd-by-using-sparkcontext-parallelize"></a>

    Create empty RDD by using sparkContext.parallelize

    We can create emptyRdd in two ways , one is using sparkContext.parallelize() and the other is using sparkContext.emptyRDD() method. The difference is being creating between these two ways is the former one creates no part files on disk , whereas the latter one creates the part files on Disk.

    <a id="h-using-sparkcontext-parallelize"></a>

    Using sparkContext.parallelize

    
    // Create emptyRdd using parallelize
    sparkContext.parallelize(Seq.empty[String])
    

    <a id="h-using-sparkcontext-emptyrdd"></a>

    Using **sparkContext.**emptyRDD

    
    // Create emptyRdd using emptyRDD
    val emptyRDD = sparkContext.emptyRDD[String]
    emptyRDD.saveAsTextFile("src/main/output2/EmptyRDD2")
    

    When above code with emptyRDD is executed it creates multiple part files which are empty.

    The complete code can be downloaded from GitHub - Spark Scala Examples project

    <a id="h-related-articles"></a>

    Related Articles