sbt编译spark程序提示value toDF is not a member of Seq()

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挪到了外面,然后好了 - -。

posted @ 2017-11-28 12:08  wswang  阅读(2513)  评论(0编辑  收藏  举报