sbt編譯spark程序提示value toDF is not a member of Seq()
前提
使用Scala編寫的Spark程序,在sbt編譯打包的時候提示value toDF is not a member of Seq(),出問題的代碼如下:
val urlDS = Seq(STU(age, count)).toDS()
其中STU是一個定義的case class,定義如下:
case class STU(age: Int, count: Int)
查找原因
開始以為是toDS()
用錯了,但是去官網看,用法沒有問題,如下:
case class Person(name: String, age: Long)
// Encoders are created for case classes
val caseClassDS = Seq(Person("Andy", 32)).toDS()
caseClassDS.show()
// +----+---+
// |name|age|
// +----+---+
// |Andy| 32|
// +----+---+
同時,如果我把代碼拷貝到spark-shell里運行,完全木有問題,這就很奇怪了。
最終原因
用法沒錯,於是我只能去谷歌了,找到了這個頁面,發現問題基本上是一個問題,該引入的包也都引用了,但是還有這個問題,但是仔細看還有一句話:
Move case class outside of the method: case class, by use of which you define the schema of the DataFrame, should be defined outside of the method needing it. You can read more about it here: https://issues.scala-lang.org/browse/SI-6649
意思是,case class的定義要在引用case class函數的外面。因為我只有一個main函數,所以把case class挪到了外面,然后好了 - -。