SparkSession and SparkContext help you start working with Apache Spark. They let you connect to Spark and use its tools to analyze big data easily.
SparkSession and SparkContext in Apache Spark
from pyspark.sql import SparkSession spark = SparkSession.builder \ .appName("MyApp") \ .getOrCreate() sc = spark.sparkContext
SparkSession is the main entry point to work with Spark SQL and DataFrames.
SparkContext is the older core object to connect to Spark, now accessed via SparkSession.
spark = SparkSession.builder.appName("TestApp").getOrCreate()
sc = spark.sparkContextspark = SparkSession.builder.master("local").appName("LocalApp").getOrCreate()
sc = SparkSession.builder.getOrCreate().sparkContext
This program creates a SparkSession and SparkContext, prints the app name, then creates and shows a small DataFrame.
from pyspark.sql import SparkSession # Create SparkSession spark = SparkSession.builder.appName("ExampleApp").getOrCreate() # Access SparkContext sc = spark.sparkContext # Print app name from SparkContext print(f"App Name: {sc.appName}") # Create a simple DataFrame data = [(1, "Alice"), (2, "Bob"), (3, "Cathy")] columns = ["id", "name"] df = spark.createDataFrame(data, columns) # Show DataFrame content print("DataFrame content:") df.show()
Always create only one SparkSession per application to avoid conflicts.
SparkContext is available inside SparkSession as spark.sparkContext.
Use SparkSession for all new Spark programs; SparkContext is mostly for backward compatibility.
SparkSession is the main way to work with Spark SQL and DataFrames.
SparkContext connects your program to the Spark cluster and is accessed via SparkSession.
Create SparkSession with builder, set app name and master if needed, then get SparkContext from it.
Practice
SparkSession in Apache Spark?Solution
Step 1: Understand SparkSession's purpose
SparkSession is designed as the main interface for working with structured data using Spark SQL and DataFrames.Step 2: Differentiate from other components
SparkContext manages cluster connection but SparkSession provides higher-level APIs for SQL and DataFrames.Final Answer:
It is the main entry point to work with Spark SQL and DataFrames. -> Option CQuick Check:
SparkSession = main SQL/DataFrame entry [OK]
- Confusing SparkSession with SparkContext's cluster management
- Thinking SparkSession stores data permanently
- Believing SparkSession removes need for SparkContext
Solution
Step 1: Recall SparkSession creation syntax
The correct way is to use SparkSession.builder, set app name, then call getOrCreate() to create or get existing session.Step 2: Identify incorrect options
SparkContext does not have builder or getOrCreate methods; SparkSession.create is not valid syntax.Final Answer:
spark = SparkSession.builder.appName('MyApp').getOrCreate() -> Option BQuick Check:
Use SparkSession.builder.appName().getOrCreate() [OK]
- Using SparkContext instead of SparkSession to create session
- Calling non-existent create() method
- Missing getOrCreate() at the end
print(sc.appName)?
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('TestApp').master('local').getOrCreate()
sc = spark.sparkContext
print(sc.appName)Solution
Step 1: Understand appName assignment
The appName 'TestApp' is set when building SparkSession and is passed to SparkContext.Step 2: Access SparkContext appName property
sc.appName returns the application name set, which is 'TestApp'.Final Answer:
TestApp -> Option DQuick Check:
sc.appName = 'TestApp' [OK]
- Confusing master URL with appName
- Expecting sc.appName to be 'SparkContext'
- Assuming sc.appName is None by default
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('App').getOrCreate()
sc = spark.sparkContext
sc = SparkContext('local', 'App')
print(sc.appName)Solution
Step 1: Analyze sc reassignment
After creating SparkSession and getting sc, the code tries to create a new SparkContext directly, which is not allowed if one already exists.Step 2: Understand SparkContext singleton rule
Only one SparkContext can exist per JVM; creating another causes an error.Final Answer:
Reassigning sc with SparkContext directly causes error. -> Option AQuick Check:
Only one SparkContext allowed; reassigning causes error [OK]
- Trying to create multiple SparkContext instances
- Ignoring SparkContext singleton restriction
- Assuming print(sc.appName) is invalid
Solution
Step 1: Use SparkSession.builder with appName and master
The correct way is to use builder, set appName to 'DataApp' and master to 'local[4]' for 4 cores, then call getOrCreate().Step 2: Retrieve SparkContext from SparkSession
Access SparkContext via spark.sparkContext property after creating SparkSession.Final Answer:
spark = SparkSession.builder.appName('DataApp').master('local[4]').getOrCreate() sc = spark.sparkContext -> Option AQuick Check:
Use builder.appName().master('local[4]').getOrCreate() + spark.sparkContext [OK]
- Using SparkContext constructor directly instead of SparkSession.builder
- Wrong master string like '4' instead of 'local[4]'
- Incorrect method names like create() or getContext()
Ready to prove it?
Pass the test and this pattern moves unknown
