【发布时间】:2016-09-09 21:11:51
【问题描述】:
我正在尝试在 Spark 中运行多个单元测试,并已从源代码中复制(并稍作修改)该位:
import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}
import org.scalatest.{BeforeAndAfterAll, Suite}
trait SharedSparkContext extends BeforeAndAfterAll {
self: Suite =>
@transient private var _sc: SparkContext = _
@transient private var _sqlContext: SQLContext = _
def sc: SparkContext = _sc
def sqlContext: SQLContext = _sqlContext
private var conf = new SparkConf(false)
override def beforeAll() {
super.beforeAll()
_sc = new SparkContext("local[*]", "Test Suites", conf)
_sqlContext = new SQLContext(_sc)
}
override def afterAll() {
try {
LocalSparkContext.stop(_sc)
_sc = null
} finally {
super.afterAll()
}
}
}
带有伴生对象的 LocalSparkContext 类只是从源中复制而来。
我想过如下使用它,它告诉我stable identifier required因为defsqlContext没有成员implicits:
class MySuite extends FlatSpec with SharedSparkContext {
import sqlContext.implicits._
// ...
}
我尝试用以下内容替换它,但这给了我空指针异常:
class MySuite extends FlatSpec with SharedSparkContext {
val sqlCtxt = sqlContext
import sqlCtxt.implicits._
// ...
}
我使用的是 Spark 1.4.1,我设置了parallelExecution in test := false。
如何让它工作(不使用额外的包)?
【问题讨论】:
标签: scala apache-spark