SparkContext is the entry point to Spark functionality. It connects your program to the Spark cluster and allows you to create RDDs (Resilient Distributed Datasets). Think of it as the main controller for Spark jobs.
Click to reveal answer
beginner
What is SparkSession and why is it important?
SparkSession is the new entry point introduced in Spark 2.0. It combines SQLContext, HiveContext, and SparkContext into one object. It lets you work with DataFrames, SQL, and streaming easily in one place.
Click to reveal answer
intermediate
How do SparkSession and SparkContext relate to each other?
SparkSession internally manages a SparkContext. When you create a SparkSession, it creates or uses an existing SparkContext. You usually use SparkSession now because it handles more features and is simpler.
Click to reveal answer
beginner
How to create a SparkSession in PySpark?
You create a SparkSession using the builder pattern like this:<br><pre>from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('MyApp').getOrCreate()</pre><br>This starts Spark and lets you use DataFrames and SQL.
Click to reveal answer
intermediate
Why should you prefer SparkSession over SparkContext in modern Spark applications?
SparkSession provides a unified interface for all Spark features like SQL, streaming, and machine learning. It simplifies code and manages SparkContext internally, so you don't have to handle multiple contexts separately.
Click to reveal answer
What is the main role of SparkContext in Apache Spark?
AConnect your program to the Spark cluster and create RDDs
BManage SQL queries only
CVisualize data in Spark
DStore data permanently
✗ Incorrect
SparkContext connects your program to the Spark cluster and lets you create RDDs for distributed computing.
Which object should you use to work with DataFrames and SQL in Spark 2.0 and later?
ASparkContext
BSQLContext
CSparkSession
DHiveContext
✗ Incorrect
SparkSession is the unified entry point for DataFrames, SQL, and other Spark features since Spark 2.0.
A. Reassigning sc with SparkContext directly causes error.
B. Missing import for SparkContext.
C. SparkSession.builder.appName() is incorrect syntax.
D. print(sc.appName) is invalid method call.
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 A
Quick Check:
Only one SparkContext allowed; reassigning causes error [OK]
Hint: Do not create SparkContext if SparkSession exists [OK]
Common Mistakes:
Trying to create multiple SparkContext instances
Ignoring SparkContext singleton restriction
Assuming print(sc.appName) is invalid
5. You want to create a SparkSession with the app name 'DataApp' and run it locally using 4 cores. Which code snippet correctly achieves this and also retrieves the SparkContext?
hard
A. spark = SparkSession.builder.appName('DataApp').master('local[4]').getOrCreate()
sc = spark.sparkContext
B. sc = SparkContext('local[4]', 'DataApp')
spark = SparkSession(sc)
C. spark = SparkSession.create('DataApp', master='local[4]')
sc = spark.getContext()
D. spark = SparkSession.builder.master('4').appName('DataApp').getOrCreate()
sc = spark.context
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 A
Quick Check:
Use builder.appName().master('local[4]').getOrCreate() + spark.sparkContext [OK]
Hint: Set master='local[4]' and get spark.sparkContext [OK]
Common Mistakes:
Using SparkContext constructor directly instead of SparkSession.builder
Wrong master string like '4' instead of 'local[4]'
Incorrect method names like create() or getContext()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.