from pyspark.sql import SparkSession
class SparkSessionUtil:
@staticmethod
def create_spark_session(app_name: str, master: str = "local[*]") -> SparkSession:
"""
Creates a SparkSession with the given app name and master URL.
:param app_name: Name of the Spark application.
:param master: Master URL to connect to. Default is "local[*]".
:return: A SparkSession instance.
"""
spark = SparkSession.builder \
.appName(app_name) \
.master(master) \
.getOrCreate()
return spark
@staticmethod
def stop_spark_session(spark: SparkSession) -> None:
"""
Stops the given SparkSession.
:param spark: The SparkSession to stop.
"""
spark.stop()
from spark_session_util import SparkSessionUtil
# Create a SparkSession
spark = SparkSessionUtil.create_spark_session("MyApp", "local[*]")
# Perform your operations here, e.g., read data, process data, write data
# Stop the SparkSession
SparkSessionUtil.stop_spark_session(spark)
【无标题】
于 2023-04-11 09:57:18 首次发布
该代码定义了一个名为SparkSessionUtil的类,包含两个静态方法。create_spark_session方法用于根据给定的appName和masterURL创建一个SparkSession。默认master设置为local[*]。stop_spark_session方法则用于停止一个SparkSession实例。示例展示了如何创建一个SparkSession进行数据操作,然后停止它。
摘要由CSDN通过智能技术生成