sc我有一个带有高度定制的 SparkConf() 的SparkContext 。如何使用 SparkContext 创建 SparkSession?我发现这篇文章:https ://stackoverflow.com/a/53633430/201657展示了如何使用 Scala 来做到这一点:
val spark = SparkSession.builder.config(sc.getConf).getOrCreate()
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用 PySpark 应用相同的技术时:
from pyspark.sql import SparkSession
spark = SparkSession.builder.config(sc.getConf()).enableHiveSupport().getOrCreate()
Run Code Online (Sandbox Code Playgroud)
它因错误而失败
AttributeError:“SparkConf”对象没有属性“_get_object_id”
正如我所说,我想SparkConf在 SparkSession 中使用与 SparkContext 中使用的相同的内容。我该怎么做?
更新
我做了一些摆弄:
from pyspark.sql import SparkSession
spark = SparkSession.builder.enableHiveSupport().getOrCreate()
sc.getConf().getAll() == spark.sparkContext.getConf().getAll()
Run Code Online (Sandbox Code Playgroud)
回报
真的
so the SparkConf of both the SparkContext & the SparkSession are the same. My assumption from this is that SparkSession.builder.getOrCreate() will use an existing SparkContext if it exists. Am I correct?
你的假设:
我的假设是 SparkSession.builder.getOrCreate() 将使用现有的 SparkContext(如果存在)。我对么?
是正确的。但是,您也可以将 SparkContext(通过 SparkConf 设置自定义配置)显式传递到 SparkSession。
from pyspark.sql import SparkSession
from pyspark import SparkConf, SparkContext
spark_conf = SparkConf()
#Define custom configuration properties
spark_conf.set("spark.executor.memory", "2g")
spark_conf.set("spark.executor.cores", "4")
context = SparkContext(conf=spark_conf)
#Define custom context properties
context.setCheckpointDir("checkpoints")
spark = (SparkSession(context)
.builder
.appName("DefaultSparkSession")
.getOrCreate())
print(spark.sparkContext._conf.getAll())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5055 次 |
| 最近记录: |