如何解决如何就地修改数据框,使其ArrayType列不能为nullnullable = false和containsNull = false?
采用以下示例数据框:
val df = Seq(Seq("xxx")).toDF("a")
模式:
root
|-- a: array (nullable = true)
| |-- element: string (containsNull = true)
如何就地修改df
,以使结果数据框在任何地方都不能为空,即具有以下架构:
root
|-- a: array (nullable = false)
| |-- element: string (containsNull = false)
我了解我可以重新创建一个强制执行不可空模式的数据框,例如遵循Change nullable property of column in spark dataframe
spark.createDataFrame(df.rdd,StructType(StructField("a",ArrayType(StringType,false),false) :: Nil))
但这不是结构化流中的选项,因此我希望它是某种就地修改。
解决方法
因此,实现这一目标的方法是使用UserDefinedFunction
// Problem setup
val df = Seq(Seq("xxx")).toDF("a")
df.printSchema
root
|-- a: array (nullable = true)
| |-- element: string (containsNull = true)
解决方案:
import org.apache.spark.sql.types.{ArrayType,StringType}
import org.apache.spark.sql.functions.{udf,col}
// We define a sub schema with the appropriate data type and null condition
val subSchema = ArrayType(StringType,containsNull = false)
// We create a UDF that applies this sub schema
// while specifying the output of the UDF to be non-nullable
val applyNonNullableSchemaUdf = udf((x:Seq[String]) => x,subSchema).asNonNullable
// We apply the UDF
val newSchemaDF = df.withColumn("a",applyNonNullableSchemaUdf(col("a")))
就在那里。
// Check new schema
newSchemaDF.printSchema
root
|-- a: array (nullable = false)
| |-- element: string (containsNull = false)
// Check that it actually works
newSchemaDF.show
+-----+
| a|
+-----+
|[xxx]|
+-----+
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。