How to create SparkSession from existing SparkContext

前端 未结 6 1011
时光取名叫无心
时光取名叫无心 2020-12-30 20:47

I have a Spark application which using Spark 2.0 new API with SparkSession. I am building this application on top of the another application which is using

6条回答
  •  梦谈多话
    2020-12-30 21:21

    Deriving the SparkSession object out of SparkContext or even SparkConf is easy. Just that you might find the API to be slightly convoluted. Here's an example (I'm using Spark 2.4 but this should work in the older 2.x releases as well):

    // If you already have SparkContext stored in `sc`
    val spark = SparkSession.builder.config(sc.getConf).getOrCreate()
    
    // Another example which builds a SparkConf, SparkContext and SparkSession
    val conf = new SparkConf().setAppName("spark-test").setMaster("local[2]")
    val sc = new SparkContext(conf)
    val spark = SparkSession.builder.config(sc.getConf).getOrCreate()
    

    Hope that helps!

提交回复
热议问题