我需要将 StructType 的空列添加到现有的 DataFrame 中。
尝试以下:
df = df.withColumn("features", typedLit(StructType(Nil)))
And:
df = df.withColumn("features", lit(new GenericRowWithSchema(Array(), StructType(Nil))))
但是,在上述两种情况下,都会收到错误,因为文字类型不受支持。
粗略地说,可以使用用户定义的函数来添加包含空行的列:
def addEmptyRowColumn(df: DataFrame, newColumnName: String): DataFrame = {
val addEmptyRowUdf = udf( () =>
new GenericRowWithSchema(Array(), StructType(Nil)), StructType(Nil))
df.withColumn(newColumnName, addEmptyRowUdf())
}
df = addEmptyRowColumn(df, "features")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)