Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve SparkContext from SparkSession

I'm running a spark batch job and uses SparkSession as I need a lot of spark-sql features to process in each of my components. The SparkContext is initialized in my parent component and been passed to the child components as SparkSession.

In one of my child components, I wanted to add two more configurations to my SparkContext. Hence, I need to retrieve the SparkContext from the SparkSession, stop it and recreate the SparkSession with the additional configuration. To do so, how can I retrieve SparkContext from SparkSession?

like image 609
Naveen Balasubramanian Avatar asked Oct 10 '17 06:10

Naveen Balasubramanian


People also ask

How do I get existing SparkContext?

By using getAll() method of SparkConf you can get all current active Spark/PySpark SparkContext settings, you can also use get() method to get value for specific settings.

What is the difference between SparkSession and SparkContext?

SparkSession vs SparkContext – Since earlier versions of Spark or Pyspark, SparkContext (JavaSparkContext for Java) is an entry point to Spark programming with RDD and to connect to Spark Cluster, Since Spark 2.0 SparkSession has been introduced and became an entry point to start programming with DataFrame and Dataset.

How do I import a SparkSession package into Pyspark?

For an existing SparkConf, use conf parameter. >>> from pyspark. conf import SparkConf >>> SparkSession.

Should I use SparkSession or SparkContext?

Once the SparkSession is instantiated, we can configure Spark's run-time config properties. Spark 2.0. 0 onwards, it is better to use sparkSession as it provides access to all the spark Functionalities that sparkContext does. Also, it provides APIs to work on DataFrames and Datasets.


1 Answers

Just to post as an answer - the SparkContext can be accessed from SparkSession using spark.sparkContext (no parenthesis)

like image 81
ayplam Avatar answered Sep 17 '22 17:09

ayplam