简体   繁体   English

使用pyspark创建sparksession后是否需要停止spark?

[英]Do I need to stop spark after creating sparksession using pyspark?

So I have this:所以我有这个:

from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("NewSpark").getOrCreate()

I heard you need to stop spark once you're done but is this necessary in my case since it's just a python program?我听说你完成后需要停止 spark,但在我的情况下这是必要的,因为它只是一个 python 程序?

The session will be closed if the spark object gets destroyed or if the script exits.如果spark对象被破坏或脚本退出,会话将关闭。 So you shouldn't need to worry about "dangling connections" or anything like that.因此,您无需担心“悬空连接”或类似问题。

However, if you have a bunch of non-spark work that you want to do at the end of the script, it may still be a good idea to stop the session early to avoid holding that connection open.但是,如果您想在脚本结束时执行大量非 Spark 工作,那么提前停止会话以避免保持该连接打开可能仍然是一个好主意。

Note that you can use the SparkSession object as a context manager to automatically stop it at the end of a scope:请注意,您可以使用SparkSession对象作为上下文管理器在范围结束时自动停止它:

with SparkSession.builder.appName("NewSpark").getOrCreate() as spark:
    # do stuff
    # ...

# spark.stop() gets called automatically here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 pyspark 在 Spark 2.0 中构建 sparkSession? - How to build a sparkSession in Spark 2.0 using pyspark? 使用 pyspark 创建 SparkSession 时出现问题 - Problem while creating SparkSession using pyspark 为什么PySpark创建SparkSession时找不到spark-submit? - Why does PySpark not find spark-submit when creating a SparkSession? 在 PySpark 中创建 SparkSession 时出错 - Error when creating SparkSession in PySpark 如何使用 PySpark 和 SparkSession 设置到 HIVE 的连接(如何添加用户名和密码)? - How to setup connection to HIVE using PySpark and SparkSession (How do I add username and password)? Spark 3.0.0 创建 SparkSession 时出错:pyspark.sql.utils.IllegalArgumentException:<exception str() failed></exception> - Spark 3.0.0 error creating SparkSession: pyspark.sql.utils.IllegalArgumentException: <exception str() failed> 一段时间后停止 Spark 会话 - Pyspark - Stop Spark Session after some time - Pyspark 如何在 dataframe 中使用 sparkSession 使用 spark-cassandra-connector 在 pyspark 中写入 - how to use sparkSession in dataframe write in pyspark using spark-cassandra-connector 如何将新列添加到 Spark DataFrame(使用 PySpark)? - How do I add a new column to a Spark DataFrame (using PySpark)? 系统在使用 PySpark 创建 SparkSession 时找不到指定的路由 - System cannot find the specified route on creating SparkSession with PySpark
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM