python-SparkSession和上下文混淆
作者:互联网
我有一个pyspark 2.0.0脚本,定义了以下会话:
spark = SparkSession \
.builder \
.appName("Python Spark") \
.master("local[*]")\
.config("spark.some.config.option", "some-value") \
.getOrCreate()
我训练了一个随机森林模型,我想保存它.因此,我正在调用以下方法:
model_rf.save( spark, "/home/Desktop")
但是它会引发以下编译错误:
TypeError: sc should be a SparkContext, got type <class 'pyspark.sql.session.SparkSession'>
当我定义一个Spark上下文时,如下所示:
from pyspark import SparkContext
sc =SparkContext()
model_rf.save( sc, "/home/Desktop")
我收到错误消息:
Cannot run multiple SparkContexts at once; existing SparkContext(app=Python Spark, master=local[*]) created by getOrCreate at <ipython-input-1-c5f83810f880>:24
解决方法:
使用spark.sparkContext(SparkSession对象将具有sparkContext)
model_rf.save( spark.sparkContext, "/home/Desktop")
标签:apache-spark,apache-spark-mllib,save,python 来源: https://codeday.me/bug/20191026/1936357.html