Skip to content

Commit a7a1027

Browse files
author
Andrew Or
committed
Don't create SparkContext before SparkSession
1 parent 3052639 commit a7a1027

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

python/pyspark/shell.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@
3535
if os.environ.get("SPARK_EXECUTOR_URI"):
3636
SparkContext.setSystemProperty("spark.executor.uri", os.environ["SPARK_EXECUTOR_URI"])
3737

38-
sc = SparkContext()
39-
atexit.register(lambda: sc.stop())
38+
SparkContext._ensure_initialized()
4039

4140
try:
4241
# Try to access HiveConf, it will raise exception if Hive is not added
43-
sc._jvm.org.apache.hadoop.hive.conf.HiveConf()
42+
SparkContext._jvm.org.apache.hadoop.hive.conf.HiveConf()
4443
spark = SparkSession.builder\
4544
.enableHiveSupport()\
4645
.getOrCreate()
@@ -49,6 +48,9 @@
4948
except TypeError:
5049
spark = SparkSession(sc)
5150

51+
sc = spark.sparkContext
52+
atexit.register(lambda: sc.stop())
53+
5254
# for compatibility
5355
sqlContext = spark._wrapped
5456
sqlCtx = sqlContext

python/pyspark/sql/session.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ def newSession(self):
191191
"""
192192
return self.__class__(self._sc, self._jsparkSession.newSession())
193193

194+
@property
195+
@since(2.0)
196+
def sparkContext(self):
197+
"""Returns the underlying :class:`SparkContext`."""
198+
return self._sc
199+
194200
@property
195201
@since(2.0)
196202
def conf(self):

0 commit comments

Comments
 (0)