美文网首页
189、Spark 2.0之Dataset开发详解-typed操

189、Spark 2.0之Dataset开发详解-typed操

作者: ZFH__ZJ | 来源:发表于2019-02-11 21:32 被阅读0次

    coalesce和repartition操作,都是用来重新定义分区的
    区别在于:coalesce,只能用于减少分区数量,而且可以选择不发生shuffle
    repartiton,可以增加分区,也可以减少分区,必须会发生shuffle,相当于是进行了一次重分区操作

    代码

    object TypedOperation {
    
      case class Employee(name: String, age: Long, depId: Long, gender: String, salary: Long)
    
      def main(args: Array[String]): Unit = {
        val sparkSession = SparkSession
          .builder()
          .appName("BasicOperation")
          .master("local")
          .getOrCreate()
    
        import sparkSession.implicits._
    
        val employeePath = this.getClass.getClassLoader.getResource("employee.json").getPath
    
        val employeeDF = sparkSession.read.json(employeePath)
    
        val employeeDS = employeeDF.as[Employee]
        println(employeeDS.rdd.partitions.size)
    
        val employeeDSRepartitioned = employeeDS.repartition(5)
        println(employeeDSRepartitioned.rdd.partitions.size)
    
        val employeeDSCoalesced = employeeDSRepartitioned.coalesce(3)
        println(employeeDSCoalesced.rdd.partitions.size)
      }
    }
    
    

    相关文章

      网友评论

          本文标题:189、Spark 2.0之Dataset开发详解-typed操

          本文链接:https://www.haomeiwen.com/subject/yikqeqtx.html