【发布时间】:2017-04-01 14:01:29
【问题描述】:
我在 IntelliJ 中有以下工作表:
import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}
/** Lazily instantiated singleton instance of SQLContext */
object SQLContextSingleton {
@transient private var instance: SQLContext = _
def getInstance(sparkContext: SparkContext): SQLContext = {
if (instance == null) {
instance = new SQLContext(sparkContext)
}
instance
}
}
val conf = new SparkConf().
setAppName("Scala Wooksheet").
setMaster("local[*]")
val sc = new SparkContext(conf)
val sqlContext = new SQLContext(sc)
val df = sqlContext.read.json("/Users/someuser/some.json")
df.show
此代码在 REPL 中有效,但似乎只在第一次运行(还有一些其他错误)。以后的每一次,错误都是:
16/04/13 11:04:57 WARN SparkContext: Another SparkContext is being constructed (or threw an exception in its constructor). This may indicate an error, since only one SparkContext may be running in this JVM (see SPARK-2243). The other SparkContext was created at:
org.apache.spark.SparkContext.<init>(SparkContext.scala:82)
如何找到已经在使用的上下文?
注意:我听到其他人说要使用conf.set("spark.driver.allowMultipleContexts","true"),但这似乎是增加内存使用量的解决方案(如未收集的垃圾)。
有没有更好的办法?
【问题讨论】:
-
我想如果你在工作表的最后一行添加
sc.close(),你会没事的——每次执行都会创建一个 SparkContext 并关闭它,所以不会有多个运行. -
@TzachZohar -- sc 好像没有 close 方法。
-
糟糕,意思是
stop(),对不起 -
@TzachZohar - 谢谢......我仍然需要确保在到达那一点之前我不会崩溃。可能使用 try /catch / finally。必须有一个更常见或更优雅的解决方案。 (???)
-
另一个想法......也许问题不在于关闭 SparkContext,而是“如何找到已经打开的 SparkContext?”
标签: scala intellij-idea apache-spark apache-spark-sql