Importing spark.implicits._ in scala

前端 未结 8 1134
感情败类
感情败类 2020-12-25 09:33

I am trying to import spark.implicits._ Apparently, this is an object inside a class in scala. when i import it in a method like so:

def f() = {
  val spark          


        
相关标签:
8条回答
  • 2020-12-25 10:33

    Create a sparksession object and use the spark.implicit._ just before you want to convert any rdd to datasets.

    Like this:

    val spark = SparkSession
          .builder
          .appName("SparkSQL")
          .master("local[*]")
          .getOrCreate()
    
    import spark.implicits._
    val someDataset = someRdd.toDS
    
    0 讨论(0)
  • 2020-12-25 10:34

    It has to do something with using val vs var in scala.

    E.g. following does not work

    var sparkSession = new SparkSession.Builder().appName("my-app").config(sparkConf).getOrCreate
    import sparkSession.implicits._
    

    But following does

    sparkSession = new SparkSession.Builder().appName("my-app").config(sparkConf).getOrCreate
    val sparkSessionConst = sparkSession
    import sparkSessionConst.implicits._
    

    I am very familiar with scala so I can only guess that the reasoning is same as why we can only use outer variables declared final inside a closure in java.

    0 讨论(0)
提交回复
热议问题