当使用 SparkSession 加载/写入某些数据时发生连续几次失败时,Spark 最终会停止工作,立即返回“无法在停止的 SparkContext 上调用方法”,无论可能的结果如何。
经过研究,我在 spark's documentation 上发现了“spark.deploy.maxExecutorRetries” ,其中指出:
If an application experiences more than spark.deploy.maxExecutorRetries failures in a row, no executors successfully start running in between those failures, and the application has no running executors then the standalone cluster manager will remove the application and mark it as failed. To disable this automatic removal, set spark.deploy.maxExecutorRetries to -1.
所以我尝试了这个:
public static SparkSession getNewSparkSession() {
return SparkSession.builder().appName("SparkJdbcSuite").master("local[*]").config("spark.deploy.maxExecutorRetries", "-1").getOrCreate();
}
我预计无论连续失败多少次,它都会继续尝试,但看起来我没有正确应用参数。
我还尝试设置 -1
、3
、"3"
,并使用 SparkConf,set("spark .deploy.maxExecutorRetries", "-1")
,但似乎没有任何作用。
有人知道如何正确设置此属性吗?
最佳答案
通过这样做解决了这个问题:
SparkConf config = new SparkConf().setMaster("local[*]").setAppName("SparkContext");
SparkContext context = SparkContext.getOrCreate(config);
return SparkSession.builder().sparkContext(context).getOrCreate();
并在使用spark后将其添加到finally
block 中:
SparkSession session = sparkSession.get();
session.sparkContext().stop();
session.close();
看起来 SparkSession 正在尝试使用已停止的上下文,这样它总是会获得一个新的上下文,除非它正在执行某些操作。但是,如果发生无法在已停止的 SparkContext 上调用方法
,它就会停止停止
,并在下次使用新的 SparkContext。
关于java - 在 SparkSession 上设置 maxExecutorRetries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57467532/