How I can check sparkSession status in my py spark code ? Requirement is to check whether sparkSession is active or not. if sparksession is not active create another spark session and call some function
I am writing and running this code in Jupyter notebook.
spark = SparkSession.builder.master("yarn") \
.config("spark.dynamicAllocation.enabled", "true") \
.config("spark.serializer",
"org.apache.spark.serializer.KryoSerializer")
.config("spark.shuffle.spill.compress", "true")
.config("spark.shuffle.service.enabled", "true")
.config("spark.io.compression.codec", "snappy")
.config("spark.kryoserializer.buffer.max", "250m") \
.config("spark.driver.memory", memory) \
.config("spark.driver.cores", cores) \
.config("spark.executor.cores", cores) \
.config("spark.executor.memory", memory) \
.config("spark.executor.instances", cores) \
.enableHiveSupport()\
.getOrCreate()
- spark prints the sparkSession details
3.
if(spark):
print("yes")
else:
print("no")
prints "yes"
spark.stop()
it stops spark application -- I checked in UI
but when I am running code in third step again
5.
if(spark):
print("yes")
else:
print("no")
Prints "yes as output
- but it do spark
error : AttributeError: 'NoneType' object has no attribute 'sc'
- But weird thing which I saw when I ran my next command
df = spark.read.csv(file_name)
It created another application and start execution of the code.
I am trying to understand whether sparkSession was killed or not as
Observation: a. if(spark) is giving TRUE as it it is prinung the kines underneath this. b. when I just write "spark" -- gave me error c. spark.read.csv ---- did not give any error and started a new application but threw the error after a while -- "Cannot call methods on a stopped SparkContext."
requirement was to check if some how sparkSession is stopped or failed while my code/application is running .. it should automatically restart
I was thinking to write
def func1:
create spark session
code to execute
def func2:
while spark is active :
time.sleep(200)
if !spark is active:
func1()
func1()
func2()