【发布时间】:2021-10-08 03:44:13
【问题描述】:
我正在创建一个数据框,该数据框被初始化为一些列设置为空。在写出之前,我将数据框键入为案例类 A。鉴于我们有一个 Dataset[A],我假设数据集的底层模式将具有正确的类型;但是,模式保留为 NullType。以下是如何重现的示例:
case class ExampleInput(
name: String,
age: Integer
)
case class ExampleOutput(
name: String,
age: Integer,
filledLater: String
)
// Define Input
val inputDS: Dataset[ExampleInput] = Seq(ExampleInput("asdf", 1)).toDS
// Calculate Output
val outputDS: Dataset[ExampleOutput] = inputDS
.withColumn("filledLater", lit(null))
.as[ExampleOutput]
预期结果架构:
StructType(
StructField("name", StringType, true),
StructField("age", StringType, false),
StructField("filledLater", StringType, true)
)
观察结果架构:
StructType(
StructField("name", StringType, true),
StructField("age", StringType, false),
StructField("filledLater", NullType, true)
)
这可以通过使用.map(x => x: ExampleOutput) 来解决,但这不太理想。有没有更好的解决方案来自动更新架构而无需手动转换列。
【问题讨论】:
标签: scala apache-spark apache-spark-sql apache-spark-dataset