Apache spark 如何在spark中将对象列表转换为DF?

Apache spark 如何在spark中将对象列表转换为DF?,apache-spark,apache-spark-sql,jodatime,Apache Spark,Apache Spark Sql,Jodatime,使用spark 2.4.X,其中一个复杂对象的字段类型为Joda`DateTime` 我想把这个复杂的对象转换成DF。以下是示例代码段: import spark.implicits._ import org.joda.time.{DateTime, DateTimeZone} case class JodaTuple(n: Int, dt: org.joda.time.DateTime) val futureDate = new DateTime(2200, 1, 1, 0, 0, DateT

使用spark 2.4.X,其中一个复杂对象的字段类型为Joda`DateTime`

我想把这个复杂的对象转换成DF。以下是示例代码段:

import spark.implicits._
import org.joda.time.{DateTime, DateTimeZone}
case class JodaTuple(n: Int, dt: org.joda.time.DateTime)
val futureDate = new DateTime(2200, 1, 1, 0, 0, DateTimeZone.UTC)
List(JodaTuple(1, futureDate)).toDF("n", "t").show()
但面临例外:

java.lang.UnsupportedOperationException: No Encoder found for org.joda.time.DateTime
- field (class: "org.joda.time.DateTime", name: "dt")
- root class: "JodaTuple"
Spark使用“java.sql.Timestamp”作为“Timestamp类型”,Joda类型可以转换为:

List((1, new java.sql.Timestamp(futureDate.getMillis))).toDF("n", "t").show(false)
对于稍微复杂的对象,可以创建对象的RDD,并映射到Spark类型:

spark.sparkContext.parallelize(
   List(JodaTuple(1, futureDate))
 )
.map(r=>(r.n, new java.sql.Timestamp(r.t.getMillis)))
.toDF("n", "t").show(false)

谢谢,@pasha701。但它比你提到的要复杂一点。将元组视为跨组使用的复杂对象,不想更改数据类型。我不能使用MaP()函数将对象转换为元组。