繁体   English   中英

Encoders.product[of a scala trait].schema in spark

[英]Encoders.product[of a scala trait ].schema in spark

如何从特征创建火花模式? 考虑一个特征:

trait A{
val name:String
val size:String
}

作为:

Encoders.product[A].schema

给出:

Error:type arguments do not conform to method product's type parameter bounds [T <: Product]

此外,字段数将超过 case class parameters > 200 的限制

案例 class 确实支持超过 22 列,请尝试在所有其他类/对象之外创建。 如果您需要创建一个包含大量字段的 dataframe 架构,这应该可行。

val schema: StructType = StructType(
    Array(
      StructField(name = "name", StringType),
      StructField(name = "size", StringType)
    )
 )
val data = Seq(Row("Ramanan","29"))
spark.createDataFrame(spark.sparkContext.parallelize(data),schema).show()

我不能告诉你为什么这不起作用的所有细节,但我提出了一个我们在 Scala Spark 项目中经常使用的稍微替代的解决方案。

Encoders.product的签名看起来像

product[T <: scala.Product](implicit evidence$5 : scala.reflect.runtime.universe.TypeTag[T])

这意味着 tt 期望 class 扩展Product特征和隐式 TypeTag。

您可以创建case class而不是特征,因为案例类会自动扩展Product (和Serializable )。

为了获得一个模式,你可以这样做:

case class A (
  val name: String,
  val size: String
)

def createSchema[T <: Product]()(implicit tag: scala.reflect.runtime.universe.TypeTag[T]) = Encoders.product[T].schema
val schema = createSchema[A]()
schema.printTreeString()

/*
root
 |-- name: string (nullable = true)
 |-- size: string (nullable = true)
*/

正如一开始所说,我无法解释所有细节,只是提供一个可行的解决方案并希望它能满足您的需求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM