【发布时间】:2015-07-25 03:46:27
【问题描述】:
来自这个article about spark testing,有一些示例代码:
class SparkExampleSpec extends FlatSpec with BeforeAndAfter {
private val master = "local[2]"
private val appName = "example-spark"
private var sc: SparkContext = _
before {
val conf = new SparkConf()
.setMaster(master)
.setAppName(appName)
sc = new SparkContext(conf)
}
after {
if (sc != null) {
sc.stop()
}
}
(...)
它可以工作,但是有一个可以为空的变量sc。这里是合理的,但我还是想避免它。
我尝试使用:
private var sc: Option[SparkContext] = None
before {
sc = Some(new SparkContext(conf))
}
after {
sc.foreach(_.stop())
}
(...)
但问题是,我必须在测试中使用sc作为Option[SparkContext],这不像普通的SparkContext那样方便
有没有办法让测试继续进行,但我们可以使用val sc: SparkContext?
(我知道在 specs2 中可以,但不确定如何在 scalatest 中进行,我们现在必须使用 scalatest)
【问题讨论】: