尝试将数据帧行映射到更新行时出现编码器错误

Encoder error while trying to map dataframe row to updated row

当我尝试在我的代码中执行如下所述的相同操作时

dataframe.map(row => {
  val row1 = row.getAs[String](1)
  val make = if (row1.toLowerCase == "tesla") "S" else row1
  Row(row(0),make,row(2))
})

我从这里获取了以上参考资料: 但是我收到编码器错误

Unable to find encoder for type stored in a Dataset. Primitive types (Int, S tring, etc) and Product types (case classes) are supported by importing spark.im plicits._ Support for serializing other types will be added in future releases.

注意:我用的是spark 2.0!

这里没有什么意外。您正在尝试使用由 Spark 1.x 编写且 Spark 2.0 不再支持的代码:

  • 在 1.x DataFrame.map 中是 ((Row) ⇒ T)(ClassTag[T]) ⇒ RDD[T]
  • 在 2.x Dataset[Row].map 中是 ((Row) ⇒ T)(Encoder[T]) ⇒ Dataset[T]

老实说,它在 1.x 中也没有多大意义。独立于版本,您可以简单地使用 DataFrame API:

import org.apache.spark.sql.functions.{when, lower}

val df = Seq(
  (2012, "Tesla", "S"), (1997, "Ford", "E350"),
  (2015, "Chevy", "Volt")
).toDF("year", "make", "model")

df.withColumn("make", when(lower($"make") === "tesla", "S").otherwise($"make"))

如果你真的想使用 map 你应该使用静态类型 Dataset:

import spark.implicits._

case class Record(year: Int, make: String, model: String)

df.as[Record].map {
  case tesla if tesla.make.toLowerCase == "tesla" => tesla.copy(make = "S")
  case rec => rec
}

或至少return一个将具有隐式编码器的对象:

df.map {
  case Row(year: Int, make: String, model: String) => 
    (year, if(make.toLowerCase == "tesla") "S" else make, model)
}

最后,如果出于某些 完全疯狂 的原因你真的想映射 Dataset[Row] 你必须提供所需的编码器:

import org.apache.spark.sql.catalyst.encoders.RowEncoder
import org.apache.spark.sql.types._
import org.apache.spark.sql.Row

// Yup, it would be possible to reuse df.schema here
val schema = StructType(Seq(
  StructField("year", IntegerType),
  StructField("make", StringType),
  StructField("model", StringType)
))

val encoder = RowEncoder(schema)

df.map {
  case Row(year, make: String, model) if make.toLowerCase == "tesla" => 
    Row(year, "S", model)
  case row => row
} (encoder)

对于预先知道数据帧模式的场景,@zero323 给出的答案是解决方案

但对于具有动态模式/或将多个数据帧传递给通用函数的场景: 从 1.6.1 从 2.2.0

迁移时,以下代码对我们有用
import org.apache.spark.sql.Row

val df = Seq(
   (2012, "Tesla", "S"), (1997, "Ford", "E350"),
   (2015, "Chevy", "Volt")
 ).toDF("year", "make", "model")

val data = df.rdd.map(row => {
  val row1 = row.getAs[String](1)
  val make = if (row1.toLowerCase == "tesla") "S" else row1
  Row(row(0),make,row(2))
})

此代码在两个版本的 spark 上执行。

缺点:提供了优化 by spark on dataframe/datasets api 不会被应用。

在我的 spark 2.4.4 版本的情况下,我必须导入隐式。这是一般性的回答

val spark2 = spark
import spark2.implicits._

val data = df.rdd.map(row => my_func(row))

其中my_func做了一些操作。

只是添加其他一些 important-to-know 点以便更好地理解其他答案(尤其是 @zero323 关于 的答案的 最后一点 map 超过 Dataset[Row]):

  • 首先,Dataframe.map给你一个Dataset(更具体地说,Dataset[T],而不是Dataset[Row])!
  • Dataset[T]总是需要编码器,这就是这句话“Dataset[Row].map((Row) ⇒ T)(Encoder[T]) ⇒ Dataset[T]”的意思。
  • 确实有lots of encoders predefined already by Spark (which can be imported by doing import spark.implicits._), but still the list would not be able to cover many domain specific types that developers may create, in which case you need to .
  • 在本页的具体示例中,df.map returns 一个 Row 类型为 Dataset,等一下,Row 类型是不在具有 Spark 预定义编码器的类型列表中,因此您将自己创建一个。
  • 而且我承认为 Row 类型创建编码器与 中描述的方法有点不同,您必须使用 RowEncoder 需要 [=28] =] 作为描述行类型的参数,就像@zero323 上面提供的那样:
// this describes the internal type of a row
val schema = StructType(Seq(StructField("year", IntegerType), StructField("make", StringType), StructField("model", StringType)))

// and this completes the creation of encoder
// for the type `Row` with internal schema described above
val encoder = RowEncoder(schema)