【发布时间】:2017-06-01 03:12:16
【问题描述】:
我正在将我的一个项目从 Spark 1.6 升级到 Spark 2.0.1。以下代码适用于 Spark 1.6,但不适用于 2.0.1:
def count(df: DataFrame): DataFrame = {
val sqlContext = df.sqlContext
import sqlContext.implicits._
df.map { case Row(userId: String, itemId: String, count: Double) =>
(userId, itemId, count)
}.toDF("userId", "itemId", "count")
}
这是错误信息:
Error:(53, 12) 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.
df.map { case Row(userId: String, itemId: String, count: Double) =>
^
Error:(53, 12) not enough arguments for method map: (implicit evidence$7: org.apache.spark.sql.Encoder[(String, String, Double)])org.apache.spark.sql.Dataset[(String, String, Double)].
Unspecified value parameter evidence$7.
df.map { case Row(userId: String, itemId: String, count: Double) =>
^
我尝试使用df.rdd.map而不是df.map,然后得到以下错误:
Error:(55, 7) value toDF is not a member of org.apache.spark.rdd.RDD[(String, String, Double)]
possible cause: maybe a semicolon is missing before `value toDF'?
}.toDF("userId", "itemId", "count")
^
如何在 Spark 2.0 中将元组的 RDD 转换为数据帧?
【问题讨论】:
-
您是否尝试导入
importing spark.implicits._? -
@rogue-one 是的,尝试将
val sqlContext = df.sqlContext import sqlContext.implicits._更改为val spark = df.sparkSession import spark.implicits._,但得到了同样的错误。
标签: scala apache-spark apache-spark-sql spark-dataframe rdd