我在 Zeppelin 笔记本上使用 Spark,而 groupByKey() 似乎不起作用。
这段代码:
df.groupByKey(row => row.getLong(0))
.mapGroups((key, iterable) => println(key))
给了我这个错误(可能是编译错误,因为它在我正在处理的数据集非常大时立即显示):
error: Unable to find encoder for type stored in a Dataset. Primitive types (Int, String, etc) and Product types (case classes) are supported by importing spark.implicits._ Support for serializing other types will be added in future releases.
我尝试添加一个案例类并将我的所有行映射到其中,但仍然出现相同的错误
import spark.implicits._
case class DFRow(profileId: Long, jobId: String, state: String)
def getDFRow(row: Row):DFRow = {
return DFRow(row.getLong(row.fieldIndex("item0")),
row.getString(row.fieldIndex("item1")),
row.getString(row.fieldIndex("item2")))
}
df.map(DFRow(_))
.groupByKey(row => row.getLong(0))
.mapGroups((key, iterable) => println(key))
我的数据框的架构是:
root
|-- item0: long (nullable = true)
|-- item1: string (nullable = true)
|-- item2: string (nullable = true)