【发布时间】:2017-07-31 19:39:58
【问题描述】:
请注意:虽然这个问题提到了 Spark (2.1),但我认为这实际上是一个 Scala (2.11) 问题,任何精通 Scala 的开发人员都能够回答它!
我有以下代码创建一个 Spark Dataset(基本上是一个 2D 表)并逐行迭代它。如果特定行的username 列的值为“fizzbuzz”,那么我想设置一个在迭代器外部定义的变量,并在行迭代完成后使用该变量:
val myDataset = sqlContext
.read
.format("org.apache.spark.sql.cassandra")
.options(Map("table" -> "mytable", "keyspace" -> "mykeyspace"))
.load()
var foobar : String
myDataset.collect().foreach(rec =>
if(rec.getAs("username") == "fizzbuzz") {
foobar = rec.getAs("foobarval")
}
)
if(foobar == null) {
throw new Exception("The fizzbuzz user was not found.")
}
当我运行它时,我得到以下异常:
error: class $iw needs to be abstract, since:
it has 2 unimplemented members.
/** As seen from class $iw, the missing signatures are as follows.
* For convenience, these are usable as stub implementations.
*/
def foobar=(x$1: String): Unit = ???
class $iw extends Serializable {
^
我得到这个有什么特别的原因吗?
【问题讨论】:
标签: scala apache-spark iterator